程序如下:
/**
* @(#)Insurance.java
*
*
* @author
* @version 1.00 2007/3/30
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Insurance extends JFrame{
private JTextField txt;
private JPanel panel;
private JCheckBox HMO;
private JCheckBox PPO;
private JCheckBox dental;
private JCheckBox vision;
private final int WINDOW_WIDTH=500;
private final int WINDOW_HEIGHT=200;
private int HMOInsurance=200;
private int PPOInsurance=600;
private int dentalInsurance=75;
private int visionInsurance=20;
public Insurance()
{
setTitle("Insurance");
setSize(WINDOW_WIDTH,WINDOW_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
dental=new JCheckBox("dental Insurance");
vision=new JCheckBox("vision Insurance");
CheckboxGroup c= new CheckboxGroup();
Checkbox HMO= new Checkbox("HMO",false,c);
Checkbox PPO= new Checkbox("PPO",false,c);
txt= new JTextField( "",2 );
HMO.addItemListener(new CheckBoxListener1());
PPO.addItemListener(new CheckBoxListener1());
dental.addItemListener(new CheckBoxListener2());
vision.addItemListener(new CheckBoxListener2());
setLayout(new FlowLayout());
panel= new JPanel();
panel.add(txt);
panel.add(HMO);
panel.add(PPO);
panel.add(dental);
panel.add(vision);
add(panel);
setVisible(true);
}
private class CheckBoxListener2 implements ItemListener
{
public void itemStateChanged(ItemEvent e)
{
if (e.getSource() == dental)
{if (dental.isSelected())
txt.setText("a");
else
txt.setText("");
}
if (e.getSource() == vision)
{if (vision.isSelected())
txt.setText("b");
else
txt.setText("");
}
}
}
private class CheckBoxListener1 implements ItemListener
{
public void itemStateChanged(ItemEvent e)
{
if (e.getSource() ==HMO)
{if (HMO.isSelected())
{
txt.setText("C");
}
}
if (e.getSource() == PPO)
{if (PPO.isSelected())
{
txt.setText("D");
}
}
}
}
public static void main(String args[]){
Insurance cgf= new Insurance();
}
}
问题1:运行此程序后,我点CheckboxGroup里的HMO或者PPO 都不会在对话框中输出结果 但dental和vision就可以 我一直不明白是哪个部分写的不对..能帮我修改下吗?
问题2: 在txt.setText()语句中 如果我想输出数字类的结果 应该怎么写?就是说Intger怎么转换成String?
private int HMOInsurance=200;
private int PPOInsurance=600;
我想输出这两个部分..应该怎么写语句?不能用""来做
程序可以正常运行 麻烦大家帮我看一下 指点下...在这里感谢大家了!
[此贴子已经被作者于2007-4-2 19:33:18编辑过]