import java.awt.*; import java.awt.event.*; public class TextFieldDemo { private Frame frame; private Button button; private TextField textfield; TextFieldDemo() { init(); } private void init() { frame = new Frame("TextFiled"); frame.setBounds(200,200,500,400); frame.setLayout(new FlowLayout()); //初始化可以确定长度 textfield = new TextField(20); frame.add(textfield); button = new Button("hello"); frame.add(button); // 添加鼠标事件监听器 myEvent(); frame.setVisible(true); } public void myEvent() { frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); textfield.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e) { //获取键盘码 int code = e.getKeyCode(); if (!(code >= KeyEvent.VK_0 && code <= KeyEvent.VK_9)) { System.out.println("please input 0~9"); //使用此方法,不处理事件,即取消掉操作 e.consume(); } } }); } //main public static void main(String[] args) { new TextFieldDemo(); } }
import java.awt.*; import java.awt.event.*; public class MyWindow { private Frame f; private TextField tf; private TextArea ta; private Button bt; MyWindow() { init(); } public void init() { f = new Frame("my window"); f.setBounds( 300, 300, 500, 400); f.setLayout(new FlowLayout()); tf = new TextField(30); ta = new TextArea(20, 30); bt = new Button("ok"); f.add(tf); f.add(bt); f.add(ta); myEvent(); f.setVisible(true); } private void myEvent() { //关闭事件 f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); bt.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String text = tf.getText(); ta.append(text+"\n"); tf.setText(""); } }); } //main public static void main(String[] args) { new MyWindow(); } }结果:
import java.awt.*; import java.awt.event.*; public class DialogDemo { private Frame fm; private Button bt; private Dialog d; private Label lab; private Button ok_btn; DialogDemo() { init(); } private void init() { fm = new Frame("dialog"); fm.setBounds(300, 300, 500, 300); fm.setLayout(new FlowLayout()); bt = new Button("dialog"); fm.add(bt); d = new Dialog(fm, "提示信息", true);//设置为true,模态对话框(不关了无法操作),设置为false,则为非模态对话框(不关了也能继续操作) d.setBounds(300, 300, 100, 100); lab = new Label(); ok_btn = new Button("ok"); d.add(lab); d.add(ok_btn); d.setLayout(new FlowLayout()); myEvent(); fm.setVisible(true); } private void myEvent() { fm.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); bt.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String info = "Dialog"; lab.setText(info); d.setVisible(true); } }); d.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { d.setVisible(false); } }); } public static void main(String[] args) { new DialogDemo(); } }结果:
import java.awt.*; import java.awt.event.*; public class MenuDemo { private Frame f; private MenuBar mb; private Menu m,subMenu; private MenuItem closeItem, subItem; MenuDemo() { init(); } private void init() { f = new Frame("dialog"); f.setBounds(300, 300, 500, 300); f.setLayout(new FlowLayout()); //菜单栏 mb = new MenuBar(); //菜单 m = new Menu("Menu"); subMenu = new Menu("subMenu"); //菜单项 subItem = new MenuItem("subItem"); closeItem = new MenuItem("quit"); //将菜单项加入菜单 subMenu.add(subItem); //将子菜单和菜单项加入菜单 m.add(subMenu); m.add(closeItem); //将菜单加入菜单栏 mb.add(m); //将菜单栏加入窗口 f.setMenuBar(mb); myEvent(); f.setVisible(true); } private void myEvent() { closeItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); } public static void main(String[] args) { new MenuDemo(); } }<strong> </strong>结果: