2013-11-09 34 views
-2

我有一个完整的编码,基本上归结为一个gui接口。问题是,我不知道如何让它工作作为自动售票机固定利率和固定金额的现金能够投入。售票机

所以,我已经做了计算总数在哪里,无论如何我做了什么,它总是以0.00英镑作为总体答案。

我非常需要帮助。这非常令人沮丧。下面是只触发了JTextField为TicketCalculation类

import java.awt.*; 
import java.text.*; 
import java.awt.event.*; 
import javax.swing.*; 


public class TicketCalculation extends JFrame implements ActionListener { 

    DecimalFormat pounds = new DecimalFormat("£#,##0.00"); 


    //creating and naming buttons and textfields 
    private JButton twentypenceBtn = new JButton("20p"); 
    private JButton fiftypenceBtn = new JButton("50p"); 
    private JButton onepoundBtn = new JButton("£1"); 
    private JButton twopoundsBtn = new JButton("£2"); 
    private JButton fivepoundsBtn = new JButton("£5"); 
    private JButton tenpoundsBtn = new JButton("£10"); 
    private JButton twentypoundsBtn = new JButton("£20"); 
    private JButton C = new JButton("Calculate"); 

    private JButton frontBtn = new JButton("<<Front Stalls>>"); 
    private JButton private1Btn = new JButton("<<Private Box>>"); 
    private JButton middleBtn = new JButton("<<Middle Stalls>>"); 
    private JButton backBtn = new JButton("<<Back Stalls>>"); 
    private JButton calcBtn = new JButton("Calculate Bill"); 

    private JTextField tickettypeTxt = new JTextField(14); 
    private JTextField stalltypeTxt = new JTextField(25); 
    private JTextField amountticketsTxt = new JTextField(14); 
    private JTextField totalamountTxt = new JTextField(10); 
    private JTextField amountdueTxt = new JTextField(13); 
    private JTextField amountpaidTxt = new JTextField(10); 


    //creating labels 
    private JLabel pickstall = new JLabel(); 
    private JLabel tictype = new JLabel(); 
    private JLabel amontic = new JLabel(); 
    private JLabel ttamon = new JLabel(); 
    private JLabel amondue = new JLabel(); 
    private JLabel amonpaid = new JLabel(); 
    private JLabel label5 = new JLabel(); 
    private JLabel spacing6 = new JLabel(); 
    private JLabel spacing7 = new JLabel(); 
    private JLabel spacing8 = new JLabel(); 
    private JLabel spacing9 = new JLabel(); 
    private JLabel spacing10 = new JLabel(); 


    //image icon declarations 
    private ImageIcon paycount = new ImageIcon(getClass().getResource("paycount.jpg")); 

    double middleprice; 
    double privateprice; 
    double backprice; 
    double frontprice; 
    double type; 
    int number; 

    public static void main(String[] args){ 
     new TicketCalculation(); 
    } 


    public TicketCalculationt(){ 
     setLayout(new BorderLayout()); 
     setSize(650,750); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 



     //naming labels 
     pickstall = new JLabel("==> Pick a Stall Type: "); 
     tictype = new JLabel ("==> Price of a Ticket: £"); 
     amontic = new JLabel("==> Amount of Tickets: "); 
     ttamon = new JLabel("==> Total Amount: "); 
     amondue = new JLabel("==> Amount Due: £"); 
     amonpaid = new JLabel("==> Amount Paid: £"); 
     label5 = new JLabel(paycount); 
     spacing6 = new JLabel("      "); 
     spacing7 = new JLabel("                              "); 
     spacing8 = new JLabel("                                       "); 
     spacing9 = new JLabel("            "); 
     spacing10 = new JLabel("        "); 



     //setting font for buttons, textfields and labels 
     pickstall.setFont(new Font("Rockwell", Font.BOLD, 20)); 
     frontBtn.setFont(new Font("System", Font.BOLD, 22)); 
     middleBtn.setFont(new Font("System", Font.BOLD, 22)); 
     backBtn.setFont(new Font("System", Font.BOLD, 22)); 
     private1Btn.setFont(new Font("System", Font.BOLD, 22)); 
     tictype.setFont(new Font("Rockwell", Font.BOLD, 20)); 
     amontic.setFont(new Font("Rockwell", Font.BOLD, 20)); 
     ttamon.setFont(new Font("Rockwell", Font.BOLD, 20)); 
     amondue.setFont(new Font("Rockwell", Font.BOLD, 20)); 
     amonpaid.setFont(new Font("Rockwell", Font.BOLD, 20)); 
     stalltypeTxt.setFont(new Font("Verdana", Font.BOLD, 20)); 
     tickettypeTxt.setFont(new Font("Verdana", Font.BOLD, 20)); 
     amountticketsTxt.setFont(new Font("Verdana", Font.BOLD, 20)); 
     totalamountTxt.setFont(new Font("Verdana", Font.BOLD, 20)); 
     amountdueTxt.setFont(new Font("Verdana", Font.BOLD, 20)); 
     amountpaidTxt.setFont(new Font("Verdana", Font.BOLD, 20)); 

     twentypenceBtn.setFont(new Font("Century Gothic", Font.BOLD, 28)); 
     fiftypenceBtn.setFont(new Font("Century Gothic", Font.BOLD, 28)); 
     onepoundBtn.setFont(new Font("Century Gothic", Font.BOLD, 28)); 
     twopoundsBtn.setFont(new Font("Century Gothic", Font.BOLD, 28)); 
     fivepoundsBtn.setFont(new Font("Century Gothic", Font.BOLD, 28)); 
     tenpoundsBtn.setFont(new Font("Century Gothic", Font.BOLD, 28)); 
     twentypoundsBtn.setFont(new Font("Century Gothic", Font.BOLD, 28)); 

     C.setFont(new Font("Serif", Font.BOLD, 28)); 

     stalltypeTxt.setEditable(false); 
     tickettypeTxt.setEditable(false); 
     totalamountTxt.setEditable(false); 
     amountdueTxt.setEditable(false); 
     amountpaidTxt.setEditable(false); 



     //positioning all buttons, textfields and labels 
     JPanel top = new JPanel(); 
     top.add(label5); 
     add("North", top); 

     JPanel mid = new JPanel(); 
     mid.add(pickstall); 
     mid.add(stalltypeTxt); 
     mid.add(spacing6); 

     mid.add(private1Btn); 
     mid.add(frontBtn); 
     mid.add(middleBtn); 
     mid.add(backBtn); 

     mid.add(tictype); 
     mid.add(tickettypeTxt); 
     mid.add(spacing7); 

     mid.add(amontic); 
     mid.add(amountticketsTxt); 
     mid.add(spacing8); 

     mid.add(ttamon); 
     mid.add(totalamountTxt); 
     mid.add(calcBtn); 
     mid.add(spacing10); 

     mid.add(amonpaid); 
     mid.add(amountpaidTxt); 
     mid.add(spacing9); 

     mid.add(twentypenceBtn); 
     mid.add(fiftypenceBtn); 
     mid.add(onepoundBtn); 
     mid.add(twopoundsBtn); 
     mid.add(fivepoundsBtn); 
     mid.add(tenpoundsBtn); 
     mid.add(twentypoundsBtn); 

     mid.add(amondue); 
     mid.add(amountdueTxt); 
     mid.add(C); 
     add("Center", mid); 




     calcBtn.addActionListener(this); 
     private1Btn.addActionListener(this); 
     frontBtn.addActionListener(this); 
     middleBtn.addActionListener(this); 
     backBtn.addActionListener(this); 

     twentypenceBtn.addActionListener(this); 
     fiftypenceBtn.addActionListener(this); 
     onepoundBtn.addActionListener(this); 
     twopoundsBtn.addActionListener(this); 
     fivepoundsBtn.addActionListener(this); 
     tenpoundsBtn.addActionListener(this); 
     twentypoundsBtn.addActionListener(this); 

     C.addActionListener(this); 


     setVisible(true); 

    } 


    public void actionPerformed(ActionEvent e){ 
     // event handler for the buttons 



     if (e.getSource() == private1Btn) 
     { 
      stalltypeTxt.setText("Private Stall Location is Chosen"); 
      tickettypeTxt.setText("30.85"); 
     } 

     else if (e.getSource() == frontBtn) 
     { 
      stalltypeTxt.setText("Front Stall Location is Chosen"); 
      tickettypeTxt.setText("15.00"); 
     } 
     else if (e.getSource() == middleBtn) 
     { 
      stalltypeTxt.setText("Middle Stall Location is Chosen"); 
      tickettypeTxt.setText("10.20"); 
     } 
     else if (e.getSource() == backBtn) 
     { 
      stalltypeTxt.setText("Back Stall Location is Chosen"); 
      tickettypeTxt.setText("5.70"); 

     } 
     else if (e.getSource() == tickettypeTxt) 
     { 

      type = Double.parseDouble(tickettypeTxt.getText()); 
     } 
     else if (e.getSource() == amountticketsTxt) 
     { 
      number = Integer.parseInt(amountticketsTxt.getText()); 
     } 
     else if (e.getSource() == calcBtn) 
     { 

     Workings c = new Workings(); 
     w.setType(type); 
     w.setNumber(number); 
     double total = w.calculateBill(); 

     totalamountTxt.setText(pounds.format(total)); 
     } 


    } 
    } 

与所谓的巷道

public class Workings { 
    // private instance variables 
    private double type, number; 

    // public no-argument constructor 
    public Workings() { } 

    // public constructor with four arguments 
    public Workings(double t, int n) 
    { 
     type = t; 
     number = n; 


    } 

    // public 'set' methods 
    public void setType(double t) { type = t; } 
    public void setNumber(int n) { number = n; } 


    // public method to calculate the bill 
    public double calculateBill() 
    { 
     double total = type * number ; 
     return total; 
    } 
} 
+0

是你的输出会是什么成分?你是否也尝试过运行一个TestClass来测试你的'workings'类中的所有东西都能正常工作? –

+0

你的代码甚至没有改变就没有编译,并且你离开了paycount.jpg图像。我的建议是创建一个由一个或多个类组成的GUI模型,并使用GUI模型填充GUI视图。我也将TicketCalculation分解为几个GUI类。 –

回答

0

ActionListener中途做辅助类的代码,当用户点击键盘上的回车键,而在那个领域。这是不直观的,所以我会说这是一个很大的禁止使用ActionListener

改为使用FocusListener从文本字段中获取“用户离开文本字段”事件。这应该拿起输入的数据,然后它应该工作。

0

这是你的问题

else if (e.getSource() == tickettypeTxt) 
{ 
    type = Double.parseDouble(tickettypeTxt.getText()); 
} 
else if (e.getSource() == amountticketsTxt) 
{ 
    number = Integer.parseInt(amountticketsTxt.getText()); 
} 
else if (e.getSource() == calcBtn) 
{ 
    Workings c = new Workings(); 
    w.setType(type); 
    w.setNumber(number); 
    double total = w.calculateBill(); 

    totalamountTxt.setText(pounds.format(total)); 
} 

删除前两个else if S和只是这样做

else if (e.getSource() == calcBtn) 
{ 
    type = Double.parseDouble(tickettypeTxt.getText()); 
    number = Integer.parseInt(amountticketsTxt.getText()); 

    Workings c = new Workings(); 
    w.setType(type); 
    w.setNumber(number); 
    double total = w.calculateBill(); 

    totalamountTxt.setText(pounds.format(total)); 
} 
+0

嗯,是的,这是一个比我的想法更简单的解决方案! –