Java.swing包中的JFrame类对于创建窗口很有效,它继承Container类,能够包含其它的组件.
右边显示了创建窗口的代码和JFrame的几个常用函数.
public class MyFrame extends JFrame {
private static final long serialVersionUID = 1379963724699883220L;
/**
* 构造函数
*
*/
public MyFrame() {
// 设置窗口标题
this.setTitle("程序标题");
// 定位窗口
this.setLocation(20, 20);
// 设置窗口大小
this.setSize(480, 320);
// 显示窗口
setVisible(true);
}
public static void main(String[] args){
new MyFrame();
}
}
将窗口定位在屏幕正中
使用Toolkit.getDefaultToolkit().getScreenSize()方法可以取得屏幕的大小,再调用setLocation函数可以将程序定位在屏幕正中.
public class MyFrame extends JFrame {
private static final long serialVersionUID = 1379963724699883220L;
/**
* 构造函数
*
*/
public MyFrame() {
// 设置窗口标题
this.setTitle("程序标题");
// 设置程序大小并定位程序在屏幕正中
setSizeAndCentralizeMe(480, 320);
// 显示窗口
setVisible(true);
}
// 设置程序大小并定位程序在屏幕正中
private void setSizeAndCentralizeMe(int width, int height) {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
this.setSize(width, height);
this.setLocation(screenSize.width / 2 - width / 2, screenSize.height
/ 2 - height / 2);
}
public static void main(String[] args){
new MyFrame();
}
}
点击窗口右上角的关闭按钮关闭窗口,退出程序
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)可以达到此功能,否则按关闭按钮窗口关闭但不退出程序.
public class MyFrame extends JFrame {
private static final long serialVersionUID = 1379963724699883220L;
/**
* 构造函数
*
*/
public MyFrame() {
// 设置窗口标题
this.setTitle("程序标题");
// 设置程序大小并定位程序在屏幕正中
setSizeAndCentralizeMe(480, 320);
// 显示窗口
setVisible(true);
// 点击窗口右上角的关闭按钮关闭窗口,退出程序
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
// 设置程序大小并定位程序在屏幕正中
private void setSizeAndCentralizeMe(int width, int height) {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
this.setSize(width, height);
this.setLocation(screenSize.width / 2 - width / 2, screenSize.height
/ 2 - height / 2);
}
public static void main(String[] args){
new MyFrame();
}
}
添加窗口关闭事件处理
// 点击窗口右上角的关闭按钮关闭窗口,退出程序
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.out.println("程序退出.");
System.exit(0);
}
});
上面的代码实现了一个WindowAdapter的匿名类,并将它注册为窗口事件的监听器.
public class MyFrame extends JFrame {
private static final long serialVersionUID = 1379963724699883220L;
/**
* 构造函数
*
*/
public MyFrame() {
// 设置窗口标题
this.setTitle("程序标题");
// 设置程序大小并定位程序在屏幕正中
setSizeAndCentralizeMe(480, 320);
// 显示窗口
setVisible(true);
// 点击窗口右上角的关闭按钮关闭窗口,退出程序
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.out.println("程序退出.");
System.exit(0);
}
});
}
// 设置程序大小并定位程序在屏幕正中
private void setSizeAndCentralizeMe(int width, int height) {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
this.setSize(width, height);
this.setLocation(screenSize.width / 2 - width / 2, screenSize.height
/ 2 - height / 2);
}
public static void main(String[] args) {
new MyFrame();
}
}
设置程序感观
UIManager.getInstalledLookAndFeels()可得到可用的感观数组,然后取数组中元素的getClassName()方法可得到感观类名,再调用
UIManager.setLookAndFeel(strLookFeel); SwingUtilities.updateComponentTreeUI(this);
方法可设置窗口感观.
public class MyFrame extends JFrame {
/**
* 构造函数
*
*/
public MyFrame() {
// 设置窗口标题
this.setTitle("程序标题");
// 设置程序大小并定位程序在屏幕正中
setSizeAndCentralizeMe(480, 320);
// 显示窗口
setVisible(true);
// 设置程序感观
setupLookAndFeel();
....;
}
...
// 设置程序感观
private void setupLookAndFeel() {
// 取得系统当前可用感观数组
UIManager.LookAndFeelInfo[] arr = UIManager.getInstalledLookAndFeels();
Random random = new Random();
String strLookFeel=arr[random.nextInt(arr.length)].getClassName();
try {
UIManager.setLookAndFeel(strLookFeel);
SwingUtilities.updateComponentTreeUI(this);
} catch (Exception e) {
System.out.println("Can't Set Lookandfeel Style to " + strLookFeel);
}
}
....
}
设置程序感观为跨平台的感观
UIManager.getCrossPlatformLookAndFeelClassName()可得到跨平台的感观.
public class MyFrame extends JFrame {
public MyFrame() {
// 设置窗口标题
this.setTitle("程序标题");
// 设置程序大小并定位程序在屏幕正中
setSizeAndCentralizeMe(480, 320);
// 显示窗口
setVisible(true);
// 设置程序感观
setupLookAndFeel();
}
// 设置程序感观
private void setupLookAndFeel() {
String strLookFeel = UIManager.getCrossPlatformLookAndFeelClassName();
try {
UIManager.setLookAndFeel(strLookFeel);
} catch (Exception e) {
System.out.println("Can't Set Lookandfeel Style to " + strLookFeel);
}
}
}