主题:看看我的程序
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TestActionEvent extends JFrame{
private JButton jbtok=new JButton("ok");
private JButton jbtCancel=new JButton("Cancel");
public TestActionEvent(){
setTitle("TestActionEvent");
getContentPane().setLayout(new GridLayout(2,1));
JPanel p1=new JPanel(new FlowLayout());
//JPanel p2=new JPanel();
getContentPane().add(p1);
p1.add(jbtok);
p1.add(jbtCancel);
ButtonListener btListener=new ButtonListener();
jbtok.addActionListener(btListener);
//btListener.ad(p2);
jbtCancel.addActionListener(btListener);
getContentPane().add(btListener.p);
}
public static void main(String[] args){
TestActionEvent frame=new TestActionEvent();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,160);
frame.setVisible(true);
}
}
class ButtonListener implements ActionListener{
// JTextField T;
JPanel p=new JPanel(new FlowLayout());
public void actionPerformed(ActionEvent e){
//System.out.println("This is "+e.getActionCommand()+"button");
p.add(new JTextField("This is "+e.getActionCommand()+"button"),new FlowLayout());
}
}
我要实现 当点击OK按钮时,信息面板上显示“OK button is clicked”,当点击Cancel按钮时,信息面板上显示“Cancel button is clicked”。
import java.awt.*;
import java.awt.event.*;
public class TestActionEvent extends JFrame{
private JButton jbtok=new JButton("ok");
private JButton jbtCancel=new JButton("Cancel");
public TestActionEvent(){
setTitle("TestActionEvent");
getContentPane().setLayout(new GridLayout(2,1));
JPanel p1=new JPanel(new FlowLayout());
//JPanel p2=new JPanel();
getContentPane().add(p1);
p1.add(jbtok);
p1.add(jbtCancel);
ButtonListener btListener=new ButtonListener();
jbtok.addActionListener(btListener);
//btListener.ad(p2);
jbtCancel.addActionListener(btListener);
getContentPane().add(btListener.p);
}
public static void main(String[] args){
TestActionEvent frame=new TestActionEvent();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,160);
frame.setVisible(true);
}
}
class ButtonListener implements ActionListener{
// JTextField T;
JPanel p=new JPanel(new FlowLayout());
public void actionPerformed(ActionEvent e){
//System.out.println("This is "+e.getActionCommand()+"button");
p.add(new JTextField("This is "+e.getActionCommand()+"button"),new FlowLayout());
}
}
我要实现 当点击OK按钮时,信息面板上显示“OK button is clicked”,当点击Cancel按钮时,信息面板上显示“Cancel button is clicked”。