2016-12-07 51 views
0
JButton btnNewButton = new JButton("Pepsi"); 
    btnNewButton.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      changeD(); 
     } 
    }); 
    btnNewButton.setBounds(23, 11, 80, 29); 
    contentPane.add(btnNewButton); 
    JButton btnNewButton_1 = new JButton("Sprite"); 
    btnNewButton.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      changeD(); 
     } 
    }); 
    btnNewButton_1.setBounds(113, 11, 80, 29); 
    contentPane.add(btnNewButton_1); 
    JButton btnFanta = new JButton("Fanta"); 
    btnNewButton.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      changeD(); 
     } 

    }); 

这些都是调用method.This的按钮是我有一个问题,只有一个按钮点击作品,百事按钮试图致电多个按钮的一种方法,但只有一个按钮使用方法

接下来的部分是我打来的方法。它确定输入的硬币和金额是否正确。你有一个错误

public double changeD() { 

     JOptionPane.showMessageDialog(null, "This Product costs 1.50 EU"); 
     String payment = JOptionPane.showInputDialog(null, "Please insert cash"); 
     int intAmount = Integer.parseInt(payment); 
     double drinkPrice = 1.50; 
     double changeDue = intAmount - drinkPrice; 
     if (intAmount == 2 + 1 + .50 && intAmount > drinkPrice) 

     { 
      JOptionPane.showInputDialog(null, "Please take your product and change " + changeDue); 

     } 

     else if (intAmount != 2 + 1 + .50 || intAmount < drinkPrice) 

     { 
      JOptionPane.showMessageDialog(null, "Please insert correct amount or coins "); 
     } 

     return (Double) null; 
+1

尝试'btnNewButton_1.addActionListener'和'btnFanta.addActionListener'。你继续添加动作侦听器到同一个按钮。 –

+0

你在暗示我改变了什么? –

+0

对不起,我看不到整个评论,谢谢你和我现在将改变它:) –

回答

0

通知可能做复制粘贴您设置相同的按钮用的ActionListener两次

JButton btnNewButton = new JButton("Pepsi"); 
btnNewButton.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 
     changeD(); 
    } 
}); 
btnNewButton.setBounds(23, 11, 80, 29); 
contentPane.add(btnNewButton); 
JButton btnNewButton_1 = new JButton("Sprite"); 

//Wrong button here !!!!!!!!!!!!!!!!!!! Should be btnNewButton_1 
btnNewButton.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 
     changeD(); 
    } 
}); 
btnNewButton_1.setBounds(113, 11, 80, 29); 
contentPane.add(btnNewButton_1); 
JButton btnFanta = new JButton("Fanta"); 
btnNewButton.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 
     changeD(); 
    } 

}); 
+0

请解释问题和解决方案,不要只是转储代码。除了别的,你已经在你的“答案”中留下了一个问题的实例。 –

+0

更好吗? – urag

+0

我的问题是,我忽略了,我在每个地方引用相同的按钮。程序在点击任何按钮时执行该方法。现在我只需要提供取消方法中购买的选项 –

相关问题