2013-12-16 91 views
0

我的代碼在下面做了我想要的一切,但它缺少一個關鍵功能。我希望它取得每個方塊的值並將其顯示在輸入框中。然後當用戶按總數時,它顯示最終價格加7%的稅。目前,這只是一個錯誤,但在將這些數額放在整個盒子中之前並未實際添加它們。我怎樣才能讓它正確地添加它們?謝謝!這裏是我的2類文件:在GUI中累計金額

import javax.swing.BorderFactory; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JTabbedPane; 
public class PizzaOrder 
{ 

    private JPanel phone; 

    public static void main (String[] args) 
    { 

    JFrame frame = new JFrame ("Pizza Order"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    JPanel phone = new JPanel(); 
    phone.setBorder (BorderFactory.createRaisedBevelBorder()); 

    JTabbedPane tp = new JTabbedPane(); 
    tp.addTab ("Pizza Order", new PizzaGUI()); 

    frame.getContentPane().add(phone); 
    frame.getContentPane().add(tp); 
    frame.pack(); 
    frame.pack();frame.setVisible(true); 

    } 
} 


import java.awt.*; 
import java.awt.event.*; 

import javax.swing.*; 


public class PizzaGUI extends JPanel 
{ 
    private JLabel resultLabel; 


    private JButton button0, button1, button2, button3, button4, button5, button6, button7, button8, button9, buttonclear; 

    public PizzaGUI() 
    { 
     // grid layout 
     setLayout (new GridLayout (5, 3)); 

     // creates buttons and label 
     resultLabel = new JLabel (" "); 
     button0 = new JButton ("Small: 10.00"); 
     button1 = new JButton ("Medium: 14.00"); 
     button2 = new JButton ("Large: 16.00"); 
     button3 = new JButton ("Soda: 2.00"); 
     button4 = new JButton ("Water: 1.50"); 
     button5 = new JButton ("Extra Cheese: 1.50"); 
     button6 = new JButton ("Pepperoni: 1.50"); 
     button7 = new JButton ("Mushroom: 1.50"); 
     button8 = new JButton ("Sausage: 1.50"); 
     button9 = new JButton ("Pepper Onion: 1.50"); 
     buttonclear = new JButton ("Total"); 

     //listeners for each button 
     button0.addActionListener (new ButtonListener0()); 
     button1.addActionListener (new ButtonListener1()); 
     button2.addActionListener (new ButtonListener2()); 
     button3.addActionListener (new ButtonListener3()); 
     button4.addActionListener (new ButtonListener4()); 
     button5.addActionListener (new ButtonListener5()); 
     button6.addActionListener (new ButtonListener6()); 
     button7.addActionListener (new ButtonListener7()); 
     button8.addActionListener (new ButtonListener8()); 
     button9.addActionListener (new ButtonListener9()); 
     buttonclear.addActionListener(new ButtonListenerT()); 

     //adds all buttons and label 
     add (resultLabel); 
     add (button0); 
     add (button1); 
     add (button2); 
     add (button3); 
     add (button4); 
     add (button5); 
     add (button6); 
     add (button7); 
     add (button8); 
     add (button9); 
     add (buttonclear); 


     setPreferredSize (new Dimension(720,360)); 
     setBackground (Color.white); 
    } 

    //code below is one listener per button 
    private class ButtonListener0 implements ActionListener 
    { 
     public void actionPerformed (ActionEvent event) 
     { 


      resultLabel.setText(resultLabel.getText() + "10.00"); 
     } 
    } 


    private class ButtonListener1 implements ActionListener 
    { 
     public void actionPerformed (ActionEvent event) 
     { 



      resultLabel.setText(resultLabel.getText() + "14.00"); 
     } 
    } 

    private class ButtonListener2 implements ActionListener 
    { 
     public void actionPerformed (ActionEvent event) 
     { 


      resultLabel.setText(resultLabel.getText() + "16.00");  
     } 
    } 


    private class ButtonListener3 implements ActionListener 
    { 
     public void actionPerformed (ActionEvent event) 
     { 


      resultLabel.setText(resultLabel.getText() + "2.00");   
     } 
    } 

    private class ButtonListener4 implements ActionListener 
    { 
     public void actionPerformed (ActionEvent event) 
     { 


      resultLabel.setText(resultLabel.getText() + "1.50");   
     } 
    } 

    private class ButtonListener5 implements ActionListener 
    { 
     public void actionPerformed (ActionEvent event) 
     { 


      resultLabel.setText(resultLabel.getText() + "1.50");   
     } 
    } 

    private class ButtonListener6 implements ActionListener 
    { 
     public void actionPerformed (ActionEvent event) 
     { 


      resultLabel.setText(resultLabel.getText() + "1.50");   
     } 
    } 

    private class ButtonListener7 implements ActionListener 
    { 
     public void actionPerformed (ActionEvent event) 
     { 


      resultLabel.setText(resultLabel.getText() + "1.50");   
     } 
    } 

    private class ButtonListener8 implements ActionListener 
    { 
     public void actionPerformed (ActionEvent event) 
     { 


      resultLabel.setText(resultLabel.getText() + "1.50");   
     } 
    } 

    private class ButtonListener9 implements ActionListener 
    { 
     public void actionPerformed (ActionEvent event) 
     { 


      resultLabel.setText(resultLabel.getText() + "1.50");  
     } 
    } 

    private class ButtonListenerT implements ActionListener 
    { 
     public void actionPerformed (ActionEvent event) 
     { 



      double total; 

      resultLabel.setText ("Total plus tax:" + total = total *1.07);   
     } 
    } 


} 
+2

我編程3年以上。我從來沒有見過有人添加類名。 –

+0

我知道它沒有任何意義,我只是試圖展示我想要實現的想法 – Lou44

+0

你是指總數= ButtonListener0 + ButtonListener1 + ButtonListener2 + ... – maheeka

回答

1

確定OP的代碼非常模糊,並且很難理解。所以我要在黑暗中扔石頭。

除了ButtonListenerT每個按鈕聽者的actionPerformed方法,更改代碼以

private class ButtonListener0 implements ActionListener 
{ 
    public void actionPerformed (ActionEvent event) 
    { 
     resultLabel.setText(String.valueOf(Double.parseDouble(resultLabel.getText()) + 10.00));   
    } 
} 

ButtonListener210.0016.00等等...

現在

private class ButtonListenerT implements ActionListener 
{ 
    public void actionPerformed (ActionEvent event) 
    { 
     double total; 
     total = Double.parseDouble(resultLabel.getText()); 
     resultLabel.setText ("Total plus tax: " + (total *1.07));   
    } 
} 
+0

它說:對於按鈕偵聽器0: 「JLabel類型的方法setText(String)不適用於參數(雙精度)」 – Lou44

+0

@ Lou44檢查現在.. –

+0

嗯它現在運行。但是,當我按任何按鈕它說在控制檯中的所有這些:(這只是它的一部分,不能發佈全部) 線程「AWT-EventQueue-0」中的異常java.lang.NumberFormatException:空字符串 \t在sun.misc.FloatingDecimal.readJavaFormatString(來源不明) \t在java.lang.Double.parseDouble(來源不明) \t在PizzaGUI $ ButtonListener2.actionPerformed(PizzaGUI.java:99) \t在javax.swing.AbstractButton中.fireActionPerformed(Unknown – Lou44