程序示例:
package MyJava.Base;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.*;
class JCheckBoxFrame extends JFrame implements ItemListener
{
private JCheckBox c1;
private JCheckBox c2;
private JCheckBox c3;
private JCheckBox c4;
private JCheckBox c5;
private JCheckBox c6;
private JCheckBox c7;
public JCheckBoxFrame(String title)
{
super(title);
setSize(400,500);
//获得容器
Container container=this.getContentPane();
//设置布局为6行一列
container.setLayout(new GridLayout(2,1));
//创建面板一,文字式问卷调查
JPanel pane=new JPanel(new GridLayout(1,4));
//设置面板标题
pane.setBorder(BorderFactory.createTitledBorder("你最喜欢的人是谁??"));
//创建选择的选项
c1=new JCheckBox("老婆");
c2=new JCheckBox("情人");
c3=new JCheckBox("二奶");
//创建默认选项
c4=new JCheckBox("以上都不喜欢",true);
//添加到面板
pane.add(c1);
pane.add(c2);
pane.add(c3);
pane.add(c4);
//把面板加入到容器中
container.add(pane);
//创建面板二,图像式JCheckBox
JPanel pane2=new JPanel(new GridLayout(3,1));
pane2.setBorder(BorderFactory.createTitledBorder("你最喜欢的明星是谁??"));
//创建带有Icon的JCheckBox
c5=new JCheckBox("柳真",new ImageIcon("E://Java//JCreator2.5//picture//liuzhen.jpg"));
c6=new JCheckBox("全智贤",new ImageIcon("E://Java//JCreator2.5//picture//quan.jpg"));
c7=new JCheckBox("宋慧乔",new ImageIcon("E://Java//JCreator2.5//picture//song2.jpg"));
c5.addItemListener(this);
c6.addItemListener(this);
c7.addItemListener(this);
//加入面板
pane2.add(c5);
pane2.add(c6);
pane2.add(c7);
//把面板加入到容器中
container.add(pane2);
}
public void itemStateChanged(ItemEvent evt)
{
if(evt.getStateChange()==evt.SELECTED)
{
if(evt.getSource()==c5)
JOptionPane.showMessageDialog(this,"好小子,有眼光,柳真是我偶像","柳真",JOptionPane.OK_OPTION);
if(evt.getSource()==c6)
JOptionPane.showMessageDialog(this,"眼光一般,没有柳真好","全知贤",JOptionPane.OK_OPTION);
if(evt.getSource()==c7)
JOptionPane.showMessageDialog(this,"眼光一般,没有柳真好","宋慧乔",JOptionPane.OK_OPTION);
}
}
}
public class JCheckBoxDemo
{
public static void main(String[] args) throws Exception
{
JCheckBoxFrame frame=new JCheckBoxFrame("JCheckBoxDemo");
// frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
{
private JCheckBox c1;
private JCheckBox c2;
private JCheckBox c3;
private JCheckBox c4;
private JCheckBox c5;
private JCheckBox c6;
private JCheckBox c7;
public JCheckBoxFrame(String title)
{
super(title);
setSize(400,500);
//获得容器
Container container=this.getContentPane();
//设置布局为6行一列
container.setLayout(new GridLayout(2,1));
//创建面板一,文字式问卷调查
JPanel pane=new JPanel(new GridLayout(1,4));
//设置面板标题
pane.setBorder(BorderFactory.createTitledBorder("你最喜欢的人是谁??"));
//创建选择的选项
c1=new JCheckBox("老婆");
c2=new JCheckBox("情人");
c3=new JCheckBox("二奶");
//创建默认选项
c4=new JCheckBox("以上都不喜欢",true);
//添加到面板
pane.add(c1);
pane.add(c2);
pane.add(c3);
pane.add(c4);
//把面板加入到容器中
container.add(pane);
//创建面板二,图像式JCheckBox
JPanel pane2=new JPanel(new GridLayout(3,1));
pane2.setBorder(BorderFactory.createTitledBorder("你最喜欢的明星是谁??"));
//创建带有Icon的JCheckBox
c5=new JCheckBox("柳真",new ImageIcon("E://Java//JCreator2.5//picture//liuzhen.jpg"));
c6=new JCheckBox("全智贤",new ImageIcon("E://Java//JCreator2.5//picture//quan.jpg"));
c7=new JCheckBox("宋慧乔",new ImageIcon("E://Java//JCreator2.5//picture//song2.jpg"));
c5.addItemListener(this);
c6.addItemListener(this);
c7.addItemListener(this);
//加入面板
pane2.add(c5);
pane2.add(c6);
pane2.add(c7);
//把面板加入到容器中
container.add(pane2);
}
public void itemStateChanged(ItemEvent evt)
{
if(evt.getStateChange()==evt.SELECTED)
{
if(evt.getSource()==c5)
JOptionPane.showMessageDialog(this,"好小子,有眼光,柳真是我偶像","柳真",JOptionPane.OK_OPTION);
if(evt.getSource()==c6)
JOptionPane.showMessageDialog(this,"眼光一般,没有柳真好","全知贤",JOptionPane.OK_OPTION);
if(evt.getSource()==c7)
JOptionPane.showMessageDialog(this,"眼光一般,没有柳真好","宋慧乔",JOptionPane.OK_OPTION);
}
}
}
public class JCheckBoxDemo
{
public static void main(String[] args) throws Exception
{
JCheckBoxFrame frame=new JCheckBoxFrame("JCheckBoxDemo");
// frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
程序运行的结果:
没有选择下面的图像时
选择下面的图像时弹出对话框,程序没做好,有点不像JCheckBox了,呵呵,下次改进了