在一个窗口中,我将 JButton 添加到 BorderLayout 内的面板中。在其他窗口中,使用相同的方式,我将 JButton 添加到 TableLayout 的“单元格”之一。第一个按钮是标准尺寸,第二个按钮不是标准尺寸。所以我试图了解第一个的大小,以便我可以为另一个设置相同的大小。不幸的是,运行此代码:
nextButton = new JButton("NEXT");
System.out.println(nextButton.getWidth());
System.out.println(nextButton.getHeight());
nextButton.setSize(150, 100);
System.out.println(nextButton.getWidth());
System.out.println(nextButton.getHeight());
(尝试了两个按钮)给出了以下输出:
- 0
- 0
- 150
- 100
而且,setSize方法根本没有任何可见的效果。你知道这里出了什么问题吗?
--编辑--
revalidate() 方法不起作用
--编辑2--
nextButton.setPreferredSize(new Dimension(150,100));
导致更加困惑:对于两个窗口,作为 System.out.println 语句的结果,我得到全零,并且有明显的变化按钮在第一个窗口中的大小,但在第二个窗口中仍然没有任何内容。
最佳答案
So I was trying to get to know the size of the first one so that I could set the same size for the other one
button2.setPreferredSize( button1.getPreferredSize() );
关于java - 无法知道标准 JButton 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6837364/