比如改成如下的:import java.awt.event.*; import javax.swing.*;public class UsingHtml implements ActionListener{ public void actionPerformed(ActionEvent e){ } public static void main(String[] args){ JFrame f = new JFrame("Test"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(new JButton("a")); f.pack(); f.setVisible(true); } } 你再试试。是不是明白了?具体可以看看 JDK 的 docs。
public void pack() Causes this Window to be sized to fit the preferred size and layouts of its subcomponents. If the window and/or its owner are not yet displayable, both are made displayable before calculating the preferred size. The Window will be validated after the preferredSize is calculated. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ java.awt.Window.pack()
import javax.swing.*;public class UsingHtml implements ActionListener{
public void actionPerformed(ActionEvent e){ }
public static void main(String[] args){
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new JButton("a"));
f.pack();
f.setVisible(true);
}
}
你再试试。是不是明白了?具体可以看看 JDK 的 docs。
Causes this Window to be sized to fit the preferred size and layouts of its subcomponents. If the window and/or its owner are not yet displayable, both are made displayable before calculating the preferred size. The Window will be validated after the preferredSize is calculated.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
java.awt.Window.pack()
setSize()就失去作用了,它是更具控件的大小
进行布局的
进行布局的,他的优先级高于setSize(),所以这里窗口就不是200*200的