有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法了解标准JButton的大小

在一个窗口中,我向BorderLayout中的面板添加了一个JButton。在另一个窗口中,使用相同的方式,我在TableLayout的一个“单元格”中添加了一个JButton。第一个按钮为标准尺寸,第二个按钮为非标准尺寸。所以我想知道第一个的尺寸,这样我就可以为另一个设定相同的尺寸。不幸的是,运行以下代码:

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
  • 一百

此外,setSize方法根本没有明显的效果。你知道这里怎么了吗

--编辑--
revalidate()方法不起作用

--编辑2--
nextButton.setPreferredSize(new Dimension(150,100));导致了更混乱的局面:由于系统的原因,我在两个窗口中都得到了全零。出来println语句,第一个窗口中的按钮大小有明显变化,但第二个窗口仍然没有变化


共 (1) 个答案

  1. # 1 楼答案

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