两个以上的JCheckBox?

时间:2016-12-11 15:59:04

标签: java swing checkbox jcheckbox

我想在我的程序中选择8个复选框,如果单击它们中的每一个都会有语句 它与前两个复选框很好用,但从第三个开始我面临问题 当我点击第三个例如它没有显示显示状态。

有谁能告诉我问题在哪里。 这是代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Satis extends JFrame implements ActionListener
{
JCheckBox b1,b2,b3,b4,b5,b6,b7,b8;

  JLabel la2,la22,la222,la3,la33,la333,la4,la44,la444,la5,la55,la555,la6,la66,la666,la7,la77,la777,la8,la88,la888,la9,la99,la999;
public Satis()
{
     this.setLayout(null);

      JLabel la1=new JLabel("Select your problem area to get an advice:");
      la1.setBounds(160,20,340,20);
       la2=new JLabel("");
       la22=new JLabel("");
       la222=new JLabel("");
       la3=new JLabel("");
       la33=new JLabel("");
       la333=new JLabel("");
       la4=new JLabel("");
       la44=new JLabel("");
       la444=new JLabel("");
       la5=new JLabel("");
       la55=new JLabel("");
       la555=new JLabel("");
       la6=new JLabel("");
       la66=new JLabel("");
       la666=new JLabel("");
       la7=new JLabel("");
       la77=new JLabel("");
       la777=new JLabel("");
       la8=new JLabel("");
       la88=new JLabel("");
       la888=new JLabel("");
       la9=new JLabel("");
       la99=new JLabel("");
       la999=new JLabel("");


      b1=new JCheckBox("Housing");
      b1.setBounds(20,45,120,20);
      b2=new JCheckBox("Medical");
      b2.setBounds(440,45,120,20);
      b3=new JCheckBox("Transportation");
      b3.setBounds(20,120,140,20);
      b4=new JCheckBox("Food");
      b4.setBounds(440,120,140,20);
      b5=new JCheckBox("Utility");
      b6=new JCheckBox("Clothes");
      b7=new JCheckBox("Debt payments");
      b8=new JCheckBox("Personal");

      add(la1);
      add(la2);
      add(la22);
      add(la222);
      la2.setBounds(10,60,330,20);
      la22.setBounds(10,75,330,20);
      la222.setBounds(10,90,330,20);
      add(b1);

      add(la3);
      add(la33);
      add(la333);
      la3.setBounds(300,60,330,20);
      la33.setBounds(300,75,330,20);
      la333.setBounds(300,90,330,20);
      add(b2);

      add(la4);
      add(la44);
      add(la444);
      la4.setBounds(10,135,330,20);
      la44.setBounds(10,150,330,20);
      la444.setBounds(10,165,330,20);
      add(b3);

      add(la5);
      add(la55);
      add(la555);
      la5.setBounds(330,135,330,20);
      la5.setBounds(330,150,330,20);
      la5.setBounds(330,165,330,20);

      add(b4);

      add(la6);
      add(b5);

      add(la7);
      add(b6);

      add(la8);
      add(b7);

      add(la9);
      add(b8);


      b1.addActionListener(this);
      b2.addActionListener(this);
}
public void actionPerformed (ActionEvent ae)
{
    String s=ae.getActionCommand();
    if (b1.isSelected())
    {
        la2.setText("1. Move to a smaller place "); 
        la22.setText("2. Move further from the city");
        la222.setText("3. Raise your insurance deductible");
    }else {
        la2.setText("");
        la22.setText("");
        la222.setText("");

    }


    if (b2.isSelected())
        {
            la3.setText("1. Dont procrastinate");
            la33.setText("2. Reevaluate your vitamins");
            la333.setText("3. Ask: Do I really need that test?");
        }
            else {
                la3.setText("");
    la33.setText("");
    la333.setText("");
            }

if (b3.isSelected())
    {

        la4.setText("1. Calm your driving habits");
        la44.setText("2. Take advantage of car insurance discounts");
        la444.setText("3. Look for local transportation incentives");
    }
        else {
            la4.setText("");
la44.setText("");
la444.setText("");
        }
    if (b4.isSelected())
    {
        la5.setText("1. Make Healthy Choices, they’re Cheaper ");
        la55.setText("2. Use Sales and Coupons");
        la555.setText("3. Plant a Garden");
    }
        else {
            la5.setText("");
            la55.setText("");
            la555.setText("");
        }

    if (b5.isSelected())
    {
        la6.setText("1. Buy Energy-Efficient Appliances ");
        la66.setText("2. Scale Back on Cable Channels");
        la666.setText("3. Fix Leaks");
    }
        else {
            la6.setText("");
            la66.setText("");
            la666.setText("");
        }
    if (b6.isSelected())
    {
        la7.setText("1. Buy Better Quality Clothing ");
        la77.setText("2. Buy Clothes at the Right Time");
        la777.setText("3. Take Care of Your Clothes Better");
    }
        else {
            la7.setText("");
            la77.setText("");
            la777.setText("");
        }
    if (b7.isSelected())
    {
        la8.setText("1. Track Spending ");
        la88.setText("2. Build Savings");
        la888.setText("3. Reduce Debt");
    }
        else {
            la8.setText("");
            la88.setText("");
            la888.setText("");
        }
    if (b8.isSelected())
    {
        la9.setText("1. Avoid Full-Price Textbooks ");
        la99.setText("2. Get deals");
        la999.setText("3. Shop online");
    }
        else {
            la9.setText("");
            la99.setText("");
            la999.setText("");
        }






    }
public static void main(String[] args)
{
    Satis St=new Satis();
    St.setVisible(true);
    St.setSize(600,400);
}

}

1 个答案:

答案 0 :(得分:0)

您忘了添加

   b3.addActionListener(this);

因此,在使用JCheckBox时,永远不会调用该方法。