Java,网格布局问题。难以放置元件

标签 java swing layout-manager grid-layout

我认为 GridLayout 有问题。 我尝试将 25 个按钮放置到 Jpanel“lightJpanel”上,但所有按钮都只位于 lightJpanel 左上角的一个按钮上。我尝试了很多东西,但我没有发现问题...... 按钮已创建,但不可见。 解决问题的办法?

这是我的代码当前给我的内容:

enter image description here

这是我的代码:

public class Window extends JFrame {

    /**
     * Main JPanel
     */
    private JPanel container = new JPanel();
    /**
     * Message area
     */
    private JLabel screen = new JLabel();
    /**
     * table light
     */
    private Light light[][] = new Light[5][5];
    /**
     * button who allows to configure the start of game
     */
    private JButton configure = new JButton("Configure the lights");
    /**
     * button who allows to start the game
     */
    private JButton play = new JButton("Play");
    /**
     * button who allows to place random lights
     */
    private JButton random = new JButton("Random configuration");
    /**
     * stop button for stop the game
     */
    private JButton stop = new JButton("Stop");


    /**
     * construct a window
     *
     * @param controller the controller of the window
     */
    public Window() {
        this.setSize(500, 500);
        this.setTitle("Game of life");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        initComponents();
        this.setContentPane(container);
        this.setVisible(true);
    }

    /**
     * Initialization of graphical components
     */
    private void initComponents() {

        //for the message
        Font font = new Font("Arial", Font.BOLD, 20);
        screen = new JLabel("Game of life");
        screen.setFont(font);
        screen.setForeground(Color.white);

        JPanel panScreen = new JPanel();
        panScreen.setPreferredSize(new Dimension(480, 40));

        JPanel menuButton = new JPanel();
        menuButton.setPreferredSize(new Dimension(480, 100));

        JPanel lightJpanel = new JPanel();
        lightJpanel.setPreferredSize(new Dimension(300, 300));

        //listeners for menu buttons
        MenuListener menuListener = new MenuListener();

        lightJpanel.setLayout(new GridLayout(5, 5, 0, 0));

        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j++) {
                light[i][j] = new Light(i, j);
                lightJpanel.add(light[i][j]);
            }
        }

        //size button
        stop.setPreferredSize(new Dimension(70, 40));
        play.setPreferredSize(new Dimension(70, 40));
        random.setPreferredSize(new Dimension(200, 40));
        configure.setPreferredSize(new Dimension(200, 40));

        //add listener
        play.addActionListener(menuListener);
        random.addActionListener(menuListener);
        configure.addActionListener(menuListener);
        stop.addActionListener(menuListener);

        //add buttons to JPanel menuButton
        menuButton.add(configure);
        menuButton.add(random);
        menuButton.add(play);
        menuButton.add(stop);

        stop.setEnabled(false);

        //decoration JLabel screen
        panScreen.add(screen);
        panScreen.setBackground(Color.blue);
        panScreen.setBorder(new javax.swing.border.BevelBorder(BevelBorder.RAISED));
        Border borderLine = BorderFactory.createLineBorder(Color.BLACK);
        panScreen.setBorder(borderLine);

        //test jpanel
        lightJpanel.setBackground(Color.CYAN);
        menuButton.setBackground(Color.black);
        container.setBackground(Color.green);

        //positioning different JPanel to main JPanel
        container.add(panScreen);
        container.add(menuButton);
        container.add(lightJpanel);
    }

这是我的 Light 类:

    import java.awt.Color;

    import javax.swing.JButton;

public class Light extends JButton{

    /**
     * coordonates of the light according to the GridLayout
     */
    private int x;
    /**
     * coordonates of the light according to the GridLayout
     */
    private int y;
    /**
     * define if the light is lit or not
     */
    private boolean lit;

    /**
     * construct a light
     * @param abs the abscissa of the light
     * @param ord the ordered of the light
     */
    public Light(int abs, int ord){
        super();
        this.x = abs;
        this.y = ord;
        // Default color
        this.setBackground(Color.gray);
        this.lit = false;
    }

    public boolean getLit(){
        return lit;
    }

    public int getX(){
        return this.x;
    }
    public int getY(){
        return this.y;
    }

    /**
     * turn on the light
     */
    public void setOn(){
        this.setBackground(Color.green);
        this.lit = true;
    }
    /**
     * turn off the light
     */
    public void setOff(){
        this.setBackground(Color.gray);
        this.lit = false;
    }   

    public void changeState(){
        if(lit)
            setOff();
        else 
            setOn();
    }
}

最佳答案

没有竞争的例子,很难说;但您可以将您的方法与显示的方法进行比较 here ,如下图所示。特别是,

  • 验证 Light 是否具有预期的首选大小和位置。

  • 当您确实想要覆盖 getPreferredSize() 时,请勿使用 setPreferredSize(),如图 here .

image

关于Java,网格布局问题。难以放置元件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23405006/

相关文章:

java - 如何在 LibGDX 中压入和弹出矩阵堆栈

java - FirebaseRecylerOptions 的实现是什么(不是 FirebaseRecylerAdapter)

java - 为什么 Java Swing 在不同的系统上表现不同?

java - Swing 不遵守我的 GridLayout Rows x Columns 定义。怎么修?

java - JEdi​​torPane,或任何java文本区域填充JFrame?

Java boolean 返回错误

java - JTable 输入键

java - JCombobox键盘输入速度

swing - 从当前的 LookAndFeel 中查找 Icon

java - 如何通过百分比设置JButton宽度?