事件处理程序

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

程序代码:

package bbb;
import java.awt.Color;
import javax.swing.*;
public class MyFrame {
    JFrame f;
    JPanel p;
    JLabel l1;
    JLabel l2;
    JButton b;
    JTextField t1;
    JTextField t2;
public MyFrame(){
    f=new JFrame("登陆窗口");
    p=new JPanel();
    l1=new JLabel("账号:");
    l2=new JLabel("密码:");
    t1=new JTextField(10);
    t2=new JTextField(10);
    b=new JButton("登陆");
    
    f.add(p);
    p.add(l1);
    p.add(t1);
    p.add(l2);
    p.add(t2);
    p.add(b);
    f.setLocation(500,300);
    f.setSize(400, 250);
    f.setVisible(true);
    p.setBackground(Color.pink);
}
    public static void main(String[] args){
        new MyFrame();
    }

}

运行结果:

posted @ 2019-05-10 11:19  姚中意  阅读(98)  评论(0编辑  收藏  举报