通过单击当前帧上的按钮显示另一帧

时间:2014-02-21 09:18:40

标签: java frame

我需要一种功能,其中单击当前正在显示的帧中的按钮,在删除第一帧的内容后显示具有新内容的新帧。我该怎么办?

我尝试过这样的事情

import java.awt.*;
import java.awt.event.*;

public class test extends Frame implements ActionListener   {
   Button b1;
   public test()    {
    Panel p1 = new Panel();
    p1.setLayout ( new FlowLayout(FlowLayout.CENTER) );
    p1.add (b1 = new Button("click"));
    b1.addActionListener(this);
    add(p1);
   }
public static void main (String[] args)     {
    Frame f1 = new test();
    f1.setVisible(true);
    f1.setSize(400,400);
}

public void actionPerformed (ActionEvent ae)    {
    String str = ae.getActionCommand();
    if (ae.getSource() instanceof Button)   {
        if ( str.equals("click") )  {
            f1.setVisible(false);
            Frame f2 = new test();
        }
    }
}
}

1 个答案:

答案 0 :(得分:0)

看,我知道如何完成。

        import java.awt.*;
    import java.awt.event.*;

    public class test extends Frame implements ActionListener   {
        Button b1;
        Label l1;
        static Frame f1,f2;

        public test()   {
            Panel p1 = new Panel();
            p1.add ( b1 = new Button("click") );
            p1.setLayout (new FlowLayout (FlowLayout.CENTER) );
            add(p1);

            b1.addActionListener(this);
            addWindowListener ( new WindowAdapter() {
                                    public void windowClosing (WindowEvent we)  {
                                        System.exit(0);
                                    }
                                }
                               );
        }

        public test(int i)  {
            Panel p1 = new Panel();
            p1.add(l1 = new Label("new"));
            p1.setLayout (new FlowLayout (FlowLayout.CENTER));
            add(p1);

            addWindowListener ( new WindowAdapter() {
                                    public void windowClosing (WindowEvent we)  {
                                        System.exit(0);
                                    }
                                }
                               );
        }

        public void actionPerformed(ActionEvent ae) {
            String s = ae.getActionCommand();
            if (ae.getSource() instanceof Button)   {
                if (s.equals("click"))  {
                    frame_invisible();
                    f2 = new test(10);
                    f2.setVisible(true);
                    f2.setSize(400,400);
                }
            }
        }

        public void frame_invisible()   {
            f1.setVisible(false);
        }

        public static void main (String[] args) {
            f1 = new test();
            f1.setVisible(true);
            f1.setSize(400,400);
        }
    }