开发者

Can't get to know the size of a standard JButton

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.


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() );


Try nextButton.setPreferredSize(new Dimension(150,100)).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜