Why are the contents of my JFrame not displaying correctly?

java, jframe, swing

Solution

You are using absolute positions without using a `null` layout, that's why you see a large button.

To see every component, you have to use

iF.setLayout(null);

but it's not a good practice, I'd suggest you to learn how to use Layouts and leave all the work to the layout manager

Problem

For the sake of less code, I am taking out code that is unrelevant to the problem, such as `addActionListener();` etc. My main class is `public class TF2_Account_Chief` I have my main `Jframe f;` and its contents: ``` private static JFrame f = new JFrame("TF2 Account C.H.I.E.F."); private JLabel runnableTogetherLabel = new JLabel("How many idlers would you like to run at a time?"); private static JTextField runnableTogetherInput = new JTextField(); private JButton runButton = new JButton("Run!"); private JButton stopButton = new JButton("Stop!"); private JButton resetButton = new JButton("Reset!"); private JButton exitButton = new JButton("Exit!"); ``` and I set the properties of all the contents: ``` public void launchFrame() { f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.pack(); f.setVisible(true); f.add(runnableTogetherInput); f.add(runnableTogetherLabel); f.add(runButton); f.add(stopButton); f.add(resetButton); f.add(exitButton); f.setSize(625, 500); runnableTogetherInput.setSize(35, 20); runnableTogetherLabel.setSize(275, 25); runButton.setSize(60, 25); stopButton.setSize(65, 25); resetButton.setSize(70, 25); exitButton.setSize(60, 25); f.setLocation(0, 0); runnableTogetherInput.setLocation(285, 3); runnableTogetherLabel.setLocation(5, 0); runButton.setLocation(330, 0); stopButton.setLocation(395, 0); resetButton.setLocation(465, 0); exitButton.setLocation(540, 0); } ``` then I have my `main()` method: ``` public static void main(String[] args) throws IOException { TF2_Account_Chief gui = new TF2_Account_Chief(); gui.launchFrame(); Container contentPane = f.getContentPane(); contentPane.add(new TF2_Account_Chief()); } ``` And then I have my second `JFrame iF` which is not displaying the contents correctly: ``` private void invalidInput() { JFrame iF = new JFrame("Invalid Input!"); JLabel iL = new JLabel("The input you have entered is invalid!"); JButton iB = new JButton("Exit!"); Container contentPane2 = iF.getContentPane(); contentPane2.add(new TF2_Account_Chief()); iF.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); iF.pack(); iF.setVisible(true); iF.add(iL); iF.add(iB); iF.setSize(500, 300); iL.setSize(125, 25); iB.setSize(60, 25); iF.setLocation(0, 0); iL.setLocation(0, 15); iB.setLocation(0, 45); } ``` Now, `JFrame iF` is launched when the `invalidInput()` method is called, but you can't see that because that part of the code is unrelevant to the problem. What does matter is that the `JFrame iF` is launched. The new frame looks like this: Any ideas on why the contents are not displaying properly? (By improperly, I mean the `JButton iB` takes up the whole frame and the frame is a light blue instead of the normal grey.)

Original source