最好是给两个button个加一个监听addActionListener(),然后写一个actionPerformed(ActionEvent e) 函数,在函数里面判断是对那个button的操作方法是:
if (e.getActionCommand()=="*****")
then
{}
else
{}

解决方案 »

  1.   

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;public class bean7 extends JFrame implements ActionListener {
    JPanel contentPanel=(JPanel)this.getContentPane();
    JButton jb1=new JButton("myJButton1");
    JButton jb2=new JButton("myJButton2");
    public bean7() {
        super("myFrame");    this.setSize(400,300);
        this.setResizable(false);
        this.setLocation(this.getToolkit().getScreenSize().width/2-200,this.getToolkit().getScreenSize().height/2-150);
        contentPanel.setLayout(new FlowLayout());
        contentPanel.add(jb1);
        contentPanel.add(jb2);
        jb1.addActionListener(this);
        jb2.addActionListener(this);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setVisible(true);
    }public void actionPerformed(ActionEvent e) {
    JButton myJB=(JButton)e.getSource();
    javax.swing.JOptionPane.showMessageDialog(contentPanel,myJB.getText());
    }public static void main(String[] args) throws Exception {
    new bean7();
    }
    }
      

  2.   

    按钮的点击时间最好使用actionPerformed事件
      

  3.   

    sorry
    应该是,
    按钮的点击动作最好使用actionPerformed事件侦帧听
      

  4.   

    为什么我这么写就错?
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    public class w implements ActionListener{
      Frame f;
      Button b1;
      Button b2;
          TextField t1;
          TextField t2;
          TextArea ta;
    public static void main(String args[]){
    w tt=new w();
    tt.go();
    }
    public void go(){
    try{
     f=new Frame("test");
     b1=new Button("input");
     b2=new Button("output");
     t1=new TextField();
     t2=new TextField();
     ta=new TextArea();
    f.setLayout(new GridLayout(3,2));
    f.add(t1);
    f.add(t2);
    f.add(b1);
    f.add(b2);
    f.add(ta);
    f.setSize(300,300);
    f.setVisible(true);
    b1.addActionListener(this);
    b2.addActionListener(this); }catch(Exception e){}
    }
    public void actionPerformed(ActionEvent e){
    try{
    if (e.getActionCommand()=="input")
    {   //w w1=new w();
    OutputStream os=new FileOutputStream("test.txt");
            PrintWriter pw=new PrintWriter(os);
            pw.println(t1.getText());
            pw.println(t2.getText());
            pw.close();
            os.close();
    }
    else 
    {DataInputStream dis=new DataInputStream(new FileInputStream("test.txt"));
                    String s="";
                    while((s=dis.readLine())!=null)
                    ta.setText(ta.getText()+"\r\n"+s);
    }
    }catch(Exception e1){}
    }
    }
      

  5.   

    给的错误是:w.java uses or overrides a deprecated api;
    recompile with -deprecation for details.
      

  6.   

    这个不是错误,是你使用了不鼓励使用的api
    编译的时候这样:
    javac w.java -deprecation
    会给出警告,不过仍然是编译成功了的,然后运行
    java w
    就可以了