package cn.itcase;
import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
public class Work5 {
public static void main(String[] args) {
final String title ="魔";
final Frame frame = new Frame(title);
frame.setBounds(300, 200, 400, 300);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
Button butt = new Button("神魔变");
Button butRed=new Button("红色");
Button butBlue=new Button("蓝色");
Button butGreen=new Button("绿色");
Button butReset=new Button("还原色");
frame.setLayout(new FlowLayout());
butRed.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e){
frame.setBackground(Color.red);
}
}) ;
butBlue.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e){
frame.setBackground(Color.blue);
}
}) ;
butGreen.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e){
frame.setBackground(Color.green);
}
}) ;
butReset.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e){
frame.setBackground(Color.white);
}
}) ;
frame.add(butReset);
frame.add(butGreen);
frame.add(butBlue);
frame.add(butRed);
MenuBar bar = new MenuBar();
Menu menu1 =new Menu("文件");
Menu menu2 =new Menu("更改名称");
MenuItem item1 =new MenuItem("打开记事本");
item1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Runtime runtime = Runtime.getRuntime();
try {
runtime.exec("notepad");
} catch (IOException e1) {
}
}
});
MenuItem item2 = new MenuItem("退出系统");
item2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
final MenuItem item3 = new MenuItem("好好学习");
item3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.setTitle(item3.getLabel());
}
});
final MenuItem item4 = new MenuItem("天天向上");
item4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.setTitle(item4.getLabel());
}
});
MenuItem item5 = new MenuItem("恢复标题");
item2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.setTitle(title);
}
});
menu2.add(item3);
menu2.add(item4);
menu2.add(item5);
menu1.add(menu2);
menu1.add(item1);
menu1.add(item2);
bar.add(menu1);
frame.setMenuBar(bar);
frame.setVisible(true);
}
} |
|