Swing Warum erhalte ich keine Ausgabe? Funktioniert der equals-vergleich in actionPeformed nicht richtig?

De

Neues Mitglied
Java:
 import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class IdealWeight extends JFrame implements ActionListener
{

    JRadioButton genderM = new JRadioButton("Male", false);
    JRadioButton genderF = new JRadioButton("Female", true);
    ButtonGroup genderGroup = new ButtonGroup();
    JPanel genderPanel = new JPanel();
    JLabel genderLabel = new JLabel("Your Gender");

    JRadioButton inch1 = new JRadioButton("60 to 64 inches", false);
    JRadioButton inch2 = new JRadioButton("64 to 68 inches", false);
    JRadioButton inch3 = new JRadioButton("68 to 72 inches", true);
    JRadioButton inch4 = new JRadioButton("72 to 76 inches", false);
    JRadioButton inch5 = new JRadioButton("76 to 80 inches", false);
    ButtonGroup inchGroup = new ButtonGroup();
    JPanel inchPanel = new JPanel();
    JLabel inchLabel = new JLabel("Your Height");

    JPanel weightPanel = new JPanel();
    JTextField weightField = new JTextField(10);
    JLabel weightLabel = new JLabel("Ideal Weight");

    public IdealWeight()
    {
        

        genderM.addActionListener(this);
        genderM.setActionCommand("genderM");
        genderF.addActionListener(this);
        genderF.setActionCommand("genderF");

        genderGroup.add(genderM);
        genderGroup.add(genderF);

        genderPanel.setLayout(new BoxLayout(genderPanel, BoxLayout.Y_AXIS));

        genderPanel.add(genderLabel);
        genderPanel.add(genderM);
        genderPanel.add(genderF);

       

        inch1.addActionListener(this);
        inch2.addActionListener(this);
        inch3.addActionListener(this);
        inch4.addActionListener(this);
        inch5.addActionListener(this);
        inch1.setActionCommand("inch1");
        inch2.setActionCommand("inch2");
        inch3.setActionCommand("inch3");
        inch4.setActionCommand("inch4");
        inch5.setActionCommand("inch5");

        inchGroup.add(inch1);
        inchGroup.add(inch2);
        inchGroup.add(inch3);
        inchGroup.add(inch4);
        inchGroup.add(inch5);

        inchPanel.setLayout(new BoxLayout(inchPanel, BoxLayout.Y_AXIS));

        inchPanel.add(inchLabel);
        inchPanel.add(inch1);
        inchPanel.add(inch2);
        inchPanel.add(inch3);
        inchPanel.add(inch4);
        inchPanel.add(inch5);

        

        weightPanel.setLayout(new BoxLayout(weightPanel, BoxLayout.X_AXIS));

        weightPanel.add(weightLabel);
        weightPanel.add(weightField);

        // Frame, verwendet BoarderLayout als Standard

        add(genderPanel, BorderLayout.WEST);
        add(inchPanel, BorderLayout.EAST);
        add(weightPanel, BorderLayout.SOUTH);
    }

    public void actionPerformed(ActionEvent event)

    {
        if ((event.getActionCommand()).equals("genderM"))
        {
            
            if (event.getActionCommand().equals("inch1"))
            {
            
                weightField.setText(" 60 kg ");
                repaint();
            }
            else if (event.getActionCommand().equals("inch2")) {
                weightField.setText(" 80 kg ");
                repaint();
            }

            else if (event.getActionCommand().equals("inch3")) {
                weightField.setText(" 90 kg ");
                repaint();
            }

            else if (event.getActionCommand().equals("inch4")) {
                weightField.setText(" 100 kg ");
                repaint();
            }
            else if (event.getActionCommand().equals("inch5")) {
                weightField.setText(" 190 kg ");
                repaint();
            }

        }

    
     else if (event.getActionCommand().equals("genderF"))
     {
     
     if (event.getActionCommand().equals("inch1"))
     {
      weightField.setText(" 50 kg "); 
     } 
     else if (event.getActionCommand().equals("inch2")) 
     {
      weightField.setText(" 70 kg "); 
      }
      
      else if (event.getActionCommand().equals("inch3")) 
      {
      weightField.setText(" 80 kg "); 
      }
      
      else if (event.getActionCommand().equals("inch4")) 
      {
      weightField.setText(" 90 kg "); 
      } else if
      (event.getActionCommand().equals("inch5")) {
      weightField.setText(" 100 kg "); } }
      
      }
     

    public static void main(String[] args)
    {
        IdealWeight weightApp = new IdealWeight();
        weightApp.setSize(350, 325);
        weightApp.setResizable(false);
        weightApp.setVisible(true);
    }
}
 
[Java]
if ((event.getActionCommand()).equals("genderM"))
{
if (event.getActionCommand().equals("inch1"))
{
[/Java]

wie soll das gleichzeitig wahr sein?
 
es sollen ja immer zwei RadioButtons gleichzeitig wahr sein(einer fürs Geschlecht und einer für die Größe) . Wie mache ich es denn, dass die Methode die inch-Buttons in Abhängigkeit der gender-Buttons anzeigt?
 
Das Action-Command brauchst du nicht wirklich, frag einfach die Source ab, die geklickt wurde -> if (event,getSource().equals(inch5)) { ... } usw.
 

Neue Themen


Zurück
Oben