私の問題は、ラジオ ボタンを選択すると、Java ターミナルで大きなエラーが発生することです。最初のボタン (ボタン 1 からボタン 4) にあります。に関し
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String
ラジオボタンは以前は機能していましたが、チェックボックスメソッドを作成した後は機能しなくなりましたが、チェックボックスの選択は機能します (ただし、クリアされません)。エラーが見つかりました、これに属していました
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;
}
}
}
}
}