lolcahol123
lolcahol123

Reputation: 3

How to migrate console to swing application

I have a basic calculation program and I've started to build a GUI and I have a window. My question, is how do I connect the two things together? I heard I should have made the GUI first but that confused me further.

I would like to be able to know how to connect my back end to the front end (GUI)

public class Calc_functions {

    //declaring subtraction feild
    public int Sub (int num1, int num2) {
        //returns the value num1 subtract num2
        return num1 - num2;
    }

    //declaring addition field
    public int Add (int fnum, int snum) {
        //returns the value num1 add num2
        return fnum + snum;
    }

    //declaring division field
    public int Div (int fnum, int snum) {
        //returns the value num1 divided by num2
        return fnum / snum;
    }

    //declaring multiplication field
    public int Mult (int fnum, int snum) {
        //returns the value num1 multiplied by num2
        return fnum * snum;
    }

}

import java.util.Scanner;

public class calc_main {

    public static void main(String[] args) {
        // calls for the Calc_functions class
        Calc_functions math = new Calc_functions ();
        //waits for user imputs and then store it as a variable
        Scanner numbers = new Scanner(System.in);
        //prints out too interface
        System.out.println("Calulator : Enter two numbers and choose a mathmatic symbol + - x /");
        System.out.println("_____________________");
        //prints out too interface
        System.out.print("First number:");
        int num1 = numbers.nextInt();
        //prints out too interface
        System.out.print("Second number:");
        int num2= numbers.nextInt();
        //prints out too interface
        System.out.print("Enter symbol +  -  x  / of the calculation you would like to perform :");
        String operation= numbers.next();

        // if the user has inputted +, it will carry out the addition of the two variables the user has unputted.
        if (operation.equals("+"))
            System.out.println(math.Add(num1, num2));
        // if the user has inputted -, it will carry out the addition of the two variables the user has unputted.
        else if (operation.equals("-"))
            System.out.println(math.Sub(num1, num2));
        // if the user has inputted x, it will carry out the addition of the two variables the user has unputted.
        else if (operation.equals("x"))
            System.out.println(math.Mult(num1, num2));
        // if the user has inputted /, it will carry out the addition of the two variables the user has unputted.
        else if (operation.equals("/"))
            System.out.println(math.Div(num1, num2));
        else
            System.out.println("The operation is not valid.");

        numbers.close();
        System.exit(0);
    }

}

import javax.swing.*;

// some code used from docs.oracle.com
public class Calc_gui {

    private static void GUI(){
        JFrame createshowGUI = new JFrame("Calc_gui");
        createshowGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Add the ubiquitous "Hello World" label.
        JLabel label = new JLabel("calcgui");
        createshowGUI.getContentPane().add(label);

        //Display the window.
        createshowGUI.pack();
        createshowGUI.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                GUI();
            }
        });
    }

}

Upvotes: 0

Views: 88

Answers (1)

Zhedar
Zhedar

Reputation: 3510

For such a simple task you won't need a "backend" and a "frontend". For this use case, it is sufficient to call your calculations and the respective action methods of your gui components. Action methods means that you add e.g. an ActionListener to a JButton which then executes the corresponding command, for example execute an addition.

You could then extract the code that needs to be executed for the 4 cases into listeners, for example (pseudo code, didn't compile it!):

void actionPerformed(ActionEvent e)
{    //listener for add-button
    int num1 = Integer.parse(textfield1.getText());
    int num2 = Interger.parse(textfield2.getText());
    textField3.setText(String.valueOf( math.add(num1, num2) ) );
}

...and then wire them to the buttons via addActionListener.

The code above get's two values from two textfields and tries to convert them to int values. Then it calls your calculation method. There are ways to add a listener to multiple buttons and detect which button was pressed (by comparing the source of the event with the component), so you won't need to duplicate all that "get and set values form textfields" code.

This is the basic principle. It may not the way it should be done for more complex apps or long running operations, since executing them in the ActionListener means they're blocking the EDT and no GUI events will be processed.

Upvotes: 1

Related Questions