JFrame,JLabel和JButtenl的运用
运用JFrame,JLabel和JButten来制作出心仪的登录程序!
public class Chengxu {
public static void main(String[] args) {
//先创建一个窗口,不过此时创建的是隐藏的。
JFrame jf=new JFrame();
//再设置窗口大小,单位是像素。
jf.setSize(800,600);
//再窗口的标题
jf.setTitle("Cxj的程序");
//设置图标
Toolkit t=Toolkit.getDefaultToolkit();
Image image=t.getImage("image\\a.jpg");
jf.setIconImage(image);
//设置窗口居中
jf.setLocationRelativeTo(null);
//设置窗口的关闭
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//存放组件,标签组件
JLabel jl=new JLabel();
jl.setText("用户名");
jl.setForeground(Color.CYAN);
jl.setFont(new Font("华文行楷",Font.BOLD,16));
jl.setSize(100, 30);
jl.setLocation(160, 50);//标签的存在位置
//把标签jl设置在窗口上面
//一个窗口的面板分为三层 根面板和中间面板 以及内容面板, 组件全部放在内容面板上面
// 获取到内容面板
Container con=jf.getContentPane();
//内容面板的默认布局是麻将布局 东西南北 中 在添加组件的时候如果没有指定放在那里则会放在中间并且把窗口沾满,
// 重新设置布局
con.setLayout(null);
con.add(jl);
con.setBackground(Color.yellow);//设置窗口颜色
//设置背景图片
Toolkit t1=Toolkit.getDefaultToolkit();
Image image1=t1.getImage("image\\e.jpg");
Icon ic=new ImageIcon(image1);
JLabel jl3=new JLabel(ic);
jl3.setSize(800, 600);
jl3.setLocation(0, 0);
con.add(jl3);
//再创造一个密码标签
JLabel jl1=new JLabel();
jl1.setText("密 码");
jl1.setForeground(Color.CYAN);
jl1.setFont(new Font("华文行楷",Font.BOLD,16));
jl1.setSize(100, 30);
jl1.setLocation(160, 130);
jl3.add(jl1);
//创建文本框
JTextField username=new JTextField();
username.setSize(233, 30);
username.setLocation(250, 50);
jl3.add(username);
JPasswordField password=new JPasswordField();
password.setSize(233, 30);
password.setLocation(250, 130);
jl3.add(password);
//创建验证码标签
JLabel jl2=new JLabel();
jl2.setText("验证码");
jl2.setForeground(Color.CYAN);
jl2.setFont(new Font("华文行楷",Font.BOLD,16));
jl2.setSize(100,30);
jl2.setLocation(160, 200);
jl3.add(jl2);
JTextField yzm1=new JTextField();
yzm1.setSize(100, 30);
yzm1.setLocation(250, 200);
jl3.add(yzm1);
JTextField yzm2=new JTextField();
yzm2.setSize(100, 30);
yzm2.setLocation(380, 200);
jl3.add(yzm2);
JButton huoqu=new JButton();
huoqu.setText("获取验证码");
huoqu.setSize(100, 30);
huoqu.setLocation(490, 200);
huoqu.setForeground(Color.GRAY);
jl3.add(huoqu);
//设置按获取验证码按钮可获得验证码
JButton ok=new JButton();
ok.setSize(160, 60);
ok.setLocation(120, 400);
ok.setText("登录");
ok.setFont(new Font("华文行楷",Font.PLAIN,25));
ok.setForeground(Color.green);
jl3.add(ok);
JButton cancel=new JButton();
cancel.setSize(160, 60);
cancel.setLocation(520, 400);
cancel.setText("取消");
cancel.setFont(new Font("华文行楷",Font.PLAIN,25));
cancel.setForeground(Color.red);
jl3.add(cancel);
//设置窗口显示出来
jf.setVisible(true);
}
}