Java 如何在JFrame中调整这些面板的大小?
我正在创建一个国际象棋游戏,其中主面板使用BorderLayout,在Java 如何在JFrame中调整这些面板的大小?,java,swing,awt,layout-manager,border-layout,Java,Swing,Awt,Layout Manager,Border Layout,我正在创建一个国际象棋游戏,其中主面板使用BorderLayout,在北部有一个面板用于按钮,在中心有一个面板用于棋盘本身(设置为GridLayout),在东部有一个侧边栏 我已使JFrame不可编辑,我希望棋盘能将面板放入,以便东面板更宽(可能有200像素),而棋盘保持正方形。我不知道如何单独更改这些组件的大小 import java.awt.*; import javax.swing.*; import javax.swing.border.*; public class GameWind
北部有一个面板用于按钮,在中心有一个面板用于棋盘本身(设置为GridLayout),在东部有一个侧边栏
我已使JFrame不可编辑,我希望棋盘能将面板放入,以便东
面板更宽(可能有200像素),而棋盘保持正方形。我不知道如何单独更改这些组件的大小
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);
}
}
import java.awt.*;
导入javax.swing.*;
导入javax.swing.border.*;
公共类GameWindow扩展JFrame{
私人最终JPanel游戏区=新JPanel(新边界布局(3,3));
私有最终JButton[][]boardSquares=新JButton[8][8];
私人最终JPanel董事会;
private final JPanel侧边栏=new JPanel();
私人最终JLabel消息=新JLabel(“游戏人…”);
公共游戏窗口(){
游戏区。设置顺序(新的清空顺序(5,5,5,5));
JToolBar工具=新的JToolBar();
工具。可设置浮动(错误);
playArea.add(工具、边框布局、页面开始);
添加(新的JButton(“新游戏”));
添加(新的JButton(“保存”));
添加(新的JButton(“还原”));
tools.addSeparator();
添加(新的JButton(“辞职”))
tools.addSeparator();
工具。添加(消息);
board=新JPanel(新网格布局(0,8));
board.setBorder(新线条边框(颜色为黑色));
游戏区。添加(板、边框布局。中心);
游戏区。添加(侧栏,边框布局。东);
插图按钮边缘=新插图(0,0,0,0);
对于(int i=0;i设置JPanel的大小并不困难。只需调用setPreferredSize()
。如果要调整EastJPanel的大小,请拨打:
sidebar.setPreferredSize(new Dimension(200, 200));
之后,您的LayoutManager
将JPanel
的大小设置为200200
对于您的另一个JPanel
,板
:不可能使组件
(如JPanel
)保持正方形。它们总是适合矩形。您需要创建自己的JComponent
子类,只在正方形中绘制所有内容,其余部分保持透明。因此,覆盖方法JComponent.paintComponent(Graphics)
设置JPanel的大小并不困难。只需调用setPreferredSize()
。如果要调整EastJPanel的大小,请拨打:
sidebar.setPreferredSize(new Dimension(200, 200));
之后,您的LayoutManager
将JPanel
的大小设置为200200
对于您的另一个JPanel
,板
:不可能使组件
(如JPanel
)保持正方形。它们总是适合矩形。您需要创建自己的JComponent
子类,只在正方形中绘制所有内容,其余部分保持透明。因此,首先,从JDK 1.4开始,Java鼓励使用BorderLayout
常量作为BorderLayout.PAGE\u START
,BorderLayout.LINE\u START
,BorderLayout.CENTER
,覆盖方法JComponent.paintComponent(Graphics),BorderLayout.LINE\u END
和BorderLayout.PAGE\u END
在您使用的后者上方
其次,您可以简单地覆盖所说的JPanel
,以便它提供一些您认为适合您的用例的大小。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);
}
}
import java.awt.*;
导入javax.swing.*;
导入javax.swing.border.*;
公共类GameWindow扩展JFrame{
私人最终JPanel游戏区=新CustomPanel(710710);
私有最终JButton[][]boardSquares=新JButton[8][8];
私人最终JPanel董事会;
私有最终JPanel侧边栏=新CustomPanel(100100);
私人最终JLabel消息=新JLabel(“游戏人…”);
公共游戏窗口(){
游戏区设置布局(新边界布局(3,3));
游戏区。设置顺序(新的清空顺序(5,5,5,5));
JToolBar工具=新的JToolBar();
工具。可设置浮动(错误);
playArea.add(工具、边框布局、页面开始);
添加(新的JButton(“新游戏”));
添加(新的JButton(“保存”));
添加(新的JButton(“还原”));
tools.addSeparator();
添加(新按钮(“辞职”);
tools.addSeparator();
工具。添加(消息);
board=新定制面板(600600);
board.setLayout(新网格布局(0,8));
board.setBorder(新线条边框(颜色为黑色));
游戏区。添加(板、边框布局。中心);
添加(侧栏,边框)