无法知道标准 JButton 的大小
在一个窗口中,我将 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 语句的结果,我得到全零,并且有明显的变化按钮在第一个窗口中的大小,但在第二个窗口中仍然没有。
In one window I added a JButton to a Panel within a BorderLayout. In other window, using the same fashion, I added a JButton to one of the "cells" of a TableLayout. The first button is of the standard size and the second button is not. 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. Unfortunately, running this code:
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());
(tried on both buttons) gave the following output:
- 0
- 0
- 150
- 100
Moreover, the setSize method had no visible effect at all. Do have any idea what's wrong here?
--EDIT--
revalidate() method does not work
--EDIT 2--nextButton.setPreferredSize(new Dimension(150,100));
results in even more mess: for both windows I got all zeros as the result of the System.out.println statements and there is a visible change of the button's size in the first window, but still nothing for the second one.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试nextButton.setPreferredSize(new Dimension(150,100))。
Try
nextButton.setPreferredSize(new Dimension(150,100))
.