否则如果语句Java Gui不会执行
我一直在尝试制作一个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);
}
}
}
}
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果... else语句托架匹配存在错误。如果面对此类型的问题,请使用VSCODE(任何其他IDE)括号配色扩展程序。顺便说一句,我纠正了错误,请查看以下代码。
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.
从我的看来,如果层次结构有错。
您的IS:
应该是:
您会发现与适当的代码格式的错误。
From what I see, you have a wrong if hierarchy.
Your is:
It should be:
You would have spotted the mistake withg proper code formatting.