图片按钮,单选框,多选框
package GUI;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
// 按钮的进一步学习
// 图片按钮,单选框,多选框,本质上也是按钮
public class Test18 {
public static void main(String[] args) {
new JButtonDemo();
new JRadioButtonDemo();
new JCheckBoxButtonDemo();
}
}
// 图标按钮
class JButtonDemo extends JFrame{
public JButtonDemo() {
Container container = getContentPane();
// 把一张图片变成图标
URL resource = JButtonDemo.class.getResource("piture.png");
ImageIcon imageIcon = new ImageIcon(resource);
// 将图标放在按钮上
JButton jButton = new JButton(imageIcon);
jButton.setToolTipText("这是图片按钮,也是提示"); // 将鼠标放在按钮上出现的提示文字
container.add(jButton);
setVisible(true);
setSize(500,500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
// 单选框,单个圆圈的
class JRadioButtonDemo extends JFrame{
public JRadioButtonDemo() {
Container container = getContentPane();
// 单选框(也是按钮),JRadioButton。分组,一组中只能选一个
JRadioButton jRadioButton1 = new JRadioButton("jRadioButton1");
JRadioButton jRadioButton2 = new JRadioButton("jRadioButton2");
JRadioButton jRadioButton3 = new JRadioButton("jRadioButton3");
ButtonGroup buttonGroup = new ButtonGroup(); // 分组,只能选择其中一个
buttonGroup.add(jRadioButton1);
buttonGroup.add(jRadioButton2);
buttonGroup.add(jRadioButton3);
container.add(jRadioButton1,BorderLayout.NORTH);
container.add(jRadioButton2,BorderLayout.CENTER);
container.add(jRadioButton3,BorderLayout.SOUTH);
setVisible(true);
setSize(500,500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
// 多选框,打勾的
class JCheckBoxButtonDemo extends JFrame{
public JCheckBoxButtonDemo() {
Container container = getContentPane();
JCheckBox jCheckBox1 = new JCheckBox("jCheckBox1");
JCheckBox jCheckBox2 = new JCheckBox("jCheckBox2");
JCheckBox jCheckBox3 = new JCheckBox("jCheckBox3");
container.add(jCheckBox1,BorderLayout.NORTH);
container.add(jCheckBox2,BorderLayout.CENTER);
container.add(jCheckBox3,BorderLayout.SOUTH);
setVisible(true);
setSize(500,500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
标签:container,JRadioButton,18,学习,add,按钮,new,500
From: https://www.cnblogs.com/zhanjianhai/p/17644883.html