简体   繁体   English

这个简单的GUI程序中的错误

[英]Error in this simple GUI program

This simple program should be able to get the Markup percentage and the wholesale Cost and calculate the retail price i put an action listener to the CALCULATE button but when i press the calculate button this error appears: 这个简单的程序应该能够获得加价百分比和批发成本并计算零售价,我将一个动作侦听器置于“ 计算”按钮上,但是当我按下“计算”按钮时,将出现以下错误:

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)

Can someone help me with this issue? 有人可以帮我解决这个问题吗?

Can someone please explain these error messages because I don't really get any of that? 有人可以解释这些错误消息,因为我什么都没得到吗?

My code is: 我的代码是:

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

You are trying to parse MPrecentage.getText() to a double, but that's the value of your label, not the value that was input. 您试图将MPrecentage.getText()解析为双精度,但这是标签的值,而不是输入的值。

You should be setting MPres to markupPercentage.getText(); 您应该将MPres设置为markupPercentage.getText();

The error, NumberFormatException, is one that is thrown if the jvm can't get a number out of the string you give it. 如果jvm无法从您提供的字符串中获取数字,则会引发错误NumberFormatException。 In this case, you are trying to parse a number from the text "Enter the markup precentage" 在这种情况下,您尝试解析文本“输入标记比例”中的数字

You're trying to reference the wrong component. 您正在尝试引用错误的组件。

This 这个

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

Should actually be 应该是

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM