如何在JFrame中调整这些面板的大小?

时间:2014-09-06 08:28:28

标签: java swing awt layout-manager border-layout

我创建了一个国际象棋游戏,其中主面板使用BorderLayout,在NORTH有一个面板用于按钮,一个面板在CENTER用于棋盘本身(设置为GridLayout)以及East的侧边栏。

我让JFrame无法访问,我想让棋盘适合面板,以便East面板宽得多(可能是200像素)而board仍然是广场。我无法弄清楚如何单独更改这些组件的大小。

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;

public class GameWindow extends JFrame {

    private final JPanel playArea = new JPanel(new BorderLayout(3,3));
    private final JButton[][] boardSquares = new JButton[8][8];
    private final JPanel board;
    private final JPanel sidebar = new JPanel();
    private final JLabel message = new JLabel("Game by ...");

    public  GameWindow() {
        playArea.setBorder(new EmptyBorder(5, 5, 5, 5));

        JToolBar tools = new JToolBar();


        tools.setFloatable(false);
        playArea.add(tools, BorderLayout.PAGE_START);
        tools.add(new JButton("New Game"));
        tools.add(new JButton("Save"));
        tools.add(new JButton("Restore")); 
        tools.addSeparator();
        tools.add(new JButton("Resign"))
        tools.addSeparator();
        tools.add(message);

        board = new JPanel(new GridLayout(0, 8));
        board.setBorder(new LineBorder(Color.BLACK));
        playArea.add(board, BorderLayout.CENTER);
        playArea.add(sidebar, BorderLayout.EAST);

        Insets buttonMargin = new Insets(0,0,0,0);
        for (int i = 0; i < boardSquares.length; i++) {
           for (int j = 0; j < boardSquares[i].length; j++) {
                JButton square = new JButton();
                square.setMargin(buttonMargin);
                if ((i+j)%2 == 0) {
                    square.setBackground(Color.WHITE);
                } 
                else {
                    square.setBackground(Color.BLACK);
                }
                board.setSize(600, 600);
                board.add(boardSquares[j][i] = square);
            }
        }
    }

    public final JComponent getChessBoard() {
    return board;
    }

    public final JComponent getGui() {
        return playArea;
    }

    public static void main(String[] args) {

        GameWindow window = new GameWindow();

        JFrame frame = new JFrame("Checkers");
        frame.setResizable(false);
        frame.add(window.getGui());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationByPlatform(true);
        frame.setSize(800, 800);

        frame.setVisible(true);
    }
}

2 个答案:

答案 0 :(得分:4)

首先,从JDK 1.4开始,Java鼓励将BorderLayout常量用作BorderLayout.PAGE_STARTBorderLayout.LINE_STARTBorderLayout.CENTERBorderLayout.LINE_END和{{1}超过你使用的后者。

其次,您可以简单地覆盖所述BorderLayout.PAGE_END的{​​{3}},以便它给出一些您认为适合您的用例的大小。 JPanel的使用受到限制,因为并非所有LayoutManage都使用它来遵守它指定的维度。

因此你可以这样做:

setPreferredSize()

您可以尝试修改此代码:

private final JPanel sidebar = new JPanel() {
    @Override
    public Dimension getPreferredSize() {
        return new Dimension(100, 100);
    }
};

此外,在设置import java.awt.*; import javax.swing.*; import javax.swing.border.*; public class GameWindow extends JFrame { private final JPanel playArea = new CustomPanel(710, 710); private final JButton[][] boardSquares = new JButton[8][8]; private final JPanel board; private final JPanel sidebar = new CustomPanel(100, 100); private final JLabel message = new JLabel("Game by ..."); public GameWindow() { playArea.setLayout(new BorderLayout(3,3)); playArea.setBorder(new EmptyBorder(5, 5, 5, 5)); JToolBar tools = new JToolBar(); tools.setFloatable(false); playArea.add(tools, BorderLayout.PAGE_START); tools.add(new JButton("New Game")); tools.add(new JButton("Save")); tools.add(new JButton("Restore")); tools.addSeparator(); tools.add(new JButton("Resign")); tools.addSeparator(); tools.add(message); board = new CustomPanel(600, 600); board.setLayout(new GridLayout(0, 8)); board.setBorder(new LineBorder(Color.BLACK)); playArea.add(board, BorderLayout.CENTER); playArea.add(sidebar, BorderLayout.LINE_START); Insets buttonMargin = new Insets(0,0,0,0); for (int i = 0; i < boardSquares.length; i++) { for (int j = 0; j < boardSquares[i].length; j++) { JButton square = new JButton(); square.setOpaque(true); square.setMargin(buttonMargin); if ((i+j)%2 == 0) { square.setBackground(Color.WHITE); } else { square.setBackground(Color.BLACK); } board.add(boardSquares[j][i] = square); } } } private class CustomPanel extends JPanel { private int width; private int height; public CustomPanel(int width, int height) { this.width = width; this.height = height; setOpaque(true); } @Override public Dimension getPreferredSize() { return new Dimension(width, height); } } public final JComponent getChessBoard() { return board; } public final JComponent getGui() { return playArea; } public static void main(String[] args) { GameWindow window = new GameWindow(); JFrame frame = new JFrame("Checkers"); frame.setResizable(false); frame.setContentPane(window.getGui()); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.setLocationByPlatform(true); frame.pack(); frame.setVisible(true); } } 的背景之前,最好调用JButton属性。

答案 1 :(得分:1)

设置JPanel的大小并不困难。只需致电setPreferredSize()即可。如果要调整东JPanel的大小,请致电:

sidebar.setPreferredSize(new Dimension(200, 200));

之后,您的LayoutManager会将JPanel的大小设置为200,200。

致你的其他JPanelboard:无法让Component(如JPanel)保持正方形。它们总是适合矩形。您需要创建自己的JComponent子类,并且只绘制正方形中的所有内容,并使其余部分保持透明。因此,请覆盖方法JComponent.paintComponent(Graphics)