ACM Interactors Freeze

寵の児 提交于 2019-11-26 11:39:29

问题


I\'m trying to make a very simple program with Swing and ACM interactors. It is taken directly from a class handout, but does not function on my computer. When I run it, it functions fine for about half a second, then briefly flashes, reloads, and then all button and text-field functionality is lost. Here\'s the code:

import acm.program.*;
import java.awt.event.*;
import javax.swing.*;

public class TextFieldExample extends ConsoleProgram {

public void init() {
    nameField = new JTextField(15);
    add(new JLabel(\"Name: \"), SOUTH);
    add(nameField, SOUTH);
    nameField.addActionListener(this);
}

public void actionPerformed(ActionEvent e) {
    if (e.getSource() == nameField) {
        println(\"Hello, \" + nameField.getText());
    }
}

private JTextField nameField;
}

If it helps, I\'m using Java SE 1.6 with Eclipse Helios Service Release 2 on a mid-2010 Mac Pro running Mac OSX 10.8.4


回答1:


As a workaround, in addition to using Java 1.5, add the field to the NORTH. Also, you may want to extend GraphicsProgram.

Modified SSCCE:

import acm.program.*;
import java.awt.event.*;
import javax.swing.*;

public class TextFieldExample extends GraphicsProgram {

    @Override
    public void init() {
        nameField = new JTextField(15);
        add(new JLabel("Name: "), NORTH);
        add(nameField, NORTH);
        nameField.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == nameField) {
            println("Hello, " + nameField.getText());
        }
    }
    private JTextField nameField;
}


来源:https://stackoverflow.com/questions/17872245/acm-interactors-freeze

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!