java - 将局部变量传递到 JTextbox

标签 java swing user-interface

我正在处理 Java 编程挑战,我创建了一个 GUI,其中有 3 个框用于输入数字 A、B 和 C,然后我有一个操作顺序框,用户可以在其中输入 A、B、C ,以及他们选择的任何数学运算符。单击“计算”按钮时,数学运算的结果将显示在结果文本框中。

编辑以获得更好的解释:

  1. 用户在 A、B 和 C 的两个或多个字段中输入数字。
  2. 用户在表达式字段中输入表达式。
  3. 当用户单击“求解”按钮时,将对表达式求值并将结果显示在“结果”字段中。

这就是我到目前为止所拥有的。

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

/*The string builder class allows the user to enter numbers into the UI
* The UI converts the Textbox input from strings to Floats and stores them in local variables
* A, B, and C
*/

    public class StringBuildUI extends JFrame
    {
        //declare all variables, constants, and components for the GUI

        private JPanel panel;                   // a panel to hold the gui components
        private JLabel messageLabelA;           // a label for the Text Field A
        private JLabel messageLabelB;           // a label for the Text Field B
        private JLabel messageLabelC;           // a label for the Text Field C
        private JLabel messageLabelR;           // a label for the Text Field results
        private JLabel messageLabelO;           // a label for the Text Field order of ops
        private JTextField boxA;                // a box to hold user input
        private JTextField boxB;                 // a box to hold user input
        private JTextField boxC;                // a box to hold user input
        private JTextField order;               // a box to determine order of operations
        private JTextField results;             // a box to hold user input
        private JButton calcButton;             // a button to calculate strings
        private JButton clearButton;            // a button to clear all text fields
        private JButton exitButton;             // a button to exit the program
        private final int WINDOW_WIDTH = 750;   //window width
        private final int WINDOW_HEIGHT = 350;  //window height

/*
Constructor method for the GUI
 */

    public StringBuildUI ()
    {

        //set title
        setTitle("String Evaluator");

        //set size of the window
        setSize(WINDOW_WIDTH,WINDOW_HEIGHT);

        //build the panel and add it to the frame and display the window
        buildPanel();
        add(panel);
        setVisible(true);
    }

 /*
 build panel adds label, text fields and all the buttons to the panel
 */

    private void buildPanel()
    {
        //create the label for the text boxes, the boxes, and the buttons
        messageLabelA = new JLabel("A");
        messageLabelB = new JLabel("B");
        messageLabelC = new JLabel("C");
        messageLabelR = new JLabel("Results");
        messageLabelO = new JLabel("Order of Operations");
        boxA = new JTextField(10);
        boxB = new JTextField(10);
        boxC = new JTextField(10);
        order = new JTextField(10);
        results = new JTextField(10);
        calcButton = new JButton("calculate");
        clearButton = new JButton(" Clear Fields");
        exitButton = new JButton("Exit");

        //Group the buttons, and add them to the panel
        add(calcButton);
        add(clearButton);
        add(exitButton);

        //Create a panel and add components to it
        panel = new JPanel();
        panel.add(messageLabelA);
        panel.add(boxA);
        panel.add(messageLabelB);
        panel.add(boxB);
        panel.add(messageLabelC);
        panel.add(boxC);
        panel.add(messageLabelO);
        panel.add(order);
        panel.add(messageLabelR);
        panel.add(calcButton);
        panel.add(clearButton);
        panel.add(exitButton);
    }
  /*
  Private inner class for event handling when the user clicks buttons
   */

    private class ButtonListener implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
          String inputA;    // holds user input from text box a
          String inputB;    // holds user input from text box b
          String inputC;    // holds user input from text box c
          Float   results;  // holds results of calculations
          Float A,B,C;      // holds converted strings for calculations
          //get string input
          inputA = boxA.getText();
          inputB = boxB.getText();
          inputC = boxC.getText();

          //Convert strings to integers
          A = Float.parseFloat(inputA);
          B = Float.parseFloat(inputB);
          C = Float.parseFloat(inputC);

         //get input from order of operations box
         Float inputABC;
         inputABC = order.getFloat();
        }

我是否提前转换? 我想获取 A B 和 C 并在计算按钮的事件处理中使用它们,以便结果将基于用户输入。在我内心深处,我认为我可以根据用户输入进行数学运算的唯一方法是变量 A B 和 C 是否是字符串,但我不确定。

如何传递从框中输入的文本并在事件处理中使用它们?

谢谢。

编辑问题的答案:数学运算是用户定义的,因此我无法将它们放入计算按钮的事件处理程序中。我还没有开始编写计算按钮,因为我不确定如何处理用户输入。

最佳答案

您在正确的时间获取数据并进行转换,但我没有看到您对数字进行任何计算或设置结果字段文本。您的 ActionListener 的 actionPerformed 需要在最后调用 results.setText(...) 方法。

顺便说一句,您需要学习并遵守 Java 命名标准,包括给出以小写字母开头的变量名称。您还需要使用“ self 注释”的名称,以使您的代码更易于理解。

另一方面,你几乎不应该使用 float,而 double 可以工作,因为通过这样做,你可以以很少的成本获得很高的精度。

关于java - 将局部变量传递到 JTextbox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21655167/

相关文章:

java - Spring错误创建名称为“org.springframework.aop.config.internalAutoProxyCreator”的bean

java - 如何在 Java Gnome/GTK 程序中使用 Glade UI(.glade 文件)?

java - 类预期错误。需要简单程序的帮助

java - Spring Boot 测试 - 没有 'com.example.MyService' 类型的合格 bean 可用

java - Jfreechart索引越界异常

java - 框架内有 2 个 J 面板。左侧面板是带有按钮的菜单。使用按钮切换右面板。

java - 如何在 JRadioButtons 中使用 ActionListener

user-interface - 适用于 Windows/Linux 桌面的 Vimium 概念

Android:如何访问默认文本颜色? (没有主题,只是标准的)

javascript - jQuery 用户界面 : smooth slider with one snap (to default value)?