Home > Software engineering >  How to get text from a textfield using a button in Java Swing, then add it to the panel?
How to get text from a textfield using a button in Java Swing, then add it to the panel?

Time:05-21

I have been developing a calculator with a user interface for about 2 days overall, and was about to finish it until I was abruptly interrupted in my progress by an error (not exactly an error).

Here is my code

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Objects;

public class GUI implements ActionListener {

    double solution = 0;
    JButton button;
    JLabel answerLabel;
    JPanel panel;
    JTextField firstNumber;
    JTextField operator;
    JTextField secondNumber;

    public GUI(){
        JFrame frame = new JFrame();

        button = new JButton("Solve");
        button.addActionListener(this);

        JLabel label = new JLabel("Welcome to Calculator");
        label.setFont(new Font("Consolas", Font.BOLD, 30));

        JLabel enterFirstNumber = new JLabel("Enter first number:");
        enterFirstNumber.setFont(new Font("Consolas", Font.BOLD, 20));

        JLabel enterOperator = new JLabel("Enter your operator( , -, *, /):");
        enterOperator.setFont(new Font("Consolas", Font.BOLD, 20));

        JLabel enterSecondNumber = new JLabel("Enter second number");
        enterSecondNumber.setFont(new Font("Consolas", Font.BOLD, 20));

        firstNumber = new JTextField();
        firstNumber.setFont(new Font("Consolas", Font.BOLD, 35));

        operator = new JTextField();
        operator.setFont(new Font("Consolas", Font.BOLD, 35));

        secondNumber = new JTextField();
        secondNumber.setFont(new Font("Consolas", Font.BOLD, 35));

        panel = new JPanel();
        panel.setBorder(BorderFactory.createEmptyBorder(30, 30, 10, 30));
        panel.setLayout(new GridLayout(0, 1));
        panel.add(label);
        panel.add(enterFirstNumber);
        panel.add(firstNumber);
        panel.add(enterOperator);
        panel.add(operator);
        panel.add(enterSecondNumber);
        panel.add(secondNumber);
        panel.add(button);

        frame.add(panel, BorderLayout.CENTER);
        frame.setSize(450, 450);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Calculator");
        frame.setVisible(true);
        frame.setResizable(false);

        double firstNumberValue = Double.parseDouble(firstNumber.getText());
        String operatorChoice = operator.getText();
        double secondNumberValue = Double.parseDouble(secondNumber.getText());
        System.out.println(firstNumberValue   secondNumberValue   operatorChoice);
    }

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

    @Override
    public void actionPerformed(ActionEvent e) {
        double firstTextBox = Double.parseDouble(firstNumber.getText());
        String operatorValue = operator.getText();
        double secondTextBox = Double.parseDouble(secondNumber.getText());

        if (Objects.equals(operatorValue, " ")) {
            solution = firstTextBox secondTextBox;
        }
        if (Objects.equals(operatorValue, "-")) {
            solution = firstTextBox secondTextBox;
        }
        if (Objects.equals(operatorValue, "*")) {
            solution = firstTextBox*secondTextBox;
        }
        if (Objects.equals(operatorValue, "/")) {
            solution = firstTextBox/secondTextBox;
        }
        answerLabel = new JLabel(String.valueOf(solution));
        panel.add(answerLabel);
    }
}

At the end I add a "method" action performed in which I use panel.add(anwerLabel);. However, this does not work.

Alongside this, if anyone can point out the other redundancies in my code that would be helpful, as I have recently started learning Java.

Thanks.

CodePudding user response:

The below code implements the suggestion from @AndrewThompson in his screen capture as animated gif

  • Related