JFrame窗口的制作需要我们熟悉一些常用的命令,
setVisible:设置窗体是否可见 //一般是放在最后面
setSize:设置窗体的高度 宽度
setLocation:设置窗体的展示位置(以窗体的左上角的顶点为准)//这个要放在size之后,不然会先放中间再设置位置,导致显示的不是在中间
setLocationRelativeTo:设置居中(传入参数null表示居中展示)
setDefaultCloseOperation:设置窗体关闭后的动作
setTitle:设置窗体的名称

案例:创建一个简单的用户登录页面
需求:1.点击刷新验证码时会随机产生验证码,
2.当验证码或者账号密码输入错误时会刷新验证码以及清空密码.
3.点击登录时会显示登录成功
4.取消登录时退出程序

public static void main(String[] args) {
 JFrame jf = new JFrame();// 新建一个窗口
 jf.setSize(800, 600);// 设置大小
 jf.setTitle(“传奇”);// 设置标题
 jf.setLocationRelativeTo(null);
 Toolkit t = Toolkit.getDefaultToolkit();
 Image image = t.getImage(“image\1.jpg”);
 jf.setIconImage(image);
 jf.setLocationRelativeTo(null);
 jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 // 设置组件内容
 // 设置标签组件
 JLabel jl = new JLabel();
 jl.setText(“用户名:”);// 设置标签的名称 
 jl.setForeground(Color.blue);//设置字体颜色
 jl.setSize(80, 30);// 设置标签的大小
 jl.setLocation(180 ,200);// 设置标签存放位置
 // 把标签jl设置在窗口上面,准确的说是放在内容面板上
 // 一个窗口的面板分为三层 根面板和中间面板 以及内容面板, 组件全部放在内容 面板上面
 // 获取到内容面板
 Container con = jf.getContentPane();// 得到了内容面板
 // 内容面板的默认布局是麻将布局,东南西北中,并且没有选择位置的话会默认放到中,中间位置放了之后,中会把整个窗口占满,不能放其 ///他的组件
 // 改变内容面板的布局
 con.setLayout(null);// null表示不存在,意思是内容面板现在没有了布局
 //设置背景图片
 Image tupian=t.getImage(“image\timg.gif”);//得到图片
 Icon ic=new ImageIcon(tupian);//把图片改成图标的形式
 JLabel jl2=new JLabel(ic);
 jl2.setSize(800, 600);
 jl2.setLocation(0, 0);
 con.add(jl2);
 // 创建密码标签
 JLabel jl1 = new JLabel();
 jl1.setText(“密 码:”);
 jl1.setForeground(Color.blue);
 jl1.setSize(80, 30);
 jl1.setLocation(180, 250);// 设置密码存放位置
 jl2.add(jl);// 把jl组件放在内容面板上
 jl2.add(jl1);
 // 创建用户名输入文本框
 JTextField username = new JTextField();// 创建文本框
 username.setSize(100, 30);
 username.setLocation(260, 200);// 设置位置
 jl2.add(username);
 // 创建密码输入文本框
 JPasswordField passworld = new JPasswordField();
 passworld.setSize(100, 30);
 passworld.setLocation(260, 250);
 jl2.add(passworld);
 jf.setVisible(true);
 //创建验证码
 JLabel yanzhengma= new JLabel();
 yanzhengma.setText(“验证码:”);
 yanzhengma.setForeground(Color.blue);
 yanzhengma.setSize(80, 30);
 yanzhengma.setLocation(180, 300);
 jl2.add(yanzhengma);
 //创建验证码输入框
 JTextField yzm=new JTextField();
 yzm.setSize(75, 30);
 yzm.setLocation(260, 300);
 JTextField yzm2=new JTextField();
 yzm2.setSize(75, 30);
 yzm2.setLocation(350, 300);
 int yzm3=(int)(Math.random()*9000+1000);//创建电脑随机生成数字
 String s=String.valueOf(yzm3);
 yzm2.setText(s);
 jl2.add(yzm2);
 jl2.add(yzm);
 //做按钮 登录按钮 取消按钮 已经验证码刷新按钮
 JButton ok=new JButton(“登录”);
 //Font f=new Font(“华文行楷”,Font.BOLD,20);//根据指定字体名称、样式和磅值大
 //小,创建一个新 Font。
//jb.setFont(f);
 Font f=new Font(“叶根友毛笔行书2.0版”,Font.BOLD,35); 
 ok.setFont(f);
 ok.setSize(150, 40);
 ok.setLocation(150, 420);
 jl2.add(ok);
 JButton cancel=new JButton(“取消”);
 cancel.setFont(f);
 cancel.setSize(150, 40);
 cancel.setLocation(420,420);
 jl2.add(cancel);
 JButton refresh=new JButton(“获取验证码”);
 Font f1=new Font(“宋体”,Font.BOLD,12);
 refresh.setFont(f1);
 refresh.setSize(100, 32);
 refresh.setLocation(460, 300);
 jl2.add(refresh);
 JLabel biaoti=new JLabel(“经典传奇”);
 biaoti.setForeground(Color.red);
 biaoti.setSize(150, 150);
 Font f2=new Font(“楷体”,Font.BOLD,35);
 Font f3=new Font(“叶根友毛笔行书2.0版”,Font.BOLD,20);
 jl.setFont(f3);
 jl1.setFont(f3);
 yanzhengma.setFont(f3);
 biaoti.setFont(f2);
 biaoti.setLocation(330, 50);
 jl2.add(biaoti);
 //创建事件监听器
 yzm.addActionListener(new ActionListener() { 
 @Override
 public void actionPerformed(ActionEvent e) { 
 }
 });
 ok.addActionListener(new ActionListener() {//创建登陆按钮的动作监听器 
 @Override
 public void actionPerformed(ActionEvent e) {
 String name=username.getText();//获取到用户名文本框的内容
 String pass=passworld.getText();//获取到密码文本框的内容
 String hedui=yzm.getText();//获取到验证码上的内容
 if(hedui.equals(s)){
 if(name.equals(“admin”) && pass.equals(“123456”)){
 JOptionPane.showMessageDialog(null, “正在登陆”);
 jf.dispose(); 
 }else{
 JOptionPane.showMessageDialog(null, “用户名或者密码不正确,请重新输入”);
 passworld.setText(null);
 yzm2.setText(String.valueOf((int)(Math.random()*9000+1000)));
 }
 }else{
 JOptionPane.showMessageDialog(null, “验证码错误”);
 yzm2.setText(String.valueOf((int)(Math.random()*9000+1000)));
 }
 //这个地方应该加一个自动刷新验证码的功能,不管是验证码错误,还是账号密码错误,都重新输入验证码
 }
 });
 cancel.addActionListener(new ActionListener() {//创建取消按钮的事件监听器 
 @Override
 public void actionPerformed(ActionEvent e) {
 // TODO Auto-generated method stub
 jf.dispose();
 }
 });
 refresh.addActionListener(new ActionListener() { 
 @Override
 public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub
 yzm2.setText(String.valueOf((int)(Math.random()*9000+1000)));
 }
 });
 }

运行结果:

java jframe创建 jframe怎么创建_java jframe创建