我想写一个 点击 按钮 然后 跳转另外一个页面的事件该怎么写?我是新手!麻烦大家帮帮忙

解决方案 »

  1.   

    XXClass extends JFrame implements ActionListener{
    JButton XXButton;
    public void init(){
    XXButton = new JButton("AButton");
    XXButton.addActionListener(this);//注册监听
    }public void actionPerformed(ActionEvent e){//点击事件
    if(e.getActionCommand().equals("AButton")){
    ....
    }
    }
    }
      

  2.   

    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    public class SwitchFrame extends JFrame implements ActionListener{ /**
     * @param args
     */

    JButton bt;
    public SwitchFrame() {
    super("Frame1");
    setSize(100,100);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);

    bt = new JButton("跳转");
    bt.addActionListener(this);
    this.setLayout(new FlowLayout());
    add(bt);
    setVisible(true);
    }

    public static void main(String[] args) {
    // TODO Auto-generated method stub
    new SwitchFrame();
    } @Override
    public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    Object source = e.getSource();
    if(source == bt) {
    this.setVisible(false);
    new Frame2();
    }
    }
    }import javax.swing.JFrame;public class Frame2 extends JFrame{
    public Frame2() {
    super("Frame2");
    setSize(200,200);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setVisible(true);
    }
    }