Java计算器

标签 java swing calculator

我需要有关此代码的帮助。结果将进入一个新窗口。我想要的是结果位于我输入值的主窗口内。

下面的代码将有一个弹出屏幕显示结果,而不是在零售价框中显示答案。我知道这可能是一个简单的问题或修复希望有人帮助我修复此代码,因为我仍在学习。

package com.retailcalcu;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class Retailcalculator  extends JFrame{

  private   JPanel panel;                           // to reference a panel
  private   JLabel wholeSaleMessageLabel;       // to reference a label
  private   JLabel markupMessageLabel;          // to reference a label
  private   JLabel retailPriceLabel;            // to reference a label

  private   JTextField  wholeSalePriceField;    // to reference the Whole Sale Price Field
  private   JTextField  markUpPercentage;       // to reference the Mark Up text field
  private   JTextField  retailPrice;            // to reference the Retail Price Field
  private   JButton calculateButton;            // to reference button
  private   final   int WINDOW_WIDTH = 400;         // Window Width

  private   final int   WINDOW_HEIGHT = 150;            // Window Height


  public static void main(String[] args){
  new Retailcalculator();
  }


    public Retailcalculator(){

        // set window title
   setTitle("Retail Price Calculator");


        // set widow size

        setSize(WINDOW_WIDTH, WINDOW_HEIGHT);



        // tell what to do when the window closes

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



      // build the panel and add it to the frame

        buildPanel();



        // add the panel to the frame's content pane

        add(panel);


        // display the window

        setVisible(true);

     }



    private void buildPanel()

    {


        // create a label for the wholesale price

        wholeSaleMessageLabel = new JLabel("Enter Wholesale Price: ");



        // create a text field 15 characters wide

        wholeSalePriceField = new JTextField(15);



        // create a label for the mark up percentage

        markupMessageLabel = new JLabel("Enter Mark Up Percentage: ");



        // create a text field 15 characters wide

        markUpPercentage = new JTextField(15);



        // create a button with the capation "Calculate Retail Price"

        calculateButton = new JButton("Calculate Retail Price");





        // add an action listener to the button

        calculateButton.addActionListener(new CalcButtonListener());





        // create a label for the retail price

        retailPriceLabel = new JLabel("The Retail Price is:  $");



        // create a text field 15 characters wide for the retail price

        retailPrice = new JTextField(15);



        // create a JPanel object and let the panel filed reference it

        panel = new JPanel();



        // add the labels, rext field and button compnenets to the panel

        panel.add(wholeSaleMessageLabel);
        panel.add(wholeSalePriceField);
        panel.add(markupMessageLabel);
        panel.add(markUpPercentage);
        panel.add(calculateButton);
        panel.add(retailPriceLabel);
        panel.add(retailPrice);

    /** calculateButton is an action listener class for the Calculate Button*/

    }

    class CalcButtonListener implements ActionListener{

        // This method executes when the user clicks on the Calculate Button

        public void actionPerformed(ActionEvent e)

        {

            String  wholesaleInput; // to hold user input for wholesale price
            String  markUpInput;        // to hold user input for mark up percentage
            double  retailPrice;        // declare retail price as a double
            double  wholePrice;         // declare wholesale prcie as a double
            double  markUp;             // declare markup percentage as a double

        // get the text entered by the user in the text field box

        wholesaleInput = wholeSalePriceField.getText();

        // convert wholesale text to double

        wholePrice = Double.parseDouble(wholesaleInput);

            // get the text entered by the user in the percentage text field

            markUpInput = markUpPercentage.getText();

            // parse out the markup percentage from the percentage text field
            markUp = Double.parseDouble(markUpInput);

            // do function to calculate retail price( wholesale * markup)

            retailPrice = ((wholePrice * markUp) * .01) + wholePrice;

            // display the results
            JOptionPane.showMessageDialog(null, retailPrice);

        }

    }
}

最佳答案

内部类CalcButtonListener中的双变量retailPriceshadowing外部类中同名的 JTextField

因此,您必须使用:

Retailcalculator.this.retailPrice.setText(Double.toString(retailPrice));

通过将外部类中名为 retailPriceJTextField 重命名为 retailPriceTextField,您可以避免混淆,只需调用:

retailPriceTextField.setText(Double.toString(retailPrice));

关于Java计算器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13037589/

相关文章:

java - Java 后缀计算器中的 StackOverFlowError

java - 如何嵌套复杂的Swing组件

java - 如何在 java 中创建自定义光标图像

c++ - 如何在 C++ 中只创建一次循环 "continue"?

c# - 计算器c#中的小数点

java - JScrollPane 中的 JPanel 缺少滚动条

java - 引用 Java 中特定父类(super class)的任何子类

java - 在 jhipster 中更新实体时正确的解决方法是什么?

java - 从普通 Java 事件创建 Observable

java - RxJava reduce() 在并行化时会不安全吗?