Java 设置JFrame大小后,JPanel大小不会更新
所以我很难让它工作。 我正在创建一个JFrame,并使用BorderLayout.center将一个JPanel添加到它的中心。就创建它而言,一切都非常有效,但每当我现在通过执行以下操作来调整窗口的大小:Java 设置JFrame大小后,JPanel大小不会更新,java,swing,jframe,jpanel,Java,Swing,Jframe,Jpanel,所以我很难让它工作。 我正在创建一个JFrame,并使用BorderLayout.center将一个JPanel添加到它的中心。就创建它而言,一切都非常有效,但每当我现在通过执行以下操作来调整窗口的大小:window.setSize(新维度(600600)),内部的JPanel不会自动更新以延伸到窗口。我试图从JPanel的大小中获取信息,但由于JPanel的大小不会在窗口更改时立即更新,因此JPanel的大小保持不变 下面是我的代码(以及我所说的):(updateMinHeightAndWid
window.setSize(新维度(600600))代码>,内部的JPanel不会自动更新以延伸到窗口。我试图从JPanel的大小中获取信息,但由于JPanel的大小不会在窗口更改时立即更新,因此JPanel的大小保持不变
下面是我的代码(以及我所说的):(updateMinHeightAndWidth()和getMinHeightAndWidth()实际上是相同的,但需要分开)
(列表标签)
公共类listenPanel扩展了JPanel{
公共窗口设置(){
窗口设置尺寸(新尺寸(600600));
window.repaint();//显示图像的位置
JPanel tempJPanel;
listenButton listenButton;
对于(temporaryIncrimentVariable=0;temporaryIncrimentVariable(最大高度*列表.getRows())){
上限=(上限+下限)/2;
}否则{
下限=(上限+下限)/2;
}
试一试{
Set Set(新维度(40,(上界+下界)/ 2))/ /设置窗口大小到中间的一半
睡眠(200);
}捕捉(中断异常e){
}
}
tempVerticalSize=上限;
上限=40000;
lowerBound=0;
试一试{
窗口大小设置(新的维度(上界+下界)/ 2, 40);/ /设置窗口大小到中间的一半
睡眠(200);
}捕捉(中断异常e){
}
while(centerPanel.getSize().width!=(MAX_width*list.getColumns())&((下限+上限)/2)!=下限){
如果(centerPanel.getSize().width>(最大宽度*列表.getColumns())){
上限=(上限+下限)/2;
}否则{
下限=(上限+下限)/2;
}
试一试{
窗口大小设置(新的维度(上界+下界)/ 2, 40);/ /设置窗口大小到中间的一半
睡眠(200);
}捕捉(中断异常e){
}
}
返回新的int[]{tempVerticalSize,上限};
}
}
还有我的构造器
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=newjframe();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setresizeable(true);
设置窗口大小(500500);
window.setTitle(“测试中心计算机”);
setLayout(新的BorderLayout());
centerPanel=新建listenPanel();
中心面板。立根背景(颜色。蓝色);
setLayout(新的GridLayout(list.getRows(),list.getColumns());
JPanel tempJPanel;
listenButton listenButton;
对于(temporaryIncrimentVariable=0;temporaryIncrimentVariable(最大高度*列表.getRows())){
上限=(上限+下限)/2;
}否则{
下限=(上限+下限)/2;
}
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};
}