How to change the dimension of a component in a JFrame(如何更改 JFrame 中组件的尺寸)
问题描述
假设我在 JFrame 中有一个 JPanel.当我调用更改该 JPanel 的首选大小的方法时,它不会更改.
Suppose I have a JPanel in a JFrame. When I invoke a method that changes the preferred size of that JPanel, it does not change.
代码如下所示:
public class SomePanel extends JPanel{
public SomePanel(){
setPreferredSize( new Dimension( 390, 40 ) );
setBackground( Color.BLACK );
}
public void expand(){
setPreferredSize( new Dimension( 390, 200 ) );
}
public static void main( String args[] ){
JFrame frame = new JFrame();
frame.setSize( 450, 500 );
frame.setLayout( new FlowLayout() );
SomePanel somePanel = new SomePanel();
frame.add( somePanel );
frame.setVisible( true );
somePanel.expand();
}
}
有什么我必须先做的吗?我试过在调用 expand() 时检查 JPanel 的大小.JPanel 在设置首选大小前后的高度保持在 40.
Is there something that I have to do first? I have tried so check the size of the JPanel when expand() is invoked. The height of the JPanel before and after setting the preferred size remains at 40.
我也尝试过使用 Dimension 变量,但也没有用.
I have also tried to use a Dimension variable, and that did not work either.
Dimension dimension;
public SomePanel(){
dimension = new Dimension( 390, 40 );
...
}
public expand(){
dimension.setSize( 390, 200 );
setPreferredSize( dimension );
}
推荐答案
您需要invalidate
容器层次结构以使其重新布局组件.
You need to invalidate
the container hierarchy to make it re-layout the components.
只需在您更改的组件上调用 invalidate
,然后调用 revalidate
.
Simply call invalidate
followed by revalidate
on the component you have changed.
这是一个小例子……
public class TestComponentHierarcy {
public static void main(String[] args) {
new TestComponentHierarcy();
}
public TestComponentHierarcy() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
frame.add(new Test());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class Test extends JPanel {
private Dimension size = new Dimension(10, 10);
public Test() {
setLayout(new GridBagLayout());
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
size.width += 10;
size.height += 10;
invalidate();
revalidate();
}
});
}
@Override
public Dimension getPreferredSize() {
return size;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
}
}
}
这篇关于如何更改 JFrame 中组件的尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何更改 JFrame 中组件的尺寸
基础教程推荐
- 在螺旋中写一个字符串 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01