0

So, I have this JFrame code, where everything seems to go well, and there are no errors, but all the components (except for one component) are ignoring the Y and X axis. Why is this happening? I even tried inserting some extremely ridiculous Y axis, such as 456789 or even -80, and it stays the same. Why?

My code:

    frame.setSize(750,200);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel = new JPanel();
    frame.setContentPane(panel);

    textField.setBounds(0,0,40,200);
    textField.setVisible(true);
    panel.add(textField);

    button.setBounds(200, 0, 40, 200);
    button.setVisible(true);
    panel.add(button);

    bInfo.setBounds(0,80,40,400);
    bInfo.setVisible(false);
    panel.add(bInfo);

    label.setBounds(0,40,40,160);
    label.setVisible(false);
    panel.add(label);

    registerButton();
    registerMoreInfoButton();

    frame.setVisible(true);

Thanks in advance! :)

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
NonameSL
  • 37
  • 4
  • 3
    1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve). 2) Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead [use layout managers](http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html), or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Jul 11 '14 at 16:19

1 Answers1

3

A JPanel uses FlowLayout by default. Calls to setBounds() and using a LayoutManager are mutually exclusive. You either use a LayoutManager (overriding any setBounds() you do) or you use no LayoutManager (setLayout(null)) and setBounds().

The preferred way is to use a LayoutManager. Learn how here http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

Durandal
  • 19,919
  • 4
  • 36
  • 70