所以我有一些难以得到这个工作。 我正在创建一个JFrame并使用BorderLayout.CENTER向它的中心添加一个JPanel。在创建它的过程中,一切都非常有效,但每当我现在通过执行以下操作重新调整窗口大小时,JPanel内部不会自动更新以延伸到窗口。我试图从JPanel的大小获取信息,但由于JPanel的大小在窗口更改后不会更新,所以JPanel的大小保持不变。设置JFrame大小后,JPanel大小不会更新
这里是我的代码(什么我谈论):(updateMinHeightAndWidth()和getMinHeightAndWidth()几乎是相同的,但要分开需要)
(listenLabel)
public class listenPanel extends JPanel {
public void redoWindowSetup() {
window.setSize(new Dimension(600,600));
window.repaint(); //Where image is shown
JPanel tempJPanel;
listenButton listenButton;
for (temporaryIncrimentVariable = 0; temporaryIncrimentVariable < list.getRows() * list.getColumns(); temporaryIncrimentVariable++) {
tempJPanel = new JPanel();
tempJPanel.setLayout(new BorderLayout());
tempJPanel.setOpaque(false);
listenButton = new listenButton();
tempJPanel.add(listenButton, BorderLayout.CENTER);
listenLabel timerLabel = new listenLabel();
tempJPanel.add(timerLabel, BorderLayout.NORTH);
centerPanel.add(tempJPanel);
}
window.add(centerPanel,BorderLayout.CENTER);
int[] updatedMinHeightAndWidth = updateMinHeightAndWidth();
window.setMinimumSize(new Dimension(updatedMinHeightAndWidth[1], updatedMinHeightAndWidth[0]));
window.setVisible(true);
window.repaint();
}
private int[] updateMinHeightAndWidth() {
int tempVerticalSize;
window.pack();
window.setVisible(true);
int upperBound = 40000;
int lowerBound = 0;
try {
window.setSize(new Dimension(40, (upperBound + lowerBound)/2)); //Set window size to halfway in the middle
Thread.sleep(200);
} catch (InterruptedException e) {
}
while (centerPanel.getSize().height != (MAX_HEIGHT * list.getRows()) && ((lowerBound + upperBound)/2) != lowerBound) {
centerPanel.repaint();
System.out.println(centerPanel.getSize().height);
if (centerPanel.getSize().height > (MAX_HEIGHT * list.getRows())) {
upperBound = (upperBound + lowerBound)/2;
} else {
lowerBound = (upperBound + lowerBound)/2;
}
try {
window.setSize(new Dimension(40, (upperBound + lowerBound)/2)); //Set window size to halfway in the middle
Thread.sleep(200);
} catch (InterruptedException e) {
}
}
tempVerticalSize = upperBound;
upperBound = 40000;
lowerBound = 0;
try {
window.setSize(new Dimension((upperBound + lowerBound)/2, 40)); //Set window size to halfway in the middle
Thread.sleep(200);
} catch (InterruptedException e) {
}
while (centerPanel.getSize().width != (MAX_WIDTH * list.getColumns()) && ((lowerBound + upperBound)/2) != lowerBound) {
if (centerPanel.getSize().width > (MAX_WIDTH * list.getColumns())) {
upperBound = (upperBound + lowerBound)/2;
} else {
lowerBound = (upperBound + lowerBound)/2;
}
try {
window.setSize(new Dimension((upperBound + lowerBound)/2, 40)); //Set window size to halfway in the middle
Thread.sleep(200);
} catch (InterruptedException e) {
}
}
return new int[]{tempVerticalSize, upperBound};
}
}
我的构造
public TestingCenterWindow() {
list = new ComputerList();
window = new JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setResizable(true);
window.setSize(500, 500);
window.setTitle("Testing Center Computers");
window.setLayout(new BorderLayout());
centerPanel = new listenPanel();
centerPanel.setBackground(Color.BLUE);
centerPanel.setLayout(new GridLayout(list.getRows(), list.getColumns()));
JPanel tempJPanel;
listenButton listenButton;
for (temporaryIncrimentVariable = 0; temporaryIncrimentVariable < list.getRows() * list.getColumns(); temporaryIncrimentVariable++) {
tempJPanel = new JPanel();
tempJPanel.setLayout(new BorderLayout());
tempJPanel.setOpaque(false);
listenButton = new listenButton();
tempJPanel.add(listenButton, BorderLayout.CENTER);
listenLabel timerLabel = new listenLabel();
tempJPanel.add(timerLabel, BorderLayout.NORTH);
centerPanel.add(tempJPanel);
}
window.add(centerPanel, BorderLayout.CENTER);
window.setJMenuBar(computerMenuBar);
int[] minHeightAndWidth = getMinHeightAndWidth();
window.setMinimumSize(new Dimension(minHeightAndWidth[1], minHeightAndWidth[0]));
window.setVisible(true);
}
private int[] getMinHeightAndWidth() {
int tempVerticalSize;
window.pack();
window.setVisible(false);
int upperBound = 40000;
int lowerBound = 0;
try {
window.setSize(new Dimension(40, (upperBound + lowerBound)/2)); //Set window size to halfway in the middle
Thread.sleep(200);
} catch (InterruptedException e) {
}
while (centerPanel.getSize().height != (MAX_HEIGHT * list.getRows()) && ((lowerBound + upperBound)/2) != lowerBound) {
if (centerPanel.getSize().height > (MAX_HEIGHT * list.getRows())) {
upperBound = (upperBound + lowerBound)/2;
} else {
lowerBound = (upperBound + lowerBound)/2;
}
try {
window.setSize(new Dimension(40, (upperBound + lowerBound)/2)); //Set window size to halfway in the middle
Thread.sleep(200);
} catch (InterruptedException e) {
}
}
tempVerticalSize = upperBound;
upperBound = 40000;
lowerBound = 0;
try {
window.setSize(new Dimension((upperBound + lowerBound)/2, 40)); //Set window size to halfway in the middle
Thread.sleep(200);
} catch (InterruptedException e) {
}
while (centerPanel.getSize().width != (MAX_WIDTH * list.getColumns()) && ((lowerBound + upperBound)/2) != lowerBound) {
if (centerPanel.getSize().width > (MAX_WIDTH * list.getColumns())) {
upperBound = (upperBound + lowerBound)/2;
} else {
lowerBound = (upperBound + lowerBound)/2;
}
try {
window.setSize(new Dimension((upperBound + lowerBound)/2, 40)); //Set window size to halfway in the middle
Thread.sleep(200);
} catch (InterruptedException e) {
}
}
return new int[]{tempVerticalSize, upperBound};
}
所以每当我试图让我第一次创建我的窗口最小尺寸,它完美的罚款。当窗口改变大小时,JPanel也会改变,所以它可以在窗口改变后获得JPanel的大小。每当我为我的listenLabel做同样的代码,redoWindowSetup()被调用后,窗口做到这一点:
http://i.stack.imgur.com/BG6x3.png
蓝色区域就是常做伸展,但不现在,所以当窗口改变大小时,JPanel的大小保持在一个单一的值。我曾尝试在窗口和centerPanel上使用paintAll和repaint,但似乎无法使其工作。
你已经发布了大量的代码,其中大部分与你的问题完全无关,因此可能会分散注意力。如果您没有尽快获得帮助,请考虑创建并发布一个[最小,完整和可验证的示例程序](http://stackoverflow.com/help/mcve),您可以将代码压缩到仍然编译的最小位,运行,没有外部依赖性(如需要链接到数据库或图像),没有额外的代码与您的问题无关,但仍显示您的问题。祝你好运。 – 2015-02-08 19:07:36
另外,在Swing程序中使用Thread.sleep(...)是一件很危险的事情,如果没有这个,很可能你的代码会好得多。 – 2015-02-08 19:08:15