lundi 3 juin 2019

Is there a way to look for a new string input after a JButton is pressed each time?

My goal is to have an application that works as a tool to do different functions, right now I am trying to make a calculator(text only) but am having trouble finding out how to look for new strings each time the button is pressed. Also, making sure certain commands can only be inputted each time the button is pressed.

Being introductory to the language I don't know what to do to make this happen.

import java.awt.event.*;

import javax.swing.*;

public class Main extends JFrame {
    static JTextField tf;
    static JFrame frame;
    static JPanel panel;
    static JTextArea ta;
    int count;
    int num1;
    int num2;
    int exp;
    char operator;
    double answer;

    static void GUI() {
        frame = new JFrame("Thank you for reading this");
        panel = new JPanel();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400,400);
        JButton button = new JButton("Test");
        tf = new JTextField(15);
        panel.add(tf);
        panel.add(button);

        JTextArea ta = new JTextArea();
        ta.setEditable(false);

                //button I'm trying to work on
        button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                String text = tf.getText();
                ta.append(text+"\n");
                if(text.equals("calc")) {
                    ta.append("What Operation (+, -, *, /, ^)?: \n");
                                        if(text.equals("*"){ 
                                                //this is where I have trouble}
                }
            }
        });

        frame.getContentPane().add(BorderLayout.CENTER, ta);
        frame.getContentPane().add(BorderLayout.SOUTH, panel);
        frame.setVisible(true);
    }

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

What I expect is to be able to type in * after typing in calc, and then continuing on with the function after that.

Aucun commentaire:

Enregistrer un commentaire