else if 语句 java gui 不会执行

else if statement java gui wont execute

我一直在尝试制作一个 java GUI 距离转换器。我的 else if 语句不会执行,只有第一个 if 语句会执行。有谁知道我为什么不执行?下面是我的编码和输出。

import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.*;



public class DistanceCalculator implements ActionListener  {
    private JFrame frame;
    private JLabel lbl1;
    private JTextField dtextfield;
    private JRadioButton cm,cf,ci;
    private JButton bcalculate,bclose;
    private JPanel centerp,southp,northp;
    
    
    DistanceCalculator(){
        frame = new JFrame("Distance Converter" );
        frame.setSize(400, 150);
        init();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        
    }
    
    void init() {
        northp = new JPanel();
        northp.setLayout(new FlowLayout());
        
        centerp = new JPanel();
        centerp.setLayout(new FlowLayout());
        
        southp = new JPanel();
        southp.setLayout(new FlowLayout());
        
        lbl1 = new JLabel("Enter a distance in kilometers");
        northp.add(lbl1);
            
        dtextfield = new JTextField(15);
        northp.add(dtextfield);
        
        cm = new JRadioButton("Convert to miles");
        centerp.add(cm);
        
        cf = new JRadioButton("Convert to feet");
        centerp.add(cf);
        
        ci = new JRadioButton("Convert to inches");
        centerp.add(ci);
        
        ButtonGroup group = new ButtonGroup();
        group.add(cm);
        group.add(cf);
        group.add(ci);
        
        bcalculate = new JButton("Calculate");
        bcalculate.addActionListener(this);
        southp.add(bcalculate);
        
        bclose = new JButton("Close");
        bclose.addActionListener(this);
        southp.add(bclose);
        
        frame.add(centerp, BorderLayout.CENTER);
        frame.add(southp, BorderLayout.SOUTH);
        frame.add(northp,BorderLayout.NORTH);
        
    }
    public static void main(String[] args) {
        DistanceCalculator gui = new DistanceCalculator();
    }
    
    public void actionPerformed(ActionEvent e) {
        if (cm.isSelected() ) {
            if (e.getSource() == bcalculate ){
                double d = Double.parseDouble(dtextfield.getText());
                double d2 =  d* 0.6214;
                String str2 = String.valueOf(d2); 

                JOptionPane.showMessageDialog(null,d+"\tKilometer is\t" + (str2) + "\tMiles");
                
            } else if(cf.isSelected()) { 
                 if (e.getSource() == bcalculate ){
                     double dd = Double.parseDouble(dtextfield.getText());
                     double d22 =  dd* 3281.0;
                     String str22 = String.valueOf(d22); 

                     JOptionPane.showMessageDialog(null,dd+"\tKilometer is\t" + (str22) + "\tFeet");
                
                
                
            }
        }
            else if(ci.isSelected()) { 
             if (e.getSource() == bcalculate ){
                    double ddd = Double.parseDouble(dtextfield.getText());
                    double d222 =  ddd* 3281.0;
                    String str222 = String.valueOf(d222); 

                    JOptionPane.showMessageDialog(null,ddd+"\tKilometer is\t" + (str222) + "\tInches");
            }
            }
            else {
                System.exit(0);
                
            }
        
        }
    }
}

下面是当我检查第二个和第三个单选按钮时得到的输出,输入一些数字并单击提交,jOptionPane 不会出现

据我所知,您的层次结构有误。 你是:

if (cm.isSelected() ) {
    if (e.getSource() == bcalculate ) {
    } else if (cf.isSelected()) { 
        if (e.getSource() == bcalculate ) {   
        }
    } else if (ci.isSelected()) { 
        if (e.getSource() == bcalculate) {
        }
    } else {
    }
}

应该是:

if (cm.isSelected() ) {
    if (e.getSource() == bcalculate ) {
    }
} else if (cf.isSelected()) {
    if (e.getSource() == bcalculate ) {   
    }
} else if (ci.isSelected()) { 
    if (e.getSource() == bcalculate) {
    }
} else {
}

如果使用正确的代码格式,您会发现错误。

if...else 语句括号匹配有误。如果您遇到此类问题,请使用 VScode 的(任何其他 IDE 的)Bracket Pair Colorizer 扩展。对了,我已经更正了错误,请看下面的代码。

import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.*;

public class Main11 implements ActionListener  {
    private JFrame frame;
    private JLabel lbl1;
    private JTextField dtextfield;
    private JRadioButton cm,cf,ci;
    private JButton bcalculate,bclose;
    private JPanel centerp,southp,northp;
    
    
    Main11(){
        frame = new JFrame("Distance Converter" );
        frame.setSize(400, 150);
        init();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        
    }
    
    void init() {
        northp = new JPanel();
        northp.setLayout(new FlowLayout());
        
        centerp = new JPanel();
        centerp.setLayout(new FlowLayout());
        
        southp = new JPanel();
        southp.setLayout(new FlowLayout());
        
        lbl1 = new JLabel("Enter a distance in kilometers");
        northp.add(lbl1);
            
        dtextfield = new JTextField(15);
        northp.add(dtextfield);
        
        cm = new JRadioButton("Convert to miles");
        centerp.add(cm);
        
        cf = new JRadioButton("Convert to feet");
        centerp.add(cf);
        
        ci = new JRadioButton("Convert to inches");
        centerp.add(ci);
        
        ButtonGroup group = new ButtonGroup();
        group.add(cm);
        group.add(cf);
        group.add(ci);
        
        bcalculate = new JButton("Calculate");
        bcalculate.addActionListener(this);
        southp.add(bcalculate);
        
        bclose = new JButton("Close");
        bclose.addActionListener(this);
        southp.add(bclose);
        
        frame.add(centerp, BorderLayout.CENTER);
        frame.add(southp, BorderLayout.SOUTH);
        frame.add(northp,BorderLayout.NORTH);
        
    }
    public static void main(String[] args) {
        Main11 gui = new Main11();
    }
    
    public void actionPerformed(ActionEvent e) {
        if (cm.isSelected() ) 
        {
            if (e.getSource() == bcalculate )
            {
                double d = Double.parseDouble(dtextfield.getText());
                double d2 =  d* 0.6214;
                String str2 = String.valueOf(d2); 

                JOptionPane.showMessageDialog(null,d+"\tKilometer is\t" + (str2) + "\tMiles");
                
            }
        }
        else if(cf.isSelected()) 
        { 
                 if (e.getSource() == bcalculate ){
                     double dd = Double.parseDouble(dtextfield.getText());
                     double d22 =  dd* 3281.0;
                     String str22 = String.valueOf(d22); 

                     JOptionPane.showMessageDialog(null,dd+"\tKilometer is\t" + (str22) + "\tFeet");        
            }
        }
        else if(ci.isSelected()) 
        { 
            if (e.getSource() == bcalculate )
            {
                    double ddd = Double.parseDouble(dtextfield.getText());
                    double d222 =  ddd* 3281.0;
                    String str222 = String.valueOf(d222); 

                    JOptionPane.showMessageDialog(null,ddd+"\tKilometer is\t" + (str222) + "\tInches");
            }
        }
        else 
        {
            System.exit(0);
                
        }       
    }
}