所以我有一些難以得到這個工作。 我正在創建一個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