没有接触界面学习的同学,一定会对界面产生一种很神秘的感觉,经过昨天的学习让我感觉一种新奇,给我的理解就是不停的在窗体上添加组件,然后给对应的组件添加事件监听,一旦事件发生就执行相应的代码 其他的就跟以前dos命令没有什么区别
下面我分享下我的一个小程序
package WindowDemo;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
public class Demo2 {
private static Frame frame; //窗体
private static MenuBar bar1; //菜单栏
private static Menu menu1;//菜单
private static MenuItem mi1;//退出系统菜单项
private static MenuItem mi2;//记事本菜单项
private static MenuItem mi3;//好好学习菜单项
private static Menu menu2;//子菜单
private static MenuItem mi4;//天天向上菜单项
private static MenuItem mi5;//恢复标题菜单项
private static String title;
public static void main(String[] args) {
frame = new Frame();
//初始化窗体
init(frame);
//设置窗体位置
setCenter(frame);
//添加组件
addComponent(frame);
//添加事件监听
addListener();
}
private static void addComponent(Frame frame) {
bar1 = new MenuBar();
menu1 = new Menu("文件");
menu2 = new Menu("更改标题");
//创建菜单项
mi1 = new MenuItem("退出系统");
mi2 = new MenuItem("记事本");
mi3 = new MenuItem("好好学习");
mi4 = new MenuItem("天天向上");
mi5 = new MenuItem("恢复标题");
//添加
menu2.add(mi3);
menu2.add(mi4);
menu2.add(mi5);
menu1.add(menu2);
menu1.add(mi1);
menu1.add(mi2);
bar1.add(menu1);
frame.setMenuBar(bar1);
}
private static void setCenter(Frame frame) {
//获取屏幕的大小
//public static Toolkit getDefaultToolkit() 获取默认工具包。
//public abstract Dimension getScreenSize() n获取屏幕的大小
//获取对象
Toolkit kit = Toolkit.getDefaultToolkit();
//获取屏幕大小
Dimension screenSize = kit.getScreenSize();
//获取窗体大小
Dimension frameSize = frame.getSize();
int width = (screenSize.width-frameSize.width)/2;
int height= (screenSize.height-frameSize.height)/2;
//设置窗体的位置
frame.setLocation(width, height);
}
private static void addListener() {
//恢复标题事件监听
mi5.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.setTitle(title);
}
});
//天天向上事件监听
mi4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.setTitle("天天向上");
}
});
//好好学习事件监听
mi3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.setTitle("好好学习");
}
});
//记事本事件监听
mi2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Runtime run = Runtime.getRuntime();
try {
run.exec("notepad");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
//退出系统事件监听
mi1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
//窗体事件监听
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
private static void init(Frame frame) {
title = "我自己的菜单实例";
frame.setTitle(title);
frame.setSize(600, 400);
//frame.setLocation(400, 300);
//显示窗体
frame.setVisible(true);
}
}
|
|