簡體   English   中英

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM