关于java GUI和清除JFrame来显示新内容?

3D-*_*tiv 4 java swing

我正在学习Java和GUI.我有一些问题,第一个是如果创建JFrame的子类和JFrame的实例之间有任何重大区别.看起来像子类更强大?我也想知道在创建GUI时是否有必要使用此代码:

Container contentPane = getContentPane();
contentPane.setLayot(new Flowlayout());
Run Code Online (Sandbox Code Playgroud)

我添加了我的GUI类,这是一个简单的测试,到目前为止,我必须提交的任务.当用户在文本字段中输入一些文本并按下按钮继续下一步时,我该怎么做才能清除框架和显示新内容或有一种特殊的方式来做到这一点是在Java?我想最好使用相同的窗口,而不是创建一个新的!?帮助id精确!谢谢

    // Gui class

    import java.awt.FlowLayout; // layout
    import java.awt.event.ActionListener; // listener
    import java.awt.event.ActionEvent; // event

    import javax.swing.JFrame; // windows properties
    import javax.swing.JLabel; // row of text
    import javax.swing.JTextField; // enter text
    import javax.swing.JOptionPane; // pop up dialog
    import javax.swing.JButton; // buttons

    // import.javax.swing.*;

    public class Gui extends JFrame {

    private JLabel text1;
    private JTextField textInput1;
    private JTextField textInput2;
    private JButton nextButton;

    // constructor creates the window and it's components
    public Gui() {
        super("Bank"); // title
        setLayout(new FlowLayout()); // set default layout

        text1 = new JLabel("New customer");
        add(text1);

        textInput1 = new JTextField(10);
        add(textInput1);

        nextButton = new JButton("Continue");
        add(nextButton);

        // create object to handle the components (action listener object)
        frameHandler handler = new frameHandler();
        textInput1.addActionListener(handler);
        nextButton.addActionListener(handler);
    }

    // handle the events (class inside another class inherits contents from class outside)
    private class frameHandler implements ActionListener {

        public void actionPerformed(ActionEvent event){

            String input1 = "";

            // check if someone hits enter at first textfield
            if(event.getSource() == textInput1){
                input1 = String.format(event.getActionCommand());
                JOptionPane.showMessageDialog(null, input1);
            }

            else if(event.getSource() == nextButton){
                // ??
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

nIc*_*cOw 5

这个小代码可以帮助您解释一些事情:

import java.awt.event.*;
import javax.swing.*;

public class FrameDisplayTest implements ActionListener
{
    /*
     * Creating an object of JFrame instead of extending it 
     * has no side effects.
     */
    private JFrame frame;
    private JPanel panel, panel1;
    private JTextField tfield;
    private JButton nextButton, backButton;

    public FrameDisplayTest()
    {
        frame = new JFrame("Frame Display Test");
        // If you running your program from cmd, this line lets it comes
        // out of cmd when you click the top-right  RED Button.
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        panel = new JPanel();
        panel1 = new JPanel();

        tfield = new JTextField(10);

        nextButton = new JButton("NEXT");
        backButton = new JButton("BACK");
        nextButton.addActionListener(this);
        backButton.addActionListener(this);

        panel.add(tfield);
        panel.add(nextButton);
        panel1.add(backButton);

        frame.setContentPane(panel);
        frame.setSize(220, 220);
        frame.setVisible(true);
    }

    public void actionPerformed(ActionEvent ae)
    {
        JButton button = (JButton) ae.getSource();
        if (tfield.getText().length() > 0)
        {
            if (button == nextButton)
            {   
                /*
                 * this will remove the first panel 
                 * and add the new panel to the frame.
                 */
                frame.remove(panel);
                frame.setContentPane(panel1);
            }
            else if (button  == backButton)
            {
                frame.remove(panel1);
                frame.setContentPane(panel);
            }
            frame.validate();
            frame.repaint(); // prefer to write this always.
        }
    }   

    public static void main(String[] args)
    {   
        /*
         * This is the most important part ofyour GUI app, never forget 
         * to schedule a job for your event dispatcher thread : 
         * by calling the function, method or constructor, responsible
         * for creating and displaying your GUI.
         */
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new FrameDisplayTest();
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)