0

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.

Frakcool
  • 10,915
  • 9
  • 50
  • 89
CJT157
  • 1
  • 1
  • What do you mean with `look for a new String`? Here's an [example](https://stackoverflow.com/questions/34164022/calculator-returns-0-0-to-all-questions-asked/34164167#34164167) of a calculator. Right now you're comparing if `ta` is equal to `calc` which it isn't, as `ta` is a `JTextArea` and `calc` is a `String`. Also you have 2 `ta` (one `static` global one and one local). You shouldn't be using `static` components in the first place – Frakcool Jun 03 '19 at 17:29
  • Oh so when I mean look for a new string, I want for the if statement to look for a new input after having received "calc". – CJT157 Jun 03 '19 at 17:33
  • You're mixing 2 programming paradigms I think: Trying to create a console calculator and having one in Swing, – Frakcool Jun 03 '19 at 17:33
  • Good point, so are you saying it's best not to try and make one the way I currently am? – CJT157 Jun 03 '19 at 17:35
  • 1
    See also this [calculator example](http://stackoverflow.com/a/7441804/418556). It uses `ScriptEngine` to evaluate the expression in the text field. – Andrew Thompson Jun 03 '19 at 17:57

1 Answers1

-1

GUI programming is event driven. An event is usually when the user types something with the keyboard or uses the mouse. After clicking the button, you change the text displayed in the JTextArea only. Then you need to wait for the user to type something. This waiting can't be done in the actionPerformed() method. You can listen for the user typing on the keyboard via a DocumentListener on the JTextArea document, as in...

Document doc = ta.getDocument();
doc.addDocumentListener(...)

How are you learning Swing? For me, the Oracle tutorials together with several books was the way I learned.

Abra
  • 19,142
  • 7
  • 29
  • 41
  • My current way of learning is using tutorials online to piece together what I want, but being new to the language I've been finding that it doesn't work too well sometimes. – CJT157 Jun 03 '19 at 17:43
  • Tutorial: [Creating a GUI With JFC/Swing](https://docs.oracle.com/javase/tutorial/uiswing/index.html) and book: [Core JFC 2nd Edition](https://www.amazon.com/Core-JFC-2nd-Kim-Topley/dp/013090581X) by Kim Topley – Abra Jun 03 '19 at 18:10
  • @Audahawk That is a not a good strategy when it comes to complex frameworks such as Swing. You have **understand** what you are doing there. Trial and error works for small things, not for frameworks that have so many different corners ... and where you get stuck when just one of the many corners is painted in the wrong color. – GhostCat Jun 03 '19 at 18:11