Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7021681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:28:57+00:00 2026-05-27T23:28:57+00:00

I’m learning Java and GUI. I have some questions, and the first is if

  • 0

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){
                // ??
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:28:58+00:00Added an answer on May 27, 2026 at 11:28 pm

    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();
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have thousands of HTML files to process using Groovy/Java and I need to
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.