0

so im having trouble moving components to a certain part of the jframe, ive tried using BorderLayout and setBounds() but both of those don't seem to work.

    // sets the start button
    start = new JButton(imgStart);
    start.setPreferredSize(new Dimension(150, 55));
    start.setFocusable(false);
    start.addActionListener(this);

    // sets the controls button
    controls = new JButton(imgControl);
    controls.setPreferredSize(new Dimension(150, 55));
    controls.setFocusable(false);
    controls.addActionListener(this);
    controls.setBounds(151, 56, 0, 0);

    JFrame frame = new JFrame();
    frame.setContentPane(this);
    frame.add(start);
    frame.add(controls, BorderLayout.WEST);
    frame.setTitle("Freedom Planet");
    frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    frame.addWindowListener(this);
    frame.setResizable(false);
    frame.setSize(imgBackground.getIconWidth(), imgBackground.getIconHeight());
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

I also have the some problem with a panel im using in a different part of my program

    // creates a panel called Info and and adds components to them
    JPanel Info = new JPanel();
    //Info.setBorder(BorderFactory.createLineBorder(Color.black, 5));
    Info.setLayout(new FlowLayout(FlowLayout.LEFT, 30, 10));
    //Info.setBounds(290, 180, -10, 10);
    Info.setOpaque(false);
    Info.add(lblTime);
    Info.add(lblTimer);
    Info.add(lblScore);
    Info.add(lblShowScore);

    addKeyListener(this);
    setFocusable(true);
    JFrame frame = new JFrame();
    frame.setLayout(new BorderLayout());
    frame.setContentPane(this);
    frame.add(Info, BorderLayout.SOUTH);
    frame.add(lblLevel);
    frame.setTitle("Freedom Planet");
    frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    frame.addWindowListener(this);
    frame.setResizable(false);
    frame.setSize(imgBackground[0].getIconWidth(), imgBackground[0].getIconHeight());
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
Tmanx7
  • 1
  • 1
    `im having trouble moving components to a certain part of the jframe` - I have no idea what part of the frame you are trying to move the compnents to. Do you want them vertically/horizontally centered? State a proper requirement. Don't set the preferred size of your components. Each component will determine its own preferred size. Currently you are using a FlowLayout that is left aligned with 30 pixels between each component. Also, variable names should NOT start with an upper case character. Be consistent!!! – camickr Jan 23 '16 at 02:34
  • I'm trying to move the components horizontally and thanks, i didn't notice that – Tmanx7 Jan 23 '16 at 02:40
  • 1) `frame.setSize(imgBackground[0].getIconWidth(), imgBackground[0].getIconHeight());` this does not account for the title bar and borders of the frame. There are better ways to go about this that are too detailed for a comment. 2) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) I suspect this case whould use a `CardLayout` in a single frame to flip between game state panels. ... – Andrew Thompson Jan 23 '16 at 09:05
  • .. 3) Besides using layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) also use layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 4) To make a button larger, give it a larger font, a large icon, or a large margin (`setMargin(Insets)`). – Andrew Thompson Jan 23 '16 at 09:08

1 Answers1

1

I'm trying to move the components horizontally

Then maybe you can use a Swing Border. An EmptyBorder will allow you to add extra space around the components.

camickr
  • 321,443
  • 19
  • 166
  • 288