package cn.itcast_07_Menu;
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class MenuDemo {
/**
* @author
*/
public static void main(String[] args) {
final Frame frame =new Frame("QQ登陆界面");
frame.setBounds(300, 300, 800, 600);
//改变窗口的布局
frame.setLayout(null);
//添加菜单栏
MenuBar menuBar = new MenuBar();
frame.setMenuBar(menuBar);
Menu titleMenu = new Menu("文件");
menuBar.add(titleMenu);
Menu fileMenu = new Menu("修改标题");
titleMenu.add(fileMenu);
MenuItem fileMenu2=new MenuItem("退出");
titleMenu.add(fileMenu2);
final MenuItem M1 = new MenuItem("好好学习");
final MenuItem M2 = new MenuItem("天天向上");
MenuItem M3 = new MenuItem("恢复标题");
fileMenu.add(M1);
fileMenu.add(M2);
fileMenu.add(M3);
//创建时间监控
final String name =frame.getTitle();
M1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.setTitle(M1.getLabel());
}
});
M1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.setTitle(M1.getLabel());
}
});
M2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.setTitle(M2.getLabel());
}
});
M3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.setTitle(name);
}
});
fileMenu.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
//添加标签
Label lable = new Label("请输入QQ号码:");
frame.add(lable);
lable.setBounds(200, 100, 100, 20);
TextField text =new TextField(80);
frame.add(text);
text.setBounds(200, 130, 200,30);
Label lable2 = new Label("请输入QQ密码:");
frame.add(lable2);
lable2.setBounds(200, 160, 100, 20);
TextField text2 =new TextField(80);
frame.add(text2);
text2.setBounds(200, 190, 200,30);
//设置登录按钮
Button button =new Button("登录");
button.setBounds(250, 240, 50,30);
frame.add(button);
//设置按钮事件
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.setBackground(Color.cyan);
}
});
//限制输键盘输入
text.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
char ch=e.getKeyChar();
if(ch>='0'&&ch<='9'){}
else{e.consume();}
}
});
text.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
char ch =e.getKeyChar();
switch(ch){
case '0':
frame.setBackground(Color.BLACK);
break;
case '1':
frame.setBackground(Color.BLUE);
break;
case '2':
frame.setBackground(Color.cyan);
break;
case '3':
frame.setBackground(Color.DARK_GRAY);
break;
case '4':
frame.setBackground(Color.gray);
break;
case '5':
frame.setBackground(Color.blue);
break;
case '6':
frame.setBackground(Color.darkGray);
break;
case '7':
frame.setBackground(Color.gray);
break;
case '8':
frame.setBackground(Color.cyan);
break;
case '9':
frame.setBackground(Color.cyan);
break;
}
}
});
//设置窗口监听
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setVisible(true);
}
}
|
|