Beto
Beto

Reputation: 806

how do I move around java gui elements?

    Panel controlPanel = new Panel ();
    JPanel chatPanel = new JPanel();
    JPanel buttomsPanel = new JPanel();

    controlPanel = new Panel();
    controlPanel.setLayout(new FlowLayout(10));



    Panel panel = new Panel();

    panel.setBackground(Color.DARK_GRAY);
    panel.setSize(700,700);
    GridLayout layout = new GridLayout(6,6, 10, 10);

    panel.setLayout(layout);


    for (int i= 0; i < 36; i++){
         panel.add(new Button(""));
    }
    controlPanel.add(panel);


    buttomsPanel.add(new Button(""));
    frame.add(buttomsPanel);


    frame.add(controlPanel);
    frame.setSize(900, 700);

Basically the second button that I add to the gui won't be displayed. Also, I want to put another two fields and I do not know how to move them either, or whether they should go into a panel first and then be moved.

Upvotes: 0

Views: 1808

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347204

Lets start with the ambaguity...

Button is either a java.awt.Button or a javafx.scene.control.Button, in either case, it's probably not going to play well with a JPanel.

Panel is a java.awt.Panel and won't play well with Swing. It's just easier to not mix AWT components within Swing.

Next, JFrame uses a BorderLayout by default, this means when you do something like...

frame.add(buttomsPanel);
frame.add(controlPanel);

It's the controlPanel which is likely to be the one which is displayed (and given the oddities of z-ordering between AWT and Swing, it's likely to want to be displayed regardless of the order you add them).

To fix this, you need to specify the position within the BorderLayout where you want the panels added, for example...

frame.add(buttomsPanel);
frame.add(controlPanel, BorderLayout.SOUTH);

And use a JPanel instead of a Panel and JButtons instead of Button...

See How to Use Borders for details

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Panel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }
                JPanel controlPanel = new JPanel(new FlowLayout(10));
                JPanel chatPanel = new JPanel();
                JPanel buttomsPanel = new JPanel();

                Panel panel = new Panel();

                panel.setBackground(Color.DARK_GRAY);
                panel.setSize(700, 700);
                GridLayout layout = new GridLayout(6, 6, 10, 10);

                panel.setLayout(layout);

                for (int i = 0; i < 36; i++) {
                    panel.add(new JButton(""));
                }
                controlPanel.add(panel);

                buttomsPanel.add(new JButton(""));

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(buttomsPanel);
                frame.add(controlPanel, BorderLayout.SOUTH);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}

Upvotes: 1

Related Questions