当前位置: 代码迷 >> Java相关 >> 好心的朋友给我个思路吧!挺着急的--千里大哥来~
  详细解决方案

好心的朋友给我个思路吧!挺着急的--千里大哥来~

热度:143   发布时间:2007-04-02 17:33:03.0
好心的朋友给我个思路吧!挺着急的--千里大哥来~

程序如下:

/**
* @(#)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编辑过]

搜索更多相关的解决方案: 好心  思路  朋友  

----------------解决方案--------------------------------------------------------

[CODE]/**
* @(#)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");

HMO= new JCheckBox("HMO",false);
PPO= new JCheckBox("PPO",false);

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();
}
}[/CODE]

1,之所以不会响应,是因为你生成的HMO和PPO不是你声明的成员变量,而是重新的一个HMO.要注意变量的作用域的问题
2,可以用int i=Integer.parseInt(String s);来转换把String转成int
如果是int转成String,那就更容易了""+i就可以了
比如
int i=100;
setText(""+i);就可以了


----------------解决方案--------------------------------------------------------
1,之所以不会响应,是因为你生成的HMO和PPO不是你声明的成员变量,而是重新的一个HMO.要注意变量的作用域的问题
HMO= new JCheckBox("HMO",false);
PPO= new JCheckBox("PPO",false);

======================================================================================================
千里大哥 我这里的HMO和PPO都需要用CheckboxGroup 两个当中只能选择一个才行 你这样修改之后 两个都是可以选择的了~ 麻烦你再帮我想想吧

第2个问题已经顺利解决!非常的感谢

----------------解决方案--------------------------------------------------------
以下是引用weibin0827在2007-4-2 17:33:03的发言:

程序如下:

/**
* @(#)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.getState())
{
txt.setText("C");
}
}
if (e.getSource() == PPO)
{if (PPO.getState())
{
txt.setText("D");
}

}

}


}

public static void main(String args[]){
Insurance cgf= new Insurance();
}
}



把这两个地方修改之后 问题就解决了...哎 真高兴!!再次感谢千里大哥的帮助!


----------------解决方案--------------------------------------------------------
你想两个选择一个,最好还是用JRadioButton,然后把它们加入到同一个ButtonGroup当中去

这样从外表上看也是单选框

----------------解决方案--------------------------------------------------------

学到了...


----------------解决方案--------------------------------------------------------
  相关解决方案