首页 > 其他 > 详细

按钮、单选框、复选框

时间:2021-08-23 08:33:49      阅读:14      评论:0      收藏:0      [点我收藏+]

### 3.5 按钮

普通按钮

```java
package com.zishi.lesson05;

import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class JButtonDemo01 extends JFrame {

public JButtonDemo01(){
Container container = this.getContentPane();
//将一个图片变成图标
URL resource = JButtonDemo01.class.getResource("Amucs.jpg");
Icon icon = new ImageIcon(resource);

//把这个图标放在按钮上
JButton button = new JButton();
button.setIcon(icon);
button.setToolTipText("图片按钮");//提示文本

container.add(button);
this.setVisible(true);
this.setSize(200,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}

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

 

- 单选按钮

```java
package com.zishi.lesson05;

import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class JButtonDemo02 extends JFrame {

public JButtonDemo02(){
Container container = this.getContentPane();
//将一个图片变成图标
URL resource = JButtonDemo01.class.getResource("Amucs.jpg");
Icon icon = new ImageIcon(resource);

//单选框
JRadioButton radioButton01 = new JRadioButton("JRadioButton01");
JRadioButton radioButton02 = new JRadioButton("JRadioButton02");
JRadioButton radioButton03 = new JRadioButton("JRadioButton03");
//由于单选框只能选择一个,所以一般将其分组
ButtonGroup group = new ButtonGroup();
group.add(radioButton01);
group.add(radioButton02);
group.add(radioButton03);

container.add(radioButton01,BorderLayout.NORTH);
container.add(radioButton02,BorderLayout.CENTER);
container.add(radioButton03,BorderLayout.SOUTH);


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

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

 

- 复选按钮

```java
package com.zishi.lesson05;

import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class JButtonDemo03 extends JFrame {

public JButtonDemo03(){
Container container = this.getContentPane();
//将一个图片变成图标
URL resource = JButtonDemo01.class.getResource("Amucs.jpg");
Icon icon = new ImageIcon(resource);

//多选框
JCheckBox checkbox01 = new JCheckBox("checkbox01");
JCheckBox checkbox02 = new JCheckBox("checkbox02");
JCheckBox checkbox03 = new JCheckBox("checkbox03");

container.add(checkbox01,BorderLayout.NORTH);
container.add(checkbox02,BorderLayout.CENTER);
container.add(checkbox03,BorderLayout.SOUTH);

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

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

按钮、单选框、复选框

原文:https://www.cnblogs.com/yizhifei/p/15174127.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!