否则如果语句Java Gui不会执行

发布于 2025-01-29 02:19:34 字数 3675 浏览 2 评论 0原文

我一直在尝试制作一个Java GUI距离转换器。我的否则语句不会执行,仅执行第一个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不会出现

I have been trying to make a java GUI distance converter. My else if statement won't execute, only the first if statement execute. Does anyone know why i wont executed? Below is my coding and the output.

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

Below is the output that im getting when i check the second and third radio button,input some number and clicked submit, the jOptionPane wont appear
Output

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

久伴你 2025-02-05 02:19:34

如果... else语句托架匹配存在错误。如果面对此类型的问题,请使用VSCODE(任何其他IDE)括号配色扩展程序。顺便说一句,我纠正了错误,请查看以下代码。

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

There was a mistake in the if...else statement bracket matching. use VScode's (any other IDE's) Bracket Pair Colorizer extension if you face this type of issue. by the way, I have corrected the mistake, kindly take a look at the following code.

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);
                
        }       
    }
}
皇甫轩 2025-02-05 02:19:34

从我的看来,如果层次结构有错。
您的IS:

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 {
}

您会发现与适当的代码格式的错误。

From what I see, you have a wrong if hierarchy.
Your is:

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

It should be:

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

You would have spotted the mistake withg proper code formatting.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文