事件处理程序

package shixun;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class handle implements ActionListener{
JFrame j;
JButton k;
JPanel s;
JLabel d;

public handle(){
j=new JFrame("操作窗口");
k=new JButton("确定改变窗口颜色吗");
k.addActionListener(this);
s=new JPanel(null);
d=new JLabel();
k.setBounds(150,80,80,40);
d.setBounds(100, 200, 80, 440);
s.add(d);
s.add(k);
j.add(s);
j.setVisible(true);
j.setSize(400, 300);
}

public static void main(String args[]){
new handle();
}

public void actionPerformed(ActionEvent arg0) {

s.setBackground(Color.red);
d.setText("您已成功改变窗口颜色!");
}
}

转载于:https://www.cnblogs.com/A9999999/p/10851323.html

你可能感兴趣的:(事件处理程序)