关于java GUI和clear JFrame以显示新内容?

关于java GUI和clear JFrame以显示新内容?,java,swing,Java,Swing,我正在学习Java和GUI。我有一些问题,第一个问题是创建JFrame的子类和JFrame的实例之间是否有任何主要区别。子类似乎更强大?我还想知道在创建GUI时是否有必要使用以下代码: Container contentPane = getContentPane(); contentPane.setLayot(new Flowlayout()); 我将我的GUI类添加到我必须提交的任务中,到目前为止这只是一个简单的测试。当用户在文本字段中输入了一些文本并按下按钮继续下一步时,我该如何清除框架并

我正在学习Java和GUI。我有一些问题,第一个问题是创建JFrame的子类和JFrame的实例之间是否有任何主要区别。子类似乎更强大?我还想知道在创建GUI时是否有必要使用以下代码:

Container contentPane = getContentPane();
contentPane.setLayot(new Flowlayout());
我将我的GUI类添加到我必须提交的任务中,到目前为止这只是一个简单的测试。当用户在文本字段中输入了一些文本并按下按钮继续下一步时,我该如何清除框架并显示新内容,还是有一种特殊的方法在Java中实现?我想一定有更好的方法使用相同的窗口,而不是创建一个新窗口!?救命啊!谢谢

    // 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){
                // ??
            }
        }
    }
}
如果您想切换(添加然后删除)JComponents,那么您必须

1) 添加/删除JComponents,然后调用

revalidate();
repaint()// sometimes required

2) 如果您需要制作向导、带有“下一步”和“上一步”按钮的面板,以及单击“下一步”和“上一步”按钮以显示某些组件,则最好且最简单的选择是实现。您可以尝试使用CardLayout

CardLayout管理共享相同显示空间的两个或多个组件(通常是JPanel实例)。CardLayout允许用户在组件之间进行选择


这段小代码可能会帮助您解释一些事情:

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();
            }
        });
    }
}

如果您的类扩展了JFrame,则可以执行以下操作:

getContentPane().removeAll();

美好的您可以拥有多少面板有限制吗?所以我只是在面板上添加了不同的内容!?不,实际上没有限制。但您最好了解java.awt包提供的不同布局。这在某种程度上可以帮助你。因为每个组件都有一个默认的布局设置,比如JFrame有BorderLayout,JPanel有FlowLayout。RegardsI不知道他们何时离开ContentPane ContentPane=frame.getContentPane();但是上面的答案修复了我的Jframe在第二次加载时出现的重复数据,
CardLayout
,这比不断添加/删除内容更好