图片按钮类:
package com.zhang.Study.图片按钮单选多选框; import javax.swing.*; import java.awt.*; import java.net.URL; public class Application extends JFrame { public Application(){ Container container= this.getContentPane();//创建一个容器 //把一个图片变成图标 URL resourse = Application.class.getResource("tiedan.jpeg"); Icon icon = new ImageIcon(resourse); //把这个图标放在按钮上 JButton button =new JButton(); button.setIcon(icon); button.setToolTipText("图片按钮");//当鼠标移动到按钮上时,会出现提示信息 //add操作 container.add(button); this.setBounds(400,400,400,400); this.setVisible(true); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new Application(); } }
单选框:
package com.zhang.Study.图片按钮单选多选框; import javax.swing.*; import java.awt.*; import java.net.URL; public class Application2 extends JFrame { public Application2(){ Container container= this.getContentPane();//创建一个容器 //单选框JRadioButton JRadioButton jRadioButton1 = new JRadioButton("JRadioButton1"); JRadioButton jRadioButton2 = new JRadioButton("JRadioButton2"); JRadioButton jRadioButton3 = new JRadioButton("JRadioButton3"); //分租ButtonGroup,把三个单选框分到一个组里面,一个组里面只能选择一个。 ButtonGroup group = new ButtonGroup(); group.add(jRadioButton1); group.add(jRadioButton2); group.add(jRadioButton3); //add操作 container.add(jRadioButton1,BorderLayout.CENTER); container.add(jRadioButton2,BorderLayout.NORTH); container.add(jRadioButton3,BorderLayout.SOUTH); this.setBounds(400,400,400,400); this.setVisible(true); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new Application2(); } }
多选框:
package com.zhang.Study.图片按钮单选多选框; import javax.swing.*; import java.awt.*; import java.net.URL; public class Application3 extends JFrame { public Application3(){ Container container= this.getContentPane();//创建一个容器 //多选框JcheckBOX JCheckBox checkBox1 = new JCheckBox("1"); JCheckBox checkBox2 = new JCheckBox("2"); //add container.add(checkBox1,BorderLayout.NORTH); container.add(checkBox2,BorderLayout.WEST); this.setBounds(400,400,400,400); this.setVisible(true); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new Application3(); } }