zoukankan      html  css  js  c++  java
  • 测试事件响应修改界面内容

    package com.swing.demo;
    
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JTextArea;
    
    /**
     * 测试事件响应修改界面内容
     * @author Administrator
     *
     */
    public class ActionListenerTest {
        public static void main(String[] args) {
            ActionListenerTest instance = new ActionListenerTest();
            instance.show();
        }
    
        private void show() {
            JFrame frame = new JFrame("窗体");
            Container contentPane = frame.getContentPane();
            JButton btn = new JButton("按钮");
            JTextArea textArea = new JTextArea();
            textArea.setBackground(Color.white);
            textArea.setRows(10);
            MyActionListener listener = new MyActionListener();
            listener.setBtn(btn);
            listener.setTextArea(textArea);
            btn.addActionListener(listener);
            contentPane.add(btn, BorderLayout.NORTH);
            contentPane.add(textArea, BorderLayout.SOUTH);
            frame.pack();
            frame.setVisible(true);
            frame.requestFocus();
            frame.setSize(400, 300);
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    
        private class MyActionListener implements ActionListener {
    
            private JButton btn;
            private JTextArea textArea;
    
            public void setBtn(JButton btn) {
                this.btn = btn;
    
            }
    
            public void setTextArea(JTextArea textArea) {
                this.textArea = textArea;
            }
    
            @Override
            public void actionPerformed(ActionEvent e) {
                btn.setText("hello world");
                textArea.setText(textArea.getText() + "new000" + "\r\n");
            }
        }
    }
  • 相关阅读:
    第十一节 jQuery特殊效果
    第十节 使用index和一个点击事件实现选项卡
    synchronized和lock两种锁的比较
    常见的四种线程池和区别
    mybatis中的#和$的区别
    web 防止SQL注入
    GIT配置免密登录
    热点 Key 问题的发现与解决
    Redis缓存击穿
    面试必问之JVM原理
  • 原文地址:https://www.cnblogs.com/freshier/p/4617165.html
Copyright © 2011-2022 走看看