0
点赞
收藏
分享

微信扫一扫

Java中JPanel使用方法

前行的跋涉者 2022-03-16 阅读 100
java

/***JPanel面板的使用方法***/
package myProject;
import java.awt.*;
import javax.swing.*;

 class Tt1 extends JFrame{
    
    JPanel mb1,mb2;
       JButton an1,an2,an3,an4,an5;
       Tt1(){                   //构造函数
            mb1=new JPanel();//JPanel()默认的是流式布局
            mb2=new JPanel();
            
            an1=new JButton("可乐");
            an2=new JButton("红茶");
            an3=new JButton("绿茶");
            an4=new JButton("啤洒");
            an5=new JButton("矿泉水");
            //添加组件时先将JPanel上的组件添加完毕,再添加JFrame的组件
            
            mb1.add(an1); mb1.add(an2);
            mb2.add(an3); mb2.add(an4);
            
            this.add(mb1,BorderLayout.SOUTH);
            this.add(mb2,BorderLayout.NORTH);
            this.add(an5);
            //边界布局被 添加在中间的组件可以不写第二个参数
            
            this.setTitle("布局综合应用");
            this.setSize(300,200);
            this.setLocation(200,200);
            this.setResizable(false);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setVisible(true);
       
    
       }
    
}
 
 
 class Tt2 extends JFrame
 {
     JPanel mb1,mb2,mb3;//三个面板
      JButton an1,an2;    //两个按钮
      JLabel bq1,bq2;     //两个标签
      JTextField wbk;     //文本框(显示)
      JPasswordField mmk;  //密码框(不显示)
    Tt2()
      {
        mb1=new JPanel();//三个面板
        mb2=new JPanel();
        mb3=new JPanel();
        
        bq1=new JLabel("用户名");//标签 
        bq2=new JLabel("密    码");
       
        an1=new JButton("登录");//按钮
        an2=new JButton("取消");
        
        wbk=new JTextField(10);//文本框,里面给一个长度
        mmk=new JPasswordField(10);//密码框,给个长度
        
         this.setLayout(new GridLayout(3,1));//网格布局三行一列
         mb1.add(bq1);   mb1.add(wbk);
         mb2.add(bq2);   mb2.add(mmk);
         mb3.add(an1);   mb3.add(an2);
         
         this.add(mb1);
         this.add(mb2);
        this.add(mb3);
        
        this.setTitle("用户登录");
        this.setSize(300,200);
        this.setLocation(200,200);
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
      }
     
 }
    

class Tt3 extends JFrame
{
    JPanel mb1,mb2,mb3;
     JButton an1,an2;
     JLabel bq1,bq2;
     JCheckBox fxk1,fxk2,fxk3;
     JRadioButton dx1,dx2;

    public Tt3()
    {
        mb1=new JPanel();
        mb2=new JPanel();
        mb3=new JPanel();
        
        bq1=new JLabel("特长");
        bq2=new JLabel("性别");
        
        an1=new JButton("注册");
        an2=new JButton("取消");
        
        fxk1=new JCheckBox("音乐");
        fxk2=new JCheckBox("体育");
        fxk3=new JCheckBox("文艺");
        
        dx1=new JRadioButton("男");
        dx2=new JRadioButton("女");
       
        
        
        this.setLayout(new GridLayout(3,1));
        
        mb1.add(bq1);   mb1.add(fxk1);  mb1.add(fxk2);  mb1.add(fxk3);
        mb2.add(bq2);   mb2.add(dx1);   mb2.add(dx2);
        mb3.add(an1);   mb3.add(an2);
        
        this.add(mb1);
        this.add(mb2);
        this.add(mb3);
        
        this.setTitle("用户注册");
        this.setSize(300,200);
        this.setLocation(300,280);
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
}

}


public class Test4 {
    public static void main(String[] args)
    {
        Tt3 t1=new Tt3();
        Tt2 t2=new Tt2();
    }

}
 

举报

相关推荐

0 条评论