Java 中的JButton按钮事件

package com.Swing;
import java.awt.Color;
import java.awt.event.*;  
import javax.swing.*;    
public class ButtonExample {
    public static void main(String[] args) {
        JFrame f = new JFrame("Demo");
        final JTextField tf = new JTextField();
        tf.setBounds(50, 50, 150, 20);
        JButton b = new JButton("点击");
        b.setBounds(100, 100, 65, 30);
        b.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                tf.setText("按钮事件如此简单");
                tf.setBackground(Color.cyan);
            }
        });
        f.add(b);
        f.add(tf);
        f.setSize(300, 250);
        f.setLocationRelativeTo(null);
    f.setLayout(null);  
    f.setVisible(true);   
}  
}  

这里写图片描述


版权声明:本文为weixin_36279318原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。