设置 JFrame 大小后 JPanel 大小不会更新
JPanel size wont update after setting JFrame size
所以我在让它工作时遇到了一些困难。
我正在创建一个 JFrame 并使用 BorderLayout.CENTER 添加一个 JPanel 到它的中心。就创建它而言,一切都非常有效,但每当我现在通过执行以下操作调整 window 的大小时:window.setSize(new Dimension(600,600));
,内部的 JPanel 不会自动更新以拉伸到 window。我正在尝试从 JPanel 的大小获取信息,但由于 JPanel 的大小不会在 window 更改后立即更新,因此 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};
}
因此,每当我在首次创建 window 时尝试获得最小尺寸时,它都可以正常工作。当 window 改变大小时,JPanel 也会改变大小,因此它可以在 window 改变后获取 JPanel 的大小。每当我为我的 listenLabel 执行完全相同的代码时,在调用 redoWindowSetup() 之后,window 会这样做:
http://i.stack.imgur.com/BG6x3.png
蓝色区域通常会拉伸,但现在不会拉伸,因此随着 window 更改大小,JPanel 的大小保持在一个值。我已经尝试在 window 和 centerPanel 上使用 paintAll 和 repaint,但我似乎无法让它工作。
我发现了如何解决这个问题。这仅仅是因为在设置 window 大小后我需要一个 JFrame.revalidate() 语句。这手动强制面板尺寸更新。
所以我在让它工作时遇到了一些困难。
我正在创建一个 JFrame 并使用 BorderLayout.CENTER 添加一个 JPanel 到它的中心。就创建它而言,一切都非常有效,但每当我现在通过执行以下操作调整 window 的大小时:window.setSize(new Dimension(600,600));
,内部的 JPanel 不会自动更新以拉伸到 window。我正在尝试从 JPanel 的大小获取信息,但由于 JPanel 的大小不会在 window 更改后立即更新,因此 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};
}
因此,每当我在首次创建 window 时尝试获得最小尺寸时,它都可以正常工作。当 window 改变大小时,JPanel 也会改变大小,因此它可以在 window 改变后获取 JPanel 的大小。每当我为我的 listenLabel 执行完全相同的代码时,在调用 redoWindowSetup() 之后,window 会这样做:
http://i.stack.imgur.com/BG6x3.png
蓝色区域通常会拉伸,但现在不会拉伸,因此随着 window 更改大小,JPanel 的大小保持在一个值。我已经尝试在 window 和 centerPanel 上使用 paintAll 和 repaint,但我似乎无法让它工作。
我发现了如何解决这个问题。这仅仅是因为在设置 window 大小后我需要一个 JFrame.revalidate() 语句。这手动强制面板尺寸更新。