如何使 JPanel 在 SplitPane 中占据 100% 的宽度和高度
How to make JPanel take 100% width and height inside SplitPane
所以我有一个包含 2 个面板的 2 个面的 splitPane。其中一个面板是经过检查的面板(分成正方形),但它周围有意想不到的边距。这是我的意思 http://prntscr.com/pxwfsk 的截图。我怎样才能摆脱这些 margins ,因为对于我的程序来说这是至关重要的。预先感谢。这是负责创建 splitPane 和 JPanel 的代码的一部分:
public static void workingFrame() throws InterruptedException {
String frameName = "Bot World";
World world = new World(); // creating right side with world for bots
WorkFrame workF = new WorkFrame(0, 0, frameName);
wfFrame = workF.newFrame();
wfFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
wfFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JSplitPane splitPane = new JSplitPane();
splitPane.setSize(width, height);
// splitPane.setDividerSize(1);
// splitPane.setDividerLocation(149);
splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
JPanel panelLeft = createLftPanel();
JPanel panelRight = world.createRightPanel();
splitPane.setLeftComponent(panelLeft);
splitPane.setRightComponent(panelRight);
wfFrame.add(splitPane);
wfFrame.revalidate();
wfFrame.setVisible(true);
// WE NEED THIS HERE because otherwise PC is not fast enough to establish all the JPanles inside the main panel
// and thus later on we cannot use method GetComponent();
Thread.sleep(1000);
// create bots on random location on panels
for (int i = 0; i < 1; i++) {
world.createBots();
// add counter to start counting bots on a map
}
for (int i = 0; i < 1; i++) {
// create food in this world
world.createFood();
}
wfFrame.revalidate();
while (world.bots.size() > 0) {
for (Bot bot : world.bots) {
if (bot.isAlive) {
bot.seePathInDirection(panelRight);
Thread.sleep(500);
wfFrame.revalidate();
}
Thread.sleep(500);
wfFrame.revalidate();
}
}
}
}
和 createRightPanel 方法:
public static JPanel createRightPanel() {
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(ROWS, COLS));
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
JPanel pane = new JPanel();
pane.setBackground(Color.WHITE);
pane.setBorder(BorderFactory.createLineBorder(Color.black));
panel.add(pane);
}
}
botWorld = panel;
return panel;
}
您的代码中有一些 "problems"。首先,不要使用组件的 setSize
(或 setPreferredSize
)方法。让布局管理器计算它的大小和位置。
不要在 Event Dispatch Thread. It will freeze the whole GUI. Since the thread sleeps, events cannot take place. Consider using a Swing Timer or a Swing Worker 中使用 Thread.sleep()
。
为什么会遇到这个问题?
因为网格布局会为其所有组件提供相同的大小。其实有点难理解,所以我会尝试用例子来解释它。
考虑一个网格布局的面板,只有 1 行和 10 列。您尝试向其中添加 10 个组件。此面板的宽度等于 100。网格布局将为每个组件提供 10 宽度,因此 10x10 = 100.
现在,假设这个面板的宽度为 102。gridlayout 将如何平均分配它?它不能。所以它会让左边有 1 个像素为空,右边有 1 个像素。这正是你所面对的。由于 GridLayout 不能平均共享宽度(和高度),它会让它为空。如果你把 window 变大,一会儿 space 就足以让他们平等地适应:
看这个.gif:
当width等于795-801时,网格布局无法将space均分到一行的20个组件。当它变为 802 时,让您烦恼的余量就会消失。这是因为行中有 20 个组件 / 800 = 每个组件的宽度为 40。 +2 左右边框(绿色的)。
产生此行为的代码:
public class Example extends JFrame implements ComponentListener {
private static final int ROWS = 20;
private static final int COLUMNS = 20;
private JLabel widthLabel;
private JPanel greenPanel;
public Example() {
super("test");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JPanel redPanel = new JPanel();
redPanel.setBorder(BorderFactory.createLineBorder(Color.RED, 1));
widthLabel = new JLabel();
redPanel.add(widthLabel);
greenPanel = new JPanel(new GridLayout(ROWS, COLUMNS));
greenPanel.setBorder(BorderFactory.createLineBorder(Color.GREEN, 1));
greenPanel.addComponentListener(this);
for (int i = 0; i < ROWS * COLUMNS; i++) {
JPanel panel = new JPanel(new BorderLayout());
panel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
greenPanel.add(panel);
}
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, redPanel, greenPanel);
add(splitPane);
setLocationByPlatform(true);
pack();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
new Example().setVisible(true);
});
}
@Override
public void componentResized(ComponentEvent e) {
widthLabel.setText("Green panel's width: " + greenPanel.getWidth());
}
@Override
public void componentMoved(ComponentEvent e) {
}
@Override
public void componentShown(ComponentEvent e) {
}
@Override
public void componentHidden(ComponentEvent e) {
}
}
并回答您的评论,不。网格布局中的组件类型无关紧要。不管有没有JPanels,它都不起作用。
所以我有一个包含 2 个面板的 2 个面的 splitPane。其中一个面板是经过检查的面板(分成正方形),但它周围有意想不到的边距。这是我的意思 http://prntscr.com/pxwfsk 的截图。我怎样才能摆脱这些 margins ,因为对于我的程序来说这是至关重要的。预先感谢。这是负责创建 splitPane 和 JPanel 的代码的一部分:
public static void workingFrame() throws InterruptedException {
String frameName = "Bot World";
World world = new World(); // creating right side with world for bots
WorkFrame workF = new WorkFrame(0, 0, frameName);
wfFrame = workF.newFrame();
wfFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
wfFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JSplitPane splitPane = new JSplitPane();
splitPane.setSize(width, height);
// splitPane.setDividerSize(1);
// splitPane.setDividerLocation(149);
splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
JPanel panelLeft = createLftPanel();
JPanel panelRight = world.createRightPanel();
splitPane.setLeftComponent(panelLeft);
splitPane.setRightComponent(panelRight);
wfFrame.add(splitPane);
wfFrame.revalidate();
wfFrame.setVisible(true);
// WE NEED THIS HERE because otherwise PC is not fast enough to establish all the JPanles inside the main panel
// and thus later on we cannot use method GetComponent();
Thread.sleep(1000);
// create bots on random location on panels
for (int i = 0; i < 1; i++) {
world.createBots();
// add counter to start counting bots on a map
}
for (int i = 0; i < 1; i++) {
// create food in this world
world.createFood();
}
wfFrame.revalidate();
while (world.bots.size() > 0) {
for (Bot bot : world.bots) {
if (bot.isAlive) {
bot.seePathInDirection(panelRight);
Thread.sleep(500);
wfFrame.revalidate();
}
Thread.sleep(500);
wfFrame.revalidate();
}
}
}
}
和 createRightPanel 方法:
public static JPanel createRightPanel() {
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(ROWS, COLS));
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
JPanel pane = new JPanel();
pane.setBackground(Color.WHITE);
pane.setBorder(BorderFactory.createLineBorder(Color.black));
panel.add(pane);
}
}
botWorld = panel;
return panel;
}
您的代码中有一些 "problems"。首先,不要使用组件的 setSize
(或 setPreferredSize
)方法。让布局管理器计算它的大小和位置。
不要在 Event Dispatch Thread. It will freeze the whole GUI. Since the thread sleeps, events cannot take place. Consider using a Swing Timer or a Swing Worker 中使用 Thread.sleep()
。
为什么会遇到这个问题?
因为网格布局会为其所有组件提供相同的大小。其实有点难理解,所以我会尝试用例子来解释它。
考虑一个网格布局的面板,只有 1 行和 10 列。您尝试向其中添加 10 个组件。此面板的宽度等于 100。网格布局将为每个组件提供 10 宽度,因此 10x10 = 100.
现在,假设这个面板的宽度为 102。gridlayout 将如何平均分配它?它不能。所以它会让左边有 1 个像素为空,右边有 1 个像素。这正是你所面对的。由于 GridLayout 不能平均共享宽度(和高度),它会让它为空。如果你把 window 变大,一会儿 space 就足以让他们平等地适应:
看这个.gif:
当width等于795-801时,网格布局无法将space均分到一行的20个组件。当它变为 802 时,让您烦恼的余量就会消失。这是因为行中有 20 个组件 / 800 = 每个组件的宽度为 40。 +2 左右边框(绿色的)。
产生此行为的代码:
public class Example extends JFrame implements ComponentListener {
private static final int ROWS = 20;
private static final int COLUMNS = 20;
private JLabel widthLabel;
private JPanel greenPanel;
public Example() {
super("test");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JPanel redPanel = new JPanel();
redPanel.setBorder(BorderFactory.createLineBorder(Color.RED, 1));
widthLabel = new JLabel();
redPanel.add(widthLabel);
greenPanel = new JPanel(new GridLayout(ROWS, COLUMNS));
greenPanel.setBorder(BorderFactory.createLineBorder(Color.GREEN, 1));
greenPanel.addComponentListener(this);
for (int i = 0; i < ROWS * COLUMNS; i++) {
JPanel panel = new JPanel(new BorderLayout());
panel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
greenPanel.add(panel);
}
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, redPanel, greenPanel);
add(splitPane);
setLocationByPlatform(true);
pack();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
new Example().setVisible(true);
});
}
@Override
public void componentResized(ComponentEvent e) {
widthLabel.setText("Green panel's width: " + greenPanel.getWidth());
}
@Override
public void componentMoved(ComponentEvent e) {
}
@Override
public void componentShown(ComponentEvent e) {
}
@Override
public void componentHidden(ComponentEvent e) {
}
}
并回答您的评论,不。网格布局中的组件类型无关紧要。不管有没有JPanels,它都不起作用。