Lost
Lost

Reputation: 1

Issues with guis

I am trying to have a window open with 2 text boxes and a drop down menu with operators. Then you enter a number in each text box and choose an operator and the program conducts the required math. There needs to be 2 classes, responds to a 0 as the second number by putting out a label that says you cant divide by 0 if it involves division (this message needs to be red and a certain font size), and when it is does the math and outputs it as a label (this also needs to be a certain color and size). I have scrapped and redone this code at least 3 times over the last week. Im genuinely lost.


// Imports
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.geometry.Pos;
import javafx.geometry.Insets;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.control.Label;

import javafx.application.Application;
import javafx.scene.control.TextField;

public class Test2Driver extends Application
{
    public static void main(String[] args) 
    {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception 
    {   
        String result = null;
        double num1 = 0;
        double num2 = 0;

        TextField num1TextField = new TextField();
        try
        {
            num1 = Double.parseDouble(num1TextField.getText());
        }
        catch(NumberFormatException e)
        {
            e.printStackTrace();
        }
        HBox num1TextBox = new HBox(150, num1TextField);
        num1TextBox.setAlignment(Pos.CENTER);

        TextField num2TextField = new TextField();
        try
        {
            num2 = Double.parseDouble(num2TextField.getText());
        }
        catch(NumberFormatException e)
        {
            e.printStackTrace();
        }
        HBox num2TextBox = new HBox(150, num2TextField);
        num2TextBox.setAlignment(Pos.CENTER);

        Label answer = new Label(result);
        HBox answerBox = new HBox(150, answer);
        answerBox.setAlignment(Pos.CENTER);
        answer.setTextFill(Color.BLACK);
        answerBox.setStyle("-fx-font-size: 36px;");

        String[] operators = {"", "+", "-", "*", "/", "%"};
        JComboBox<String> control = new JComboBox<>(operators);
        control.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event)
            {
                JComboBox control = (JComboBox)event.getSource();

                Object selected = control.getSelectedItem();

                if(selected.toString().equals(""))
                {
                    Calculator.blank();
                }
                else if(selected.toString().equals("+"))
                {
                    Calculator.add();
                }
                else if(selected.toString().equals("-"))
                {
                    Calculator.subtract();
                }
                else if(selected.toString().equals("*"))
                {
                    Calculator.multiply();
                }
                else if(selected.toString().equals("/"))
                {
                    Calculator.divide();
                }
                else if(selected.toString().equals("%"))
                {
                    Calculator.modulus();
                }
            }
        });

        VBox display = new VBox(num1TextBox, num2TextBox, answer);
        display.setAlignment(Pos.CENTER);
        display.setPadding(new Insets(10));

        Scene scene = new Scene(display);
        primaryStage.setWidth(400);
        primaryStage.setHeight(300);
        primaryStage.setScene(scene);
        primaryStage.setTitle("Calculator");
        primaryStage.show();
    }

    public class Calculator extends Test2Driver
    {
        public String blank(String result) 
        {
            result = "Error, pick an operator.";

            return result;
        }

        public String add(double num1, double num2, String result)
        {
            double sum;

            sum = num1 + num2;

            result = String.valueOf(sum);

            return result;
        }

        public String subtract(double num1, double num2, String result)
        {
            double difference;

            difference = num1 - num2;

            result = String.valueOf(difference);

            return result;
        }

        public String multiply(double num1, double num2, String result)
        {
            double product;

            product = num1 * num2;

            result = String.valueOf(product);

            return result;
        }

        public String divide(double num1, double num2, String result)
        {
            if(num2 == 0)
            {
                result = "Can not divide by 0!";

                return result;
            }
            else
            {
                double quotient;

                quotient = num1 / num2;

                result = String.valueOf(quotient);

                return result;
            }
        }

        public String modulus(double num1, double num2, String result)
        {
            if(num2 == 0)
            {
                result = "Can not divide by 0!";

                return result;
            }
            else
            {
                double modulus;

                modulus = num1 % num2;

                result = String.valueOf(modulus);

                return result;
            }
        }
    }
}

Upvotes: 0

Views: 42

Answers (1)

Abdul G
Abdul G

Reputation: 46

I checked your code and found lots of error: In order to use the following line:

 answer.setTextFill(Color.BLACK);

you must import the correct Color for javafx not awt:

//import java.awt.Color; use javafx.scene.paint.Color instead


//Imports
//import java.awt.Color; use javafx.scene.paint.Color instead
import javafx.scene.paint.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.geometry.Pos;
import javafx.geometry.Insets;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.control.Label;

import javafx.application.Application;
import javafx.scene.control.TextField;    

public class Main extends Application {

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

@Override
public void start(Stage primaryStage) {
    try {
        String result = null;
        double num1 = 0;
        double num2 = 0;

        TextField num1TextField = new TextField();
        try
        {
            num1 = Double.parseDouble(num1TextField.getText());
        }
        catch(NumberFormatException e)
        {
            e.printStackTrace();
        }
        HBox num1TextBox = new HBox(150, num1TextField);
        num1TextBox.setAlignment(Pos.CENTER);

        TextField num2TextField = new TextField();
        try
        {
            num2 = Double.parseDouble(num2TextField.getText());
        }
        catch(NumberFormatException e)
        {
            e.printStackTrace();
        }
        HBox num2TextBox = new HBox(150, num2TextField);
        num2TextBox.setAlignment(Pos.CENTER);

        Label answer = new Label(result);
        HBox answerBox = new HBox(150, answer);
        answerBox.setAlignment(Pos.CENTER);
        answer.setTextFill(Color.BLACK);
        answerBox.setStyle("-fx-font-size: 36px;");

        String[] operators = {"", "+", "-", "*", "/", "%"};
        JComboBox<String> control = new JComboBox<>(operators);

        final double finalNum1 = num1;
        final double finalNum2 = num2;
        control.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event)
            {
                //Create instance of Calculator class
                Calculator calc = new Calculator();

                JComboBox control = (JComboBox)event.getSource();

                Object selected = control.getSelectedItem();

                if(selected.toString().equals(""))
                {
                    calc.blank();
                }
                else if(selected.toString().equals("+"))
                {
                    calc.add(finalNum1, finalNum2, "");
                }
                else if(selected.toString().equals("-"))
                {
                    calc.subtract(finalNum1, finalNum2, "");
                }
                else if(selected.toString().equals("*"))
                {
                    calc.multiply(finalNum1, finalNum2, "");
                }
                else if(selected.toString().equals("/"))
                {
                    calc.divide(finalNum1, finalNum2, "");
                }
                else if(selected.toString().equals("%"))
                {
                    calc.modulus(finalNum1, finalNum2, "");
                }
            }
        });

        VBox display = new VBox(num1TextBox, num2TextBox, answer);
        display.setAlignment(Pos.CENTER);
        display.setPadding(new Insets(10));

        Scene scene = new Scene(display);
        primaryStage.setWidth(400);
        primaryStage.setHeight(300);
        primaryStage.setScene(scene);
        primaryStage.setTitle("Calculator");
        primaryStage.show();
    } catch(Exception e) {
        e.printStackTrace();
    }
   }

    public class Calculator extends Main
    {
        public String blank() 
        {
            String result;
            result = "Error, pick an operator.";

            return result;
        }

        public String add(double num1, double num2, String result)
        {
            double sum;

            sum = num1 + num2;

            result = String.valueOf(sum);

            return result;
        }

        public String subtract(double num1, double num2, String result)
        {
            double difference;

            difference = num1 - num2;

            result = String.valueOf(difference);

            return result;
        }

        public String multiply(double num1, double num2, String result)
        {
            double product;

            product = num1 * num2;

            result = String.valueOf(product);

            return result;
        }

        public String divide(double num1, double num2, String result)
        {
            if(num2 == 0)
            {
                result = "Can not divide by 0!";

                return result;
            }
            else
            {
                double quotient;

                quotient = num1 / num2;

                result = String.valueOf(quotient);

                return result;
            }
        }

        public String modulus(double num1, double num2, String result)
        {
            if(num2 == 0)
            {
                result = "Can not divide by 0!";

                return result;
            }
            else
            {
                double modulus;

                modulus = num1 % num2;

                result = String.valueOf(modulus);

                return result;
            }
        }
    }
}

Upvotes: 1

Related Questions