gpt4 book ai didi

java - 这个简单的 GUI 程序出错

转载 作者:行者123 更新时间:2023-12-01 16:33:29 26 4
gpt4 key购买 nike

这个简单的程序应该能够获取加价百分比和批发成本并计算零售价,我在计算按钮上放置了一个 Action 监听器,但是当我按下计算按钮时,会出现此错误:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: 
For input string: "Enter the markup precentage"
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at java.lang.Double.parseDouble(Unknown Source)
at mm$CalcListerner.actionPerformed(mm.java:58)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)

有人可以帮我解决这个问题吗?

有人可以解释一下这些错误消息吗,因为我真的不明白这些错误消息?

我的代码是:

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

public class mm extends JFrame {

private JTextField WholesaleCost;
private JTextField markupPresentage;
private JLabel WCost;
private JLabel MPrecentage;
private JButton button;
private JPanel pannel;
private final int Width = 250;
private final int height = 320;

public mm() {
setTitle("Retail Price Calculator");
setSize(Width, height);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
buildPanel();
add(pannel);
}

private void buildPanel() {
WholesaleCost = new JTextField(10);
markupPresentage = new JTextField(10);

WCost = new JLabel("enter the Whole Sale cost");
MPrecentage = new JLabel("Enter the markup precentage");

button = new JButton("Calculate");
button.addActionListener(new CalcListerner());

pannel = new JPanel();
pannel.add(WholesaleCost);
pannel.add(markupPresentage);
pannel.add(WCost);
pannel.add(MPrecentage);
pannel.add(button);
}

private class CalcListerner implements ActionListener {

public void actionPerformed(ActionEvent e) {
String WSaleinput;
String MPres;
WSaleinput = WholesaleCost.getText();
MPres = MPrecentage.getText();

double Value = Double.parseDouble(WSaleinput) * (Double.parseDouble(MPres) / 100);

JOptionPane.showMessageDialog(null, "Retail Price is " + Value);
}
}

public static void main(String[] args) {
mm x = new mm();
}
}

最佳答案

您试图引用错误的组件。

这个

WSaleinput=WholesaleCost.getText();
MPres =MPrecentage.getText();

实际上应该是

WSaleinput = WholesaleCost.getText();
MPres = markupPresentage.getText();

关于java - 这个简单的 GUI 程序出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11963474/

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