图片按钮
package 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("12.jpg");
Icon icon =new ImageIcon(resource);
JButton button = new JButton();
button.setIcon(icon);
button.setToolTipText("图片按钮");
container.add(button);
setVisible(true);
setBounds(100,100,500,300);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo01();
}
}
单选按钮
package lesson05;
import javafx.scene.control.RadioButton;
import javax.swing.*;
import java.awt.*;
public class JButtonDemo02 extends JFrame {
public JButtonDemo02(){
Container container=this.getContentPane();
JRadioButton r1=new JRadioButton("01");
JRadioButton r2=new JRadioButton("02");
JRadioButton r3=new JRadioButton("03");
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(r1);
buttonGroup.add(r2);
buttonGroup.add(r3);
container.add(r1,BorderLayout.NORTH);
container.add(r2,BorderLayout.CENTER);
container.add(r3,BorderLayout.SOUTH);
setVisible(true);
setBounds(100,100,500,300);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo02();
}
}
多选按钮
package lesson05;
import javax.swing.*;
import java.awt.*;
public class JButtonDemo03 extends JFrame {
public JButtonDemo03() {
Container container=this.getContentPane();
JCheckBox box1 = new JCheckBox("01");
JCheckBox box2 = new JCheckBox("02");//设置多选框
container.setLayout(new FlowLayout());
container.add(box1);
container.add(box2);
setVisible(true);
setBounds(100,100,500,300);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo03();
}
}