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”
The problem is you
extendtheJFrameclass from your class, but you initiate your ownJFrameinmain(), also you cannot calladd()because you are in main which isstaticwhileadd()requires an instance to be created before being called, see my comment on your question, however, you should not extend aJFrameand rather do:also always create a EDT (Event Dispatch Thread on which all UI components lay) I did this by
SwingUtilities.invokeLater();also always add to aJFrames contentPane viagetContentPane().add();. Another thing, why do you add your Jframe to your Jframe? as you can see I showed an example of adding aJPanelEDIT:
you make your
JFramea global variable and then you can useframe.setLayout()etc.however if you want your current code to work do this:
but extending a
JFrameis not recommended