第六次上机

完成一个按钮的事件处理程序,实现功能自拟,例如:改变窗口的背景颜色,改变按钮的位置等等

package swing;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing .*;
public class TestButton implements ActionListener{
       JFrame f;
       JPanel p;
       JButton b;
       public TestButton() {
           f = new JFrame();
           p = new JPanel();
           b = new JButton("按钮");
           p.add(b);
           f.add(p);
           f.setVisible(true);
           f.setSize(400,300);
           
           b.addActionListener(this);
       }

    @Override
    public void actionPerformed(ActionEvent e) {
           p.setBackground(Color.yellow);
        
    }
    public static void main(String[] args) {
        new TestButton();
        
    }
}

猜你喜欢

转载自www.cnblogs.com/susususu/p/10835941.html