简体   繁体   English

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

[英]About java GUI and clear JFrame to show new content?

I'm learning Java and GUI. 我正在学习Java和GUI。 I have some questions, and the first is if there are any major difference between creating a subclass of JFrame and an instance of JFrame. 我有一些问题,第一个是如果创建JFrame的子类和JFrame的实例之间有任何重大区别。 It seems like like a subclass is more powerful? 看起来像子类更强大? I also wonder if it's necessary to use this code when creating a GUI: 我也想知道在创建GUI时是否有必要使用此代码:

Container contentPane = getContentPane();
contentPane.setLayot(new Flowlayout());

I add my GUI class, it's a simple test so far, to a task that I have to hand in. When a user has entered some text in the textfield and press the button to continue to the next step, how do I do to clear the frame and show a new content or is there a special way to do this is in Java? 我添加了我的GUI类,这是一个简单的测试,到目前为止,我必须提交的任务。当用户在文本字段中输入一些文本并按下按钮继续下一步时,我该怎么做才能清除框架和显示新内容或有一种特殊的方式来做到这一点是在Java? I guess there must be better to use the same window instead of creating a new!? 我想最好使用相同的窗口,而不是创建一个新的!? Help id preciated! 帮助id精确! Thanks 谢谢

    // 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){
                // ??
            }
        }
    }
}

This small code might help you explain things : 这个小代码可以帮助您解释一些事情:

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

if you want to switch (add then remove) JComponents, then you have to 如果你想切换(添加然后删除)JComponents,那么你必须

1) add/remove JComponents and then call 1)添加/删除JComponents然后调用

revalidate();
repaint()// sometimes required

2) better and easiest choice would be implements CardLayout 2)更好和最简单的选择是实现CardLayout

If your requirement is to make a wizard, a panel with next and prev buttons, and on clicking next/prev button showing some component. 如果您的要求是制作向导,带有next和prev按钮的面板,以及单击next / prev按钮显示某个组件。 You could try using CardLayout. 您可以尝试使用CardLayout。

The CardLayout manages two or more components (usually JPanel instances) that share the same display space. CardLayout管理共享相同显示空间的两个或多个组件(通常是JPanel实例)。 CardLayout let the user choose between the components. CardLayout让用户在组件之间进行选择。

How to Use CardLayout 如何使用CardLayout

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

getContentPane().removeAll();

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

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