gpt4 book ai didi

java - 在 GUI 中合计金额

转载 作者:行者123 更新时间:2023-12-01 13:42:24 25 4
gpt4 key购买 nike

我下面的代码可以完成我想要的一切,除了缺少一个关键功能。我希望它获取每个方 block 的值并将其显示在输入框中。然后,当用户按下总计时,它会显示最终价格加上 7% 的税。目前这只是一个错误,但之前它只是将金额放入总计框中,而没有实际将其相加。我怎样才能让它正确地添加它们?谢谢你!这是我的 2 个类文件:

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);
}
}


}

最佳答案

好吧,OP 的代码非常模糊且难以理解。所以我要在黑暗中扔一 block 石头。

在除 ButtonListenerT 之外的每个按钮列表器的 actionPerformed 方法中,将代码更改为

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

对于 ButtonListener2 10.00 将是 16.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));
}
}

关于java - 在 GUI 中合计金额,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20618307/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com