操作环境:
eclipse
Java6 以上
- 创建project,创建使用gui的class,然后继承JFrame。(class EventFireGui extends JFrame)
- 进行基本设置
import javax.swing.JFrame;
public class EventFireGui extends JFrame{
public EventFireGui(){
super("Event Firer");
setBounds(100, 100, 300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args){
new EventFire Guie();
}
}
setBounds
- public void setBounds(int x, int y, int width, int height)
移动并调整此组件的大小。左上角的新位置由x和y指定,新大小由宽度和高度指定。如果宽度或高度值小于上一次调用setMinimumSize指定的最小值,则会自动放大。
Overrides:
setBounds in class Component
Parameters:
- x - the new x-coordinate of this component
- y - the new y-coordinate of this component
- width - the new width of this component
- height - the new height of this component
setDefaultCloseOperation
- public void setDefaultCloseOperation(int operation)
设置当用户在此帧上启动“关闭”时默认发生的操作。必须指定以下选项之一:
- DO_NOTHING_ON_CLOSE (defined in WindowConstants): 不执行任何操作;要求程序处理注册的WindowListener对象的windowClosing方法中的操作。
- HIDE_ON_CLOSE (defined in WindowConstants): 调用任何注册的WindowListener对象后自动隐藏框架。
- DISPOSE_ON_CLOSE (defined in WindowConstants):调用任何注册的WindowListener对象后,自动隐藏和释放框架。
- EXIT_ON_CLOSE (defined in JFrame): 使用系统退出方法退出应用程序。仅在应用程序中使用此选项。
setVisible
- public void setVisible(boolean b)
根据参数b的值显示或隐藏此窗口。
Parameters:
- b-如果为true,则使窗口可见,否则隐藏窗口。如果窗口和/或其所有者尚不可显示,则两者都将变为可显示。窗口将在可见之前进行验证。如果窗口已经可见,这将使窗口位于前面。
- 如果为false,则隐藏此窗口、其子组件及其所有的子组件。通过调用setVisible(true),可以使窗口及其子组件再次可见。
- 放入Panel,然后在里面放入按钮
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class EventFireGui extends JFrame{
public EventFireGui