任何帮助都会非常感激,因为我是一个绝对的初学者。我完全不知道我的计划有什么问题。它如下:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class TempConvert {
private JRadioButton jc1, jf1, jk1, jc2, jf2, jk2;
private JTextField in, out;
public TempConvert(){
JFrame frame = new JFrame("Align");
JPanel p1 = new JPanel(new GridLayout(4, 1));
p1.add(new JLabel("Input Scale"));
p1.add(jc1 = new JRadioButton("Celsius"));
p1.add(jf1 = new JRadioButton("Fahrenheit"));
p1.add(jk1 = new JRadioButton("Kelvin"));
ButtonGroup inGroup = new ButtonGroup();
inGroup.add(jc1);
inGroup.add(jf1);
inGroup.add(jk1);
JPanel p2 = new JPanel(new GridLayout(4,1));
p2.add(new JLabel("Output Scale"));
p2.add(jc2 = new JRadioButton("Celsius"));
p2.add(jf2 = new JRadioButton("Fahrenheit"));
p2.add(jk2 = new JRadioButton("Kelvin"));
ButtonGroup outGroup = new ButtonGroup();
outGroup.add(jc2);
outGroup.add(jf2);
outGroup.add(jk2);
JPanel p3 = new JPanel(new FlowLayout());
p3.add(new JLabel("Input"));
p3.add(in = new JTextField(10));
JPanel p4 = new JPanel(new FlowLayout());
p4.add(new JLabel("Output"));
p4.add(out = new JTextField(10));
out.setEditable(false);
ActionEventHandler hdlr = new ActionEventHandler();
jc1.addActionListener(hdlr);
jf1.addActionListener(hdlr);
jk1.addActionListener(hdlr);
jc2.addActionListener(hdlr);
jf2.addActionListener(hdlr);
jk2.addActionListener(hdlr);
in.addActionListener(hdlr);
JPanel full = new JPanel(new BorderLayout());
full.add(p1, BorderLayout.WEST);
full.add(p2, BorderLayout.EAST);
full.add(p3, BorderLayout.NORTH);
full.add(p4, BorderLayout.SOUTH);
frame.add(full);
frame.setSize(400, 200);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
private class ActionEventHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
ConvMeth conObj = new ConvMeth();
String input = "";
input = in.getText();
double temp = Double.parseDouble(input);
if(e.getSource() == in){
if(input == ""){
out.setText(String.format("No Input"));
}
else{
if(e.getSource() == jc1){
if(e.getSource() == jc2){
out.setText(String.format("%.2f", temp));
}
else if(e.getSource() == jf2){
conObj.celfar(temp);
out.setText(String.format("%.2f", temp));
}
else if(e.getSource() == jk2){
conObj.celkel(temp);
out.setText(String.format("%.2f", temp));
}
}
else if(e.getSource() == jf1){
if(e.getSource() == jf2){
out.setText(String.format("%.2f", temp));
}
else if(e.getSource() == jc2){
conObj.farcel(temp);
out.setText(String.format("%.2f", temp));
}
else if(e.getSource() == jk2){
conObj.farkel(temp);
out.setText(String.format("%.2f", temp));
}
}
else if(e.getSource() == jk1){
if(e.getSource() == jk2){
out.setText(String.format("%.2f", temp));
}
else if(e.getSource() == jf2){
conObj.kelfar(temp);
out.setText(String.format("%.2f", temp));
}
else if(e.getSource() == jc2){
conObj.kelcel(temp);
out.setText(String.format("%.2f", temp));
}
}
}
}
}
}
public static void main(String[] args) {
TempConvert myTempConvert = new TempConvert();
}
}
我尝试点击按钮时收到的错误是:
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at sun.misc.FloatingDecimal.parseDouble(Unknown Source)
at java.lang.Double.parseDouble(Unknown Source)
at TempConvert$ActionEventHandler.actionPerformed(TempConvert.java:69)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.JToggleButton$ToggleButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
同时在文本字段中输入任何内容,同时不再出现错误,也不执行任何操作。