所以我在使用它时遇到了一些困难。
我正在使用BorderLayout.CENTER创建一个JFrame并将JPanel添加到它的中心。在创建它时,一切都非常有效,但每当我通过执行:window.setSize(new Dimension(600,600));
调整窗口大小时,内部的JPanel都不会自动更新以延伸到窗口。我试图从JPanel的大小中获取信息,但由于JPanel的大小在窗口更改后没有更新,因此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的大小保持一个单一的值。我已经尝试在window和centerPanel上使用paintAll并重新绘制,但我似乎无法让它工作。
答案 0 :(得分:0)
我发现了如何解决这个问题。这只是因为我在设置窗口大小后需要一个JFrame.revalidate()语句。这会手动强制更新面板大小。