Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

About java GUI and clear JFrame to show new content?

Tags:

java

swing

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){
                // ??
            }
        }
    }
}
like image 310
3D-kreativ Avatar asked Dec 21 '22 03:12

3D-kreativ


1 Answers

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();
            }
        });
    }
}
like image 180
nIcE cOw Avatar answered Dec 24 '22 02:12

nIcE cOw