Java GUI - Socket Panels

I am trying to make a form that takes user inputs for a first and last name and prints a welcome message combining the two. I wanted to combine the input data together in a grid panel, then group the Go button and a text box that displays a message in another panel, and nest them together using BorderLayout within the same frame.

General layout:

(in 3x2 grid)

First Name: __________________________ Last Name: __________________________ Age: _______________ 

"GO BUTTON" "Welcome Mark Summers"

The professor wants everything in one .java file, which is my problem. I usually separate these things and then extend the classes and have no problems. What I'm trying to do is save the input and output panels under my constructor, and then under my main task create

 JFrame frame = new JFrame(); 

and then

 add(new CalebBreckonHW3(320,120)); 

thereby placing a 320x120 panel inside a 700x700 "JFrame". I am getting an error, although I cannot reference the non-static add method (java.awt.Component) from the static context. I cannot set my basic non-static or I will get an error.

Am I now adding code to this right through Can Can 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 a frame before my add operation eliminates the static error, but it gives me the error "add window to container"

+4
source share
1 answer

The problem is that 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 the main, which is static while add() requires, so that the instance was created before the call, see my comment on your question, however you should not extend the 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 creates an EDT (Thread Dispatch Thread), on which all user interface components lie. I did this using SwingUtilities.invokeLater(); , also always adding a JFrame contentPane via getContentPane().add(); . Another thing is why are you adding your Jframe to your Jframe? As you can see, I showed an example of adding JPanel

EDIT:

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

however, if you want your current code to work, follow these steps:

 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 a JFrame not recommended

+7
source

Source: https://habr.com/ru/post/1434259/


All Articles