2016-12-11 22 views
-1

Ich möchte in meinem Programm 8 Checkboxen, und jedes von ihnen, wenn es angeklickt wird werden Anweisungen Es funktioniert gut mit den ersten beiden Checkboxen, aber ab dem 3. Ich konfrontiert Problem , wenn ich zum Beispiel auf den 3. klicken Es zeigte nicht die Aussagen.mehr als zwei JCheckBox?

Kann mir bitte jemand sagen, wo das Problem ist. Hier ist der Code:

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

} 
+0

Ihre ersten beiden JCheckBoxen haben entsprechende Listener hinzugefügt, also wissen Sie bereits, wie Sie das tun - warum stellen Sie diese Frage dann? –

+0

1) Java GUIs müssen auf verschiedenen Betriebssystemen, Bildschirmgrößen, Bildschirmauflösungen usw. mit verschiedenen PLAFs in verschiedenen Locales arbeiten. Als solche sind sie nicht für ein perfektes Pixel-Layout geeignet. Verwenden Sie stattdessen Layout-Manager oder [Kombinationen davon] (http://stackoverflow.com/a/5630271/418556) zusammen mit Layout-Auffüllung und Rahmen für [Leerraum] (http://stackoverflow.com/a/17874718/ 418556). 2) Um eine bessere Hilfe zu erhalten, sollten Sie ein [MCVE] oder [Short, Self Contained, Correct Example] (http://www.sscce.org/) veröffentlichen. Beachten Sie, dass ein MCVE 3 Kontrollkästchen anstelle von 8 hätte. –

Antwort

0

Sie haben vergessen

b3.addActionListener(this); 

So ist die Methode hinzufügen wird nie aufrufen, wenn Sie die JCheckBox verwenden.