简体   繁体   English

如何使用按钮关闭框架并打开新框架?

[英]How can I use Button to Close Frame and Open New Frame?

That's My Code Down Here. 那是我的代码在这里。 I want the answer for java.awt.Button and java.awt.Frame . 我想要java.awt.Buttonjava.awt.Frame的答案。

Can any one help me with it? 有人可以帮我吗?

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

public class TestGUI extends Frame implements ActionListener, WindowListener{

    private Label lbl;
    private Label lbl1
    private Label lbl2;
    private Label lbl3;
    private TextField tf;
    private TextField tf1;
    private TextField tf2;
    private Button btn;
    private Button btn1;
    private Frame frame;

    public TestGUI() {
        setLayout(new FlowLayout());

        lbl = new Label("Hi Guys! That's My First GUI Program and is made by me too");
        add(lbl);

        lbl1 = new Label("Enter Your Name Please ~");
        add(lbl1);

        tf1 = new TextField(30);
        tf1.setEditable(true);
        add(tf1);

        lbl2 = new Label("Enter Your Age Please ~");
        add(lbl2);

        tf2 = new TextField(30);
        tf2.setEditable(true);
        add(tf2);

        lbl3 = new Label("Enter Your School/College Name Please ~");
        add(lbl3);

        tf = new TextField(28);
        tf.setEditable(true);
        add(tf);

        btn = new Button("Cancel");
        add(btn);

        btn.addActionListener(this);

        addWindowListener(this);

        setTitle("My own GUI");
        setSize(500, 300);

        setVisible(true);
    }

    public static void main(String[] args){
        TestGUI app = new TestGUI();
    }

    @Override
    public void actionPerformed(ActionEvent evt){

    }

    @Override
    public void windowClosing(WindowEvent evt){
        System.exit(0);
    }

    @Override public void windowDeactivated(WindowEvent evt){}

    @Override public void windowActivated(WindowEvent evt){}

    @Override public void windowOpened(WindowEvent evt){}

    @Override public void windowClosed(WindowEvent evt){}

    @Override public void windowIconified(WindowEvent evt){}

    @Override public void windowDeiconified(WindowEvent evt){}
}

Thanks in Advance. 提前致谢。

You're just complicating the things. 您只是使事情复杂化。 Instead of extending the frame & implementing those interfaces, just extend JFrame. 无需扩展框架和实现这些接口,只需扩展JFrame。

public class TestGUI extends JFrame{...}

In your TestGUI frame create another JFrame say otherFrame and create two bottons say Open & Close and then bind ActionListener to them. 在您的TestGUI框架中,创建另一个JFrame,例如otherFrame,并创建两个botton,分别表示“打开和关闭”,然后将ActionListener绑定到它们。

openBtn.addActionListener(new ActionListener(){
    otherFrame.setVisible(true);
});

closeBtn.addActionListener(new ActionListener(){
    otherFrame.setVisible(false);
});

The setVisible() method accepts boolean & this is what you actually need. setVisible()方法接受布尔值,这是您实际需要的。 Much simpler & cleaner code. 更简单,更干净的代码。

It might make more sense for you to use a JFrame instead of a Frame (I recomend you read Kumar Vivek Mitra's answer here to get a better idea of why). 对于您来说,使用JFrame而不是Frame可能更有意义(我建议您在此处阅读Kumar Vivek Mitra的答案以更好地了解原因)。 If you use a JFrame, you'll need to call yourJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) to stop your program when you close the window. 如果使用JFrame,则在关闭窗口时需要调用yourJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)来停止程序。

To respond to your button clicks, simply pass Anonymous Classes to your buttons addOnClickListener() method, like this: 要响应您的按钮单击,只需将匿名类传递给您的按钮addOnClickListener()方法,如下所示:

btn.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {

                        //Do stuff here

                    }

                });

Then you should be able to remove your existing actionPerformed() method. 然后,您应该可以删除现有的actionPerformed()方法。

For opening a new frame and closing your existing one, you should be creating two JFrame objects instead of extending Frame (or JFrame). 要打开一个新框架并关闭现有框架,您应该创建两个JFrame对象,而不是扩展Frame(或JFrame)。 Then, when you want to open your second frame, just call secondFrame.setVisable(true) , and close your first one with firstFrame.dispose . 然后,当您要打开第二个框架时,只需调用secondFrame.setVisable(true) ,并使用firstFrame.dispose关闭第一个firstFrame.dispose However, I'd have a look at JDialogs and JOptionPanes first to see if they might work better for you. 但是,我先来看一下JDialogs和JOptionPanes ,看看它们是否可能对您更好。

After all this you should be able to remove all your WindowListener stuff, as that's for something slightly different. 完成所有这些之后,您应该可以删除所有WindowListener内容,因为那是稍有不同的内容。 (Have a look here if you're interested) (如果您有兴趣,请在这里看看)

Finally, don't forget to add a semicolon after your lbl1 label. 最后,不要忘记在lbl1标签之后添加分号。 ;) ;)

Good luck! 祝好运!

You may use ActionListener interface. 您可以使用ActionListener接口。 However for a little addition to above guys commented. 但是,除了上述几点,其他人发表了评论。 You may add animation to your frame by adding for loop and setSize method within the loop and the height width of the corresponding loop's variable. 您可以通过在循环内添加for循环和setSize方法以及相应循环变量的高度宽度,来向帧中添加动画。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM