### 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();
}
}