swing基础语法1JFrame Jpanel JButton

本文详细介绍了Swing库中常用的组件,包括JFrame窗口、JPanel面板、JButton按钮、JRadioButton单选按钮和JCheckBox多选按钮的创建与使用。通过实例展示了如何设置组件属性、添加滚动条、设置背景颜色以及添加悬浮提示等操作。同时强调了窗口关闭事件的处理和组件布局管理的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

swing 中的关闭事件系统自带 可以直接调用
Component.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Component可以是JFrame也可以是Jpanel

需要构造一个容器
Container container = this.getContentPane();
窗口,面板的背景颜色需要调用容器的setBackground(Color)方法设置

this.setBounds()方法用于设置窗口,面板…的位置和大小

this.setVisible(true); 设置可见性

这些方法基本上在所有需要使用swing语句的类中都要使用

窗口

JFrame
创建窗口类 需要 extends JFrame
初始化方法
init(){}
创建一个窗口的构造方法
new JFrame.var
设置文字Lable(窗口左上角的lable)
JLabel jLabel = new JLabel("String ");

class Myframe02 extends JFrame{
   public void init(){
       this.setVisible(true);
       this.setBounds(100,100,400,400);

       JLabel label = new JLabel("Swing");
       this.add(label);

       //让文本标签居中
       label.setHorizontalAlignment(SwingConstants.CENTER);
       //获得一个容器
       Container container = this.getContentPane();
       container.setBackground(Color.CYAN);

       this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
   }
}

面板

Jpanel
窗口只是最外层的框架,基本上所有的操作都是在面板中进行的,
需要构造窗口 再用container.add 将Jpanel添加到JFrame中去
虽然用this.add也可以 但是如果窗口中的组件多了用this.add会使得组件不那么协调
也可以在contianer的setlayout方法中设置组件的添加形式
设置有滚动条的面板
JScrollPane jScrollPane = new JScrollPane();

public class JScrolPanelDemo extends JFrame {
  public JScrolPanelDemo(){
      Container container = this.getContentPane();
      
      //文本框
      TextArea textArea = new TextArea(20,50);//行,列
      textArea.setText("文本框");

      JScrollPane jScrollPane = new JScrollPane(textArea);//设置有滚动条的面板
      container.add(jScrollPane);


      this.setVisible(true);
      this.setSize(400,400);
      this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      
  }

  public static void main(String[] args) {
      new JScrolPanelDemo();
  }
}

按钮

JButton
获得图片图标
类名.class.getResource(“路径”)
URL resource = JButtonDemo01.class.getResource(“1910300210 19软工1班 严昌敬 创新作业.jpg”);//只能是相对路径,所以图片要放在该类所在的文件夹下
Icon icon = new ImageIcon(resource); 将图片转化为图标
JButton button=new JButton(icon);

鼠标放上去的悬浮提示
button.setToolTipText(“图片图标”);

public class JButtonDemo01 extends JFrame {
    public JButtonDemo01()  {
        Container container = this.getContentPane();

        URL resource = JButtonDemo01.class.getResource("1910300210 19软工1班 严昌敬 创新作业.jpg");
        Icon icon = new ImageIcon(resource);

        JButton button=new JButton(icon);
        button.setToolTipText("图片图标");//鼠标放上去的悬浮提示

        container.add(button);

        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setSize(500,300);
        this.setVisible(true);

    }

    public static void main(String[] args) {
        new JButtonDemo01();
    }
}


单选按钮
new JRadioButton();先创建按钮
ButtonGroup group = new ButtonGroup(); 在创建一个ButtonGroup 一个Group中只能选择一个按钮从而达到单选的目的
然后把每个JRadioButton添加到 group中去
group.add(JRadioButton)
然后再将每个JRadioButton放到container中

public class JButtonDemo02 extends JFrame{
    public JButtonDemo02()  {
        Container container = this.getContentPane();

        //设置单选按钮
        JRadioButton radioButton1 = new JRadioButton("radioButton1");
        JRadioButton radioButton2 = new JRadioButton("radioButton2");
        JRadioButton radioButton3 = new JRadioButton("radioButton3");

        //要实现单选 就要把多个选项放在group里面 一个group只能选一个
        ButtonGroup group = new ButtonGroup();
        group.add(radioButton1);
        group.add(radioButton2);
        group.add(radioButton3);

        container.add(radioButton1,BorderLayout.CENTER);
        container.add(radioButton2,BorderLayout.NORTH);
        container.add(radioButton3,BorderLayout.SOUTH);

        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setSize(500,300);
        this.setVisible(true);

    }

    public static void main(String[] args) {
        new JButtonDemo02();
    }
}

多选按钮
new JCheckBox()
然后把创造的多个CheckBox放到container中去

public class JButtonDemo03 extends JFrame {
    public JButtonDemo03()  {
        Container container = this.getContentPane();

        //设置多选按钮
        JCheckBox jCheckBox01 = new JCheckBox("choose01");
        JCheckBox jCheckBox02 = new JCheckBox("choose02");
        JCheckBox jCheckBox03 = new JCheckBox("choose03");

        container.setLayout(new FlowLayout());
        container.add(jCheckBox01);
        container.add(jCheckBox02);
        container.add(jCheckBox03);

        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setSize(500,300);
        this.setVisible(true);

    }

    public static void main(String[] args) {
        new JButtonDemo03();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值