【发布时间】:2019-11-16 19:45:53
【问题描述】:
所以我有一个带有 2 个侧面的 splitPane,其中包含 2 个面板。其中一个面板是经过检查的(分成正方形),但它周围有意想不到的边距。这是我的意思http://prntscr.com/pxwfsk 的屏幕截图。我怎样才能摆脱这些边距,因为对于我的程序来说它是至关重要的。预先感谢。下面是负责创建 splitPane 和 JPanels 的部分代码:
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;
}
【问题讨论】:
标签: java swing jpanel jsplitpane