【发布时间】:2014-03-04 03:27:19
【问题描述】:
好的。我不确定我的问题的标题以及我是否使用了正确的词。 由于我是一个自学成才的业余爱好者,我发现很难问我的问题,因为我不知道正确的术语,所以我会用代码写一些东西然后问我的问题。我编写它时没有导入语句、设置布局和滚动条以及其他一些东西,只是为了让它更简单。
public class Foo{
JTextArea text;
public static void main(String[] args){
Foo foo = new Foo;
foo.go();
}
public void go(){
JFrame frame = new JFrame();
JButton button = new JButton("One");
JButton button2 = new JButton("Two");
JPanel panel = new JPanel();
frame.setVisible(true);
frame.setSize(600, 300);
frame.getContentPane().add(BorderLayout.EAST, panel);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(button);
panel.add(button2);
text = new JTextArea(10, 20);
panel.add(text);
button.addActionListener(new ButtLis());
button2.addActionListener(new ButtLis());
}
class ButtLis implements ActionListener{
@override
// this is where I have the problem
text.append();
}
}
我想要的是进入我的内部类 (ButtLis) 的 if 语句,它将确定按下了哪些按钮,然后基于此将某些文本附加到 JTextArea。但我不知道该调用什么来找出按下了哪个按钮。
【问题讨论】:
-
event.getSource()在您的ActionPerformed(ActionEvent event)中。您需要适当地转换它。
标签: java swing jbutton actionlistener