面板填充JavaSwing中的窗口

brc7rcf0  于 2021-06-29  发布在  Java
关注(0)|答案(2)|浏览(363)

我在一个窗口中添加了两个10x10的网格,但我无法使它们以较小的尺寸显示。它们只是填满了整个Windows。我该如何把它们整齐地放在中间一个较小的尺寸,以便有一些标签和按钮的空间?我把代码贴在下面以供参考。

public static void main(String[] args) {
    window = new JFrame();
    window.setTitle("Battleship.exe");
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setPreferredSize(new Dimension(600, 600));

    P1_container = new JPanel(new GridLayout(10,10));
    P1_container.setBorder(BorderFactory.createLineBorder(Color.black, 5));

    compContainer = new JPanel(new GridLayout(10,10));
    compContainer.setBorder(BorderFactory.createLineBorder(Color.black, 5));

    grid = new JPanel[10][10];
    for (int i =0; i< 10; i++) {
        for (int j =0; j< 10; j++) {
            grid[i][j] = new JPanel();
            grid[i][j].setBackground(Color.white);
            grid[i][j].setBorder(BorderFactory.createLineBorder(Color.blue, 2));
            grid[i][j].setPreferredSize(new Dimension(25,25));
            P1_container.add(grid[i][j]);
        }
    }

    enemyGrid = new JPanel[10][10];
    for (int i =0; i< 10; i++) {
        for (int j =0; j< 10; j++) {
            enemyGrid[i][j] = new JPanel();
            enemyGrid[i][j].setBackground(Color.white);
            enemyGrid[i][j].setBorder(BorderFactory.createLineBorder(Color.red, 2));
            enemyGrid[i][j].setPreferredSize(new Dimension(25, 25));
            compContainer.add(enemyGrid[i][j]);
        }
    }

    GridLayout layout = new GridLayout(1, 2);
    layout.setHgap(150);
    mainPanel = new JPanel(layout); 

    mainPanel.add(P1_container);
    mainPanel.add(compContainer);

    window.add(mainPanel);
    window.pack();
    window.setVisible(true);
}
e0bqpujr

e0bqpujr1#

你的代码有24个编译错误。一旦我修复了编译错误,我修改了一行并注解掉了一行以获得这个gui。

下面是完整的可运行代码。

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class BattleshipGUI {

    public static void main(String[] args) {
        JFrame window = new JFrame();
        window.setTitle("Battleship.exe");
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//      window.setPreferredSize(new Dimension(600, 600));

        JPanel P1_container = new JPanel(new GridLayout(10,10));
        P1_container.setBorder(BorderFactory.createLineBorder(Color.black, 5));

        JPanel compContainer = new JPanel(new GridLayout(10,10));
        compContainer.setBorder(BorderFactory.createLineBorder(Color.black, 5));

        JPanel[][] grid = new JPanel[10][10];
        for (int i =0; i< 10; i++) {
            for (int j =0; j< 10; j++) {
                grid[i][j] = new JPanel();
                grid[i][j].setBackground(Color.white);
                grid[i][j].setBorder(BorderFactory.createLineBorder(Color.blue, 2));
                grid[i][j].setPreferredSize(new Dimension(25,25));
                P1_container.add(grid[i][j]);
            }
        }

        JPanel[][] enemyGrid = new JPanel[10][10];
        for (int i =0; i< 10; i++) {
            for (int j =0; j< 10; j++) {
                enemyGrid[i][j] = new JPanel();
                enemyGrid[i][j].setBackground(Color.white);
                enemyGrid[i][j].setBorder(BorderFactory.createLineBorder(Color.red, 2));
                enemyGrid[i][j].setPreferredSize(new Dimension(25, 25));
                compContainer.add(enemyGrid[i][j]);
            }
        }

        GridLayout layout = new GridLayout(0, 2);
        layout.setHgap(150);
        JPanel mainPanel = new JPanel(layout); 

        mainPanel.add(P1_container);
        mainPanel.add(compContainer);

        window.add(mainPanel);
        window.pack();
        window.setVisible(true);
    }

}
ryevplcw

ryevplcw2#

使用swing布局管理器的关键是嵌套容器。
将mainpanel嵌套到另一个jpanel中,该jpanel使用一个布局来帮助您实现目标,例如将mainpanel放置在borderlayout.center位置的borderlayout。然后在其他borderlayout位置将其他gui组件添加到这个相同的外部jpanel中。
如果不希望主面板展开,请使用其他不展开的外部布局,如flowlayout。

相关问题