Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 超过两个JCheckBox?_Java_Swing_Checkbox_Jcheckbox - Fatal编程技术网

Java 超过两个JCheckBox?

Java 超过两个JCheckBox?,java,swing,checkbox,jcheckbox,Java,Swing,Checkbox,Jcheckbox,我想在我的程序8复选框,其中每一个如果点击将有语句 在前两个复选框中效果很好,但从第三个复选框开始,我就遇到了问题 例如,当我单击第三个时,它没有显示statemnts 谁能告诉我哪里出了问题。 代码如下: import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Satis extends JFrame implements ActionListener { JCheckBox b1,b2,b3

我想在我的程序8复选框,其中每一个如果点击将有语句 在前两个复选框中效果很好,但从第三个复选框开始,我就遇到了问题 例如,当我单击第三个时,它没有显示statemnts

谁能告诉我哪里出了问题。 代码如下:

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);
}

}
你忘了加上

   b3.addActionListener(this);

因此,当您使用JCheckBox时,该方法永远不会被调用

您的前两个JCheckbox添加了适当的侦听器,因此您已经知道如何做到这一点——那么您为什么要问这个问题呢?1)Java GUI必须在不同的操作系统、屏幕大小、屏幕分辨率等上工作,在不同的地区使用不同的PLAF。因此,它们不利于像素完美布局。而是使用布局管理器,或与布局填充和边框一起使用。2) 为了更快地获得更好的帮助,请发布一个or。请注意,MCVE将有3个复选框,而不是8个复选框。