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

  • SEARCH
  • Home
  • 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 8523949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:33:15+00:00 2026-06-11T07:33:15+00:00

I am attempting to make a form that takes user inputs for first name

  • 0

I am attempting to make a form that takes user inputs for first name and last name and prints a welcome message combining the two. I wanted to group the input stuff together into a grid panel, then group the “Go” button and the text field that displays the message in another panel, and nest both of those together using a BorderLayout within one frame.

Generic layout:

(in a 3×2 grid)

First Name: __________________________  
Last Name:  __________________________  
Age:        _______________

“GO BUTTON” “Welcome Mark Summers”

The professor wants it all in one .java file which is my problem. I would normally separate these things then extend the classes and have no problem. What I’m trying to do is save the input and output panels under my constructor, then under my main, create

JFrame frame = new JFrame();

and then

add(new CalebBreckonHW3(320,120));

thereby putting a 320×120 panel inside my 700×700 JFrame “frame”. I get an error message though that I can’t reference the non-static method add(java.awt.Component) from a static context. I can’t set my main to non-static or I’ll get an error.

I’m in the process of adding the code to this right now through an Can anyone point me in the right direction?

import java.awt.*;
import javax.swing.*;

public class CalebBreckonHW3 extends JFrame {
    private JButton jbtGreet = new JButton("Greet Me");
    private JLabel firstOprLbl = new JLabel("First name");
    private JLabel lastOprLbl = new JLabel("Last name");
    private JLabel ageOprLbl = new JLabel("Age");
    private JTextField jtfFirst = new JTextField(10);
    private JTextField jtfLast = new JTextField(15);
    private JTextField jtfAge = new JTextField(3);
    // I'll get to the action events after I get this down
    private JTextField jtfGreet = new JTextField("Welcome firstname, lastname");

    public CalebBreckonHW3(int height, int width) {
        setLayout(new BorderLayout());
        setSize(height, width);
        JPanel inputPanel = new JPanel();
        inputPanel.setLayout(new GridLayout(3,2,0,5));
        jtfGreet.setEditable(false);
        inputPanel.add(firstOprLbl);
        inputPanel.add(jtfFirst);
        inputPanel.add(lastOprLbl);
        inputPanel.add(jtfLast);
        inputPanel.add(ageOprLbl);
        inputPanel.add(jtfAge);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setTitle("Greeting App");
        frame.setSize(700,700);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

        add(new CalebBreckonHW3(320,120));
    }
}

EDIT: Adding frame in front of my add statement gets rid of the static error but it gives me an error “adding a window to a container”

  • 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-06-11T07:33:16+00:00Added an answer on June 11, 2026 at 7:33 am

    The problem is you extend the JFrame class from your class, but you initiate your own JFrame in main(), also you cannot call add() because you are in main which is static while add() requires an instance to be created before being called, see my comment on your question, however, you should not extend a JFrame and rather do:

    public class CalebBreckonHW3  {
    
        public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
    
            @Override
            public void run() {
            JFrame frame = new JFrame();
            frame.setTitle("Greeting App");
           // frame.setSize(700,700); //not recommened
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
             JPanel panel=new JPanel(/*set layout if needed*/); 
             //panel.add(new JButton("Hello")); //add components to panel
    
            frame.getContentPane().add(panel);//why did you add your JFrame again???? so I guess you'd wanna add a JPanel or something with the components you need.
    
            frame.pack();
            frame.setVisible(true);
           }
    
        });
     }
    ...
    }
    

    also always create a EDT (Event Dispatch Thread on which all UI components lay) I did this by SwingUtilities.invokeLater(); also always add to a JFrames contentPane via getContentPane().add();. Another thing, why do you add your Jframe to your Jframe? as you can see I showed an example of adding a JPanel

    EDIT:

    you make your JFrame a global variable and then you can use frame.setLayout() etc.

    however if you want your current code to work do this:

    public class CalebBreckonHW3  extends JFrame{
    
        public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
    
            @Override
            public void run() {
            new CalebBreckonHW3().createandShowUI();
           }
    
        });
     }
     private void createAndShowUI() {
            setTitle("Greeting App");
            //setSize(700,700); //not recommened
            setLocationRelativeTo(null);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
             JPanel panel=new JPanel(/*set layout if needed*/); 
             //panel.add(new JButton("Hello")); //add components to panel
    
            getContentPane().add(panel);//why did you add your JFrame again???? so I guess you'd wanna add a JPanel or something with the components you need.
    
            pack();//this will override setsize and use LayoutManager
            setVisible(true);     
     }
    ...
    }
    

    but extending a JFrame is not recommended

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to make a form that asks the user for a number of
I'm attempting to make a form like so, with horizontal layout: <form id=foo name=foo
Attempting to make a NSObject called 'Person' that will hold the login details for
I'm attempting to make a simple linear text game that will display inside a
I am attempting to make my first mobile website by just using a different
Im attempting to make a little app that lets you add text boxes to
I'm attempting to make my form pretty I'm not sure what I'm doing. I've
I am validating input on a form and attempting to prompt the user of
I have a form where two fields on the first page of the form
I'm attempting to post a simple form that includes unicode characters to a servlet

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.