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();
}
});
这些是调用方法的按钮。这是我遇到问题的地方,只有一个按钮点击工作,百事可乐按钮
接下来的部分是我正在呼唤的恶意。它确定输入的硬币和金额是否正确。
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;
答案 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();
}
});