0

I am trying to make a GUI for a simple program. I simply want a text field so that a user can input some text, but the JTextField will not display in my JPanel, even though both my buttons and labels disply with no problems. any help on how to fix this problem much appreciated. Here is my code.

public static void main(String[] args) {



    JFrame frame = new JFrame("David's cube program!");
      frame.setVisible(true);
      frame.setSize(800,350); // i used some code from 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setResizable(false);


      frame.add(panel1);
      panel1.setLayout(null);

      JButton button = new JButton("Click to display moves");
      panel1.add(button);
      button.setLocation(150, 20);
      button.setSize(200,50);
      button.addActionListener (new Button1Action()); 

      JLabel textArea = new JLabel("Click the button to run moves!"); 
      panel1.add(textArea);
      textArea.setLocation(15, 40);
      textArea.setSize(600, 100);

      textBox = new JTextField(20);
      textBox.setFont(textBox.getFont().deriveFont(20f));
      textBox.setLocation(15, 180);
      textBox.addActionListener (new TextAction() );
      textBox.setVisible(true);
      panel1.add(textBox);

      JButton button2 = new JButton("Click to change file path");
      panel1.add(button2);
      button2.setLocation(150, 160);
      button2.setSize(200,50);
      button2.addActionListener (new Button2Action()); 

      JLabel textArea2 = new JLabel("the textfield should be just above me"); 
      panel1.add(textArea2);
      textArea2.setLocation(15, 200);
      textArea2.setSize(600, 100);

}
FLOX DAW
  • 1
  • 3
  • Did you try to `validate`/`revalidate` the panel, or `repaint`? – Idos May 04 '16 at 08:58
  • Also call `frame.setVisible(true);` at the **end** – Idos May 04 '16 at 08:59
  • And you didn't give any size to `textBox` – Arnaud May 04 '16 at 09:02
  • `panel1.setLayout(null);` 1) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). That GUI could be easily laid out using a `GridBagLayout`. – Andrew Thompson May 04 '16 at 10:04
  • Avoid using `null` layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify – MadProgrammer May 04 '16 at 10:20
  • @Idos Remember, those methods only work if you're using a layout manager – MadProgrammer May 04 '16 at 10:20

3 Answers3

0

Move frame.add(panel1) to the end of main, just before 'frame.setVisible(true)'. So, the last threelines of main become:

textArea2.setSize(600, 100);
frame.add(panel1);
frame.setVisible(true)
AhmadWabbi
  • 2,253
  • 1
  • 20
  • 35
0

I put frame.setVisible(true) as the last line and textBox.setSize(100, 50); in with the textBox variables and this solved the problem.

FLOX DAW
  • 1
  • 3
0
  • As you are using absolute layout, it is necessary to mention the size and location of the UI components.
  • Frame should be made visible only at the end.
  • It is not recommended to use absolute layout. Try using other Layouts like GridBagLayout , BorderLayout, GridLayout, CardLayout and so on.
Beniton Fernando
  • 1,533
  • 1
  • 14
  • 21