赞
踩
- import java.awt.EventQueue;
- import java.awt.GridLayout;
- import java.awt.event.KeyAdapter;
- import java.awt.event.KeyEvent;
-
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- import javax.swing.UIManager;
- import javax.swing.border.EmptyBorder;
-
-
- public class EventDemo extends JFrame{
-
- /**
- *
- */
- private static final long serialVersionUID = 8162801603108721756L;
-
- private JPanel contentPane;
-
- private JTextField textField1;
- private JTextField textField2;
-
- private JLabel label1;
- private JLabel label2;
-
-
- public static void main(String[] args){
- try {
- UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");// 设置程序外观的风格
- } catch (Throwable e) {
- // TODO: handle exception
- e.printStackTrace();
- }
-
- EventQueue.invokeLater(new Runnable(){
-
- @Override
- public void run() {
- // TODO Auto-generated method stub
- try {
- EventDemo frame = new EventDemo();// 设置窗体
- frame.setVisible(true);// 设置可见
- } catch (Exception e) {
- // TODO: handle exception
- e.printStackTrace();
- }
- }
- });
- }
-
- public EventDemo(){
- setTitle("监听文本输入事件");
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(100, 100, 250, 100);
-
- contentPane = new JPanel();// 创建全局内容面板
- contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));// 设置边框大小
-
- setContentPane(contentPane);// 应用全局内容面板
-
- contentPane.setLayout(new GridLayout(2,1,5,5));// 为内容面板使用网格布局
-
- JPanel pannel1 = new JPanel();// 新建面板来组织标签
- contentPane.add(pannel1);// 添加一号内容面板
- label1 = new JLabel("第一个文本框");// 一号面板标签
- pannel1.add(label1);
- textField1 = new JTextField();// 一号内容面板文本框
-
- // 一号 文本框添加按键释放事件监听
- textField1.addKeyListener(new KeyAdapter() {
- @Override
- public void keyReleased(KeyEvent e){
- do_textField1_keyRelease(e);
- }
- });
- pannel1.add(textField1);// 应用文本框
- textField1.setColumns(18);// 将文本框设置成18列
-
- JPanel panel2 = new JPanel();
- contentPane.add(panel2);
- label2 = new JLabel("第二个文本框");
- panel2.add(label2);
- textField2 = new JTextField();
- panel2.add(textField2);
- textField2.setColumns(18);
-
- }
-
- protected void do_textField1_keyRelease(KeyEvent e) {
- // TODO Auto-generated method stub
- String text = textField1.getText();// 获得第一个文本框的文本内容
- textField2.setText(text);// 将获取的文本内容显示在第二个文本框内
- }
-
-
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。