packageDemo02;importjava.awt.TextField;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjavax.swing.JFrame;publicclassTextText01{publicstaticvoidmain(String[] args){newMyFrame();}}classMyFrameextendsJFrame{publicMyFrame(){TextField textField =newTextField();this.add(textField);MyActionListener2 myActionListener2=newMyActionListener2();
textField.addActionListener( myActionListener2);
textField.setEchoChar('%');this.setVisible(true);this.pack();this.setDefaultCloseOperation(EXIT_ON_CLOSE);}}classMyActionListener2implementsActionListener{@OverridepublicvoidactionPerformed(ActionEvent e){// TODO 自动生成的方法存根TextField field =(TextField)e.getSource();System.out.println(field.getText());
field.setText("");}}
键盘
packageDemo03;importjava.awt.event.KeyAdapter;importjava.awt.event.KeyEvent;importjavax.swing.JFrame;publicclassTestKeyListener{publicstaticvoidmain(String[] args){newKeyFrame();}}classKeyFrameextendsJFrame{publicKeyFrame(){this.setBounds(10,10,300,400);this.setVisible(true);this.setDefaultCloseOperation(EXIT_ON_CLOSE);this.addKeyListener(newKeyAdapter(){@OverridepublicvoidkeyPressed(KeyEvent e){// TODO 自动生成的方法存根int keycode =e.getKeyCode();System.out.println(keycode);if(keycode==KeyEvent.VK_0){System.out.println("你按到零 0");}if(keycode==KeyEvent.VK_UP){System.out.println("你按到零 上键");}if(keycode==KeyEvent.VK_W){System.out.println("你按到零 W");}}});}}
可以知道你点击了什么
packageDemo03;importjava.awt.Color;importjava.awt.event.WindowAdapter;importjava.awt.event.WindowEvent;importjava.awt.event.WindowListener;importjavax.swing.JFrame;publicclassTestWindow{publicstaticvoidmain(String[] args){newWindowFrame();}}classWindowFrameextendsJFrame{publicWindowFrame(){this.setBackground(Color.blue);this.setBounds(100,100,100,100);this.setVisible(true);//this.setDefaultCloseOperation(EXIT_ON_CLOSE);//this.addWindowListener(new WindowListener());this.addWindowListener(newWindowListener(){@OverridepublicvoidwindowOpened(WindowEvent e){// TODO 自动生成的方法存根System.out.println(" windowOpened");}@OverridepublicvoidwindowClosing(WindowEvent e){// TODO 自动生成的方法存根setVisible(false);System.out.println("我要关闭");}@OverridepublicvoidwindowClosed(WindowEvent e){// TODO 自动生成的方法存根System.out.println("windowClosed");}@OverridepublicvoidwindowIconified(WindowEvent e){// TODO 自动生成的方法存根System.out.println("windowIconified");}@OverridepublicvoidwindowDeiconified(WindowEvent e){// TODO 自动生成的方法存根}@OverridepublicvoidwindowActivated(WindowEvent e){// TODO 自动生成的方法存根System.out.println("windowActivated");}@OverridepublicvoidwindowDeactivated(WindowEvent e){// TODO 自动生成的方法存根}});}}