建立了一个窗口 并在里面建立了多个控件 设置了布局管理器 当窗口发生改变时 如何让布局管理器更新 让控件适应窗口的尺寸 下面是我做的一个例子 import javax.swing.*;
import java.awt.*; public class Test extends JFrame { public static void main(String[] args) {
Test test = new Test();
JPanel jp = new JPanel();
GridLayout gridlayout = new GridLayout(5, 6); jp.setLayout(gridlayout); test.setSize(300, 300);
test.setLocation(300, 300);
test.setContentPane(jp); for (int i = 0; i < 30; i++) {
JButton d = new JButton("" + i);
jp.add(d);
}
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test.setVisible(true); try {
Thread.sleep(3000);
} catch (InterruptedException e) { } test.setSize(6*60, 5*50);
test.repaint(); }
} 当窗口发生改变后 布局管理器没有更新
当手动通过鼠标放大或缩小窗口尺寸时 布局管理器 “更新”可以正常显示各控件 求能让布局管理器 “更新”的语句 我查了帮助文档 不过都没找到
可能是找错地方了..应该去哪个类查找 谢谢
import java.awt.*; public class Test extends JFrame { public static void main(String[] args) {
Test test = new Test();
JPanel jp = new JPanel();
GridLayout gridlayout = new GridLayout(5, 6); jp.setLayout(gridlayout); test.setSize(300, 300);
test.setLocation(300, 300);
test.setContentPane(jp); for (int i = 0; i < 30; i++) {
JButton d = new JButton("" + i);
jp.add(d);
}
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test.setVisible(true); try {
Thread.sleep(3000);
} catch (InterruptedException e) { } test.setSize(6*60, 5*50);
test.repaint(); }
} 当窗口发生改变后 布局管理器没有更新
当手动通过鼠标放大或缩小窗口尺寸时 布局管理器 “更新”可以正常显示各控件 求能让布局管理器 “更新”的语句 我查了帮助文档 不过都没找到
可能是找错地方了..应该去哪个类查找 谢谢
先把窗口 隐藏 再让其显示 面板刷新 test.setVisible(false/true);不过 我想知道下 setVisible 的工作过程 是如何的知道的请留言 谢谢
public void setVisible(boolean aFlag) {
if(aFlag != isVisible()) {
super.setVisible(aFlag);
Container parent = getParent();
if(parent != null) {
Rectangle r = getBounds();
parent.repaint(r.x,r.y,r.width,r.height);
}
// Some (all should) LayoutManagers do not consider components
// that are not visible. As such we need to revalidate when the
// visible bit changes.
revalidate();
}
}
他调用了revalidate()方法,从组件树的根部开始对需要重新布局的组件进行布局