-1

I've put quite a few objects into this JFrame. however many of them do not appear until I hover them mainly the button as well as the JTextField. I only included the JSpinner components so that it wasn't so long. Is there anything in my spinners or lacking with my spinners that would make them not appear? I've stripped it down alot so that it is just one textfield. Still nothing shows up. I've put everything in panels, however still nothing shows up.

public static void screen() {
    JFrame myFrame = new JFrame("Yore");
    myFrame.setAlwaysOnTop(true);
    myFrame.setResizable(false);
    myFrame.setForeground(Color.BLACK);
    myFrame.setSize(300,365);
    myFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    myFrame.setVisible(true);
    myFrame.setBackground(Color.WHITE);
    JPanel panel = new JPanel();
    myFrame.setContentPane(panel);
    JTextField Username = new JTextField();
    Username.setText("Username");
    Username.setColumns(10);
    panel.add(Username);

    }
Xofox
  • 21
  • 7
  • 1
    1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) 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). **My first guess is that this is the core of the problem. But I don't like guessing - so post an MCVE.** – Andrew Thompson Jul 19 '17 at 17:22
  • Thanks for your help. I'll keep that in mind. Thank you for the advice to use layout managers I'll try that right now. – Xofox Jul 19 '17 at 17:27
  • 1
    *"Anything else you would suggest?"* Post an MCVE. – Andrew Thompson Jul 19 '17 at 18:07
  • Note: An MCVE / SSCCE is ***not*** uncompilable code snippets. Please read the documents again, carefully. – Andrew Thompson Jul 20 '17 at 12:28

4 Answers4

0

Try using a layout manager for the frame. Also, add a JPanel to the frame and add all of the Spinners to the Panel.

Using the correct layout manager, you can programatically add the Spinners to the Panel, so that you do not need to hard code the bounds. You will also have the ability to have them resize as the window resizes, for example.

Take a look here to get started with layout managers. https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

BaneDad
  • 157
  • 13
  • Ok, so I backed everything up and started over. However after re adding the textpanel it still didn't appear. I even added the form layout, however still nothing. Anything else you would suggest? – Xofox Jul 19 '17 at 17:55
  • It is hard to continue to help you with such limited code you have provided – BaneDad Jul 19 '17 at 20:11
0

myFrame.getContentPane() returns an AWT container, not a Swing component. To change this you have to use

JPanel panel = new JPanel();
panel.add(..);
myFrame.setContentPane(panel);
panel.validate();

Add your spinners to the panel and your problem is solved.

Earl Dumarest
  • 116
  • 1
  • 3
0

Sorry, i forgot pne statement.

public static void screen() {
    JFrame myFrame = new JFrame("Yore");
    myFrame.setAlwaysOnTop(true);
    myFrame.setResizable(false);
    myFrame.setForeground(Color.BLACK);
    myFrame.setSize(300,365);
    myFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    myFrame.setVisible(true);
    myFrame.setBackground(Color.WHITE);
    JPanel panel = new JPanel();
    myFrame.setContentPane(panel);
    JTextField Username = new JTextField();
    Username.setText("Username");
    Username.setColumns(10);
    panel.add(Username); 

  panel.validate();

  }
Earl Dumarest
  • 116
  • 1
  • 3
0

Add spinners to the panel. and then add panel to your JFrame.

panel.add(spinner_5);
bhavna garg
  • 270
  • 2
  • 19