50,528
社区成员
发帖
与我相关
我的任务
分享
public class ActionEventDemo1{
public static void main(String[] args) {
new Paint();
}
}
class Paint{
private JFrame frame = null;
private JButton but = null;
private JTextArea textArea = null;
public Paint(){
frame = new JFrame("ActionEvent事件");
frame.setLayout(new GridLayout(2,1));
but = new JButton("显示");
textArea = new JTextArea();
frame.add(but);
frame.add(textArea);
frame.setSize(400,250);
frame.setLocation(400,400);
frame.setVisible(true);
//按钮事件
but.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if(e.getSource()==but){ //获取事件并判断是否为该按钮事件
String str = "按钮,";
textArea.append(str);
}
}
});
}
}
public class ActionEventDemo2{
public static void main(String[] args) {
new Paint();
}
}
class Paint implements ActionListener{
private JFrame frame = null;
private JButton but = null;
private JTextArea textArea = null;
public Paint(){
frame = new JFrame("ActionEvent事件");
frame.setLayout(new GridLayout(2,1));
but = new JButton("显示");
textArea = new JTextArea();
frame.add(but);
frame.add(textArea);
frame.setSize(400,250);
frame.setLocation(400,400);
frame.setVisible(true);
//按钮事件
but.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==but){ //获取事件并判断是否为该按钮事件
String str = "按钮,";
textArea.append(str);
}
}
}