简体   繁体   中英

About java GUI and clear JFrame to show new content?

I'm learning Java and 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. It seems like like a subclass is more powerful? I also wonder if it's necessary to use this code when creating a 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? I guess there must be better to use the same window instead of creating a new!? Help id preciated! 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

1) add/remove JComponents and then call

revalidate();
repaint()// sometimes required

2) better and easiest choice would be implements 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. You could try using CardLayout.

The CardLayout manages two or more components (usually JPanel instances) that share the same display space. CardLayout let the user choose between the components.

How to Use CardLayout

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

getContentPane().removeAll();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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