我的问题是当我选择它时,我的单选按钮会在java终端中导致巨大的错误。它是在开始按钮(按钮1到按钮4)按钮1启动加油站(所以你可以选择你想要的气体类型),但它没有打开单选按钮可供选择,它给了我一堆错误,与
有关Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String
radiobuttons之前工作过,但在我做了复选框方法后它停止工作,但复选框选择工作(但不清除)。 发现错误,它属于这个
litres=Double.parseDouble(gasL.getText());
所以我暂时删除了那个部分,但不知道如何让它工作,因为我试图从用户可以输入的文本框中获取升。气杆仍然无法打开
清除工作对于复选框,但我不确定单选按钮,这给了我错误
这是我的代码
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
* A GUI for a Gas Bar, which allows you to choose the gas type you want, and optional car checks for things like
* windshield and gas check. The price is outputted afterwards.
*
* @author Kevin Hy
* @date 08/06/13
*/
public class New_Gas_Bar extends JFrame
{
public JPanel panel1,panel2,panel3,panel4,panel5;
public JLabel main1, main2, main3,main4,main5,main6;
public JLabel gasBar,total;
public JButton button1,button2,button3,button4,clearGas,clearCar;
public JRadioButton bronzeG,silverG,goldG,selfS,fullS,fullService,selfService;
public JCheckBox oilC,windWash,pressureClock;
public JTextField gasL;
static double fullCost,selfCost,discount,gasCost,litres,cleaningC;
static boolean boughtGas=false;
public New_Gas_Bar()
{
super (" New Gas Bar");
setSize(640,640);
Container container = getContentPane();
panel1=new JPanel();
panel2=new JPanel();
panel3=new JPanel();
panel4=new JPanel();
panel5=new JPanel();
panel1.setBackground(new Color(107,202,226));
panel1.setLayout(new GridLayout(6,1));
main1 = new JLabel (" Gas Bar Project ");
main2 = new JLabel (" ICS4U0 - September 2013");
main3 = new JLabel (" ");
main4 = new JLabel ("Enter Litres of Gas");
main5 = new JLabel ("Select Full or Self Serve");
main6 = new JLabel ("Select Gas Quality");
button1 = new JButton ("Gas Station");
button2 = new JButton ("Car Wash");
button3 = new JButton ("Total");
button4 = new JButton ("Exit");
clearCar = new JButton ("Clear");
clearGas = new JButton ("Clear");
panel2.setBackground(new Color(144,160,170));
panel2.setLayout(new FlowLayout ());
panel2.add (button1);
panel2.add (button2);
panel2.add (button3);
panel2.add (button4);
panel1.add (main1);
panel1.add (main2);
panel1.add (main3);
//container.setLayout(new BorderLayout());
container.add(panel1,BorderLayout.NORTH);
container.add(panel2,BorderLayout.CENTER);
container.add(panel3,BorderLayout.EAST);
container.add(panel4,BorderLayout.EAST);
panel3.setBackground(new Color(107,202,226));
panel4.setBackground(new Color(107,202,226));
ButtonHandler handler = new ButtonHandler ();
button1.addActionListener (handler);
button2.addActionListener (handler);
button3.addActionListener (handler);
button4.addActionListener (handler);
clearCar.addActionListener (handler);
clearGas.addActionListener (handler);
ButtonGroup serveStyle = new ButtonGroup();
fullS = new JRadioButton ("Full Serve");
selfS = new JRadioButton ("Self Serve");
serveStyle.add (fullS);
serveStyle.add (selfS);
ButtonGroup serveGas = new ButtonGroup();
bronzeG = new JRadioButton("Bronze Service");
silverG = new JRadioButton("Silver Service");
goldG = new JRadioButton("Gold Service");
serveGas.add (bronzeG);
serveGas.add (silverG);
serveGas.add (goldG);
gasL = new JTextField(5);
oilC = new JCheckBox("Oil Change");
windWash = new JCheckBox("Windshield Wash");
pressureClock = new JCheckBox("Air Pressure Check");
RadioButtonHandler radioHand = new RadioButtonHandler ();
bronzeG.addItemListener (radioHand);
silverG.addItemListener (radioHand);
goldG.addItemListener (radioHand);
fullS.addItemListener (radioHand);
selfS.addItemListener (radioHand);
CheckBoxHandler checkHand = new CheckBoxHandler();
oilC.addItemListener (checkHand);
windWash.addItemListener (checkHand);
pressureClock.addItemListener (checkHand);
setVisible(true);
}
public static void main (String [] args)
{
New_Gas_Bar application = new New_Gas_Bar();
}
public class ButtonHandler implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
if (event.getSource () == button1)
{
panel1.setVisible(true);
panel2.setVisible(true);
panel3.setVisible(true);
panel4.setVisible(false);
panel3.setLayout(new GridLayout(10,1));
panel3.add(clearGas);
panel3.add(main5);
panel3.add(fullS);
panel3.add(selfS);
panel3.add(main6);
panel3.add(bronzeG);
panel3.add(silverG);
panel3.add(goldG);
panel3.add(main4);
panel3.add(gasL);
litres=Double.parseDouble(gasL.getText());
//JOptionPane.showMessageDialog (null, litres);
}
else if (event.getSource () == button2)
{
panel1.setVisible(true);
panel2.setVisible(true);
panel3.setVisible(false);
panel4.setVisible(true);
panel4.setLayout(new FlowLayout ());
panel4.add(clearCar);
panel4.add(oilC);
panel4.add(windWash);
panel4.add(pressureClock);
}
else if (event.getSource () == button3)
{
JOptionPane.showMessageDialog (null, "This is your total:");
JOptionPane.showMessageDialog (null, litres);
}
else if (event.getSource () == button4)
{
System.exit(0);
}
if (event.getSource () == clearGas)
{
JOptionPane.showMessageDialog (null, "Clearing All Selected Options");
bronzeG.setSelected(false);
silverG.setSelected(false);
goldG.setSelected(false);
fullS.setSelected(false);
selfS.setSelected(false);
gasCost=0;
boughtGas=false;
}
if (event.getSource () == clearCar)
{
JOptionPane.showMessageDialog (null, "Clearing All Selected Options");
pressureClock.setSelected(false);
windWash.setSelected(false);
oilC.setSelected(false);
cleaningC=0;
}
}
}
public class RadioButtonHandler implements ItemListener
{
public void itemStateChanged(ItemEvent event)
{
if (event.getSource () == fullS)
{
JOptionPane.showMessageDialog (null, "Full Serve Selected");
fullCost =1.25;
boughtGas=true;
}
else if (event.getSource () == selfS)
{
JOptionPane.showMessageDialog (null, "Self Serve Selected");
selfCost =0.60;
boughtGas=true;
}
if (event.getSource () == bronzeG)
{
JOptionPane.showMessageDialog (null, "Bronze Gas Option Selected");
gasCost=0.50;
boughtGas=true;
}
if (event.getSource () == silverG)
{
JOptionPane.showMessageDialog (null, "Silver Gas Option Selected");
gasCost=0.75;
boughtGas=true;
}
if (event.getSource () == goldG)
{
JOptionPane.showMessageDialog (null, "Gold Gas Option Selected");
gasCost=0.95;
boughtGas=true;
}
}
}
public class CheckBoxHandler implements ItemListener
{
public void itemStateChanged (ItemEvent event)
{
if (event.getSource () == oilC)
{
JOptionPane.showMessageDialog (null, "Oil Cleaning Selected");
if (boughtGas==true)
{
cleaningC+=10.00;
}
else
{
cleaningC+=12.50;
}
}
if (event.getSource () == windWash)
{
JOptionPane.showMessageDialog (null, "Windshield Washing Selected");
if (boughtGas==true)
{
cleaningC+=10.00;
}
else
{
cleaningC+=12.50;
}
}
if (event.getSource () == pressureClock)
{
JOptionPane.showMessageDialog (null, "Air Pressure Check Selected");
if (boughtGas==true)
{
cleaningC+=10.00;
}
else
{
cleaningC+=12.50;
}
}
}
}
}
答案 0 :(得分:0)
看一下你得到的错误。它说NumberFormatException。我相信问题在于你的Double.parseDouble(gasL.getText())。当gasL留空时会发生什么?程序如何将空字符串解析为double?尝试将gasL的默认值设置为0或查看try / catch以处理异常。
gasL = new JTextField("0",5);
这会删除错误消息,但我无法弄清楚为什么你的加油站面板没有打开,对不起。
答案 1 :(得分:0)
我认为最好不使用以下方法:Double.parseDouble()直接。
当然,您不希望开发自己的方法来解析双打等等。
我喜欢做的是将空字符串甚至空值映射为零。
public static double myParseDouble(String s) {
if (s == null || s.equals("")) {
return 0.0;
} else {
return Double.parseDouble(s);
}
}
在解析int,float,long和BigIntegers时,你必须做同样的事情。
我建议您在自己的项目中使用我的方法,并“使用它”,通过它运行各种字符串并查看它的作用。
如果您需要更多有关本练习或其他人的帮助,可以发送电子邮件至:kaydell@yahoo.com
我在编程教学方面取得的最大成功是使用比论坛和电子邮件更具互动性的软件。我喜欢使用聊天和屏幕共享,我可以在屏幕上演示如何做某事,然后学生可以在我教练时做类似的事情。