본문 바로가기

Programming/Java_Etc

자바(JavaFX) - 계산기

반응형

1. main.java

package application;
	
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;


public class Main extends Application {
	@Override
	public void start(Stage primaryStage) {
		try {
			// fxml layout loader
//			FXMLLoader loader = new FXMLLoader();
//			loader.setLocation(getClass().getResource("MainView.fxml"));
//			// window root
//			BorderPane mainLayoutAnchorPane = (BorderPane) loader.load();
//			Scene scene = new Scene(mainLayoutAnchorPane);
//			scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
//			primaryStage.setScene(scene);
						
			Parent root = FXMLLoader.load(getClass().getResource("MainView.fxml"));			
			primaryStage.setScene(new Scene(root));
			
			primaryStage.setTitle("first javafx");			
			primaryStage.show();
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		launch(args);
	}
}

2. MainView.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.shape.Rectangle?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>


<VBox fx:controller="application.Controller"  maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="500.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <StackPane prefHeight="150.0" prefWidth="380.0">
         <children>
            <Rectangle arcHeight="5.0" arcWidth="5.0" fill="#b8d2e4" height="120.0" stroke="BLACK" strokeType="INSIDE" width="300.0" />
            <Text fx:id="result" strokeType="OUTSIDE" strokeWidth="0.0" text="0" textAlignment="CENTER">
               <font>
                  <Font size="51.0" />
               </font>
            </Text>
         </children>
      </StackPane>
      <HBox alignment="TOP_CENTER" prefHeight="70.0" prefWidth="200.0">
         <children>
            <Button alignment="TOP_CENTER" minWidth="-Infinity" mnemonicParsing="false" prefHeight="60.0" prefWidth="60.0" text="7" textAlignment="CENTER" onAction="#operand">
               <font>
                  <Font size="27.0" />
               </font>
               <HBox.margin>
                  <Insets right="10.0" />
               </HBox.margin>
            </Button>
            <Button alignment="TOP_CENTER" minWidth="-Infinity" mnemonicParsing="false" prefHeight="60.0" prefWidth="60.0" text="8" textAlignment="CENTER" onAction="#operand">
               <font>
                  <Font size="27.0" />
               </font>
               <HBox.margin>
                  <Insets right="10.0" />
               </HBox.margin>
            </Button>
            <Button alignment="TOP_CENTER" minWidth="-Infinity" mnemonicParsing="false" prefHeight="60.0" prefWidth="60.0" text="9" textAlignment="CENTER" onAction="#operand">
               <font>
                  <Font size="27.0" />
               </font>
               <HBox.margin>
                  <Insets right="10.0" />
               </HBox.margin>
            </Button>
            <Button alignment="TOP_CENTER" minWidth="-Infinity" mnemonicParsing="false" prefHeight="60.0" prefWidth="60.0" text="/" textAlignment="CENTER" onAction="#operator">
               <font>
                  <Font size="27.0" />
               </font>
            </Button>
         </children>
         <VBox.margin>
            <Insets bottom="10.0" />
         </VBox.margin>
      </HBox>
      <HBox alignment="TOP_CENTER" prefHeight="70.0" prefWidth="200.0">
         <children>
            <Button alignment="TOP_CENTER" minWidth="-Infinity" mnemonicParsing="false" prefHeight="60.0" prefWidth="60.0" text="4" textAlignment="CENTER" onAction="#operand">
               <font>
                  <Font size="27.0" />
               </font>
               <HBox.margin>
                  <Insets right="10.0" />
               </HBox.margin>
            </Button>
            <Button alignment="TOP_CENTER" minWidth="-Infinity" mnemonicParsing="false" prefHeight="60.0" prefWidth="60.0" text="5" textAlignment="CENTER" onAction="#operand">
               <font>
                  <Font size="27.0" />
               </font>
               <HBox.margin>
                  <Insets right="10.0" />
               </HBox.margin>
            </Button>
            <Button alignment="TOP_CENTER" minWidth="-Infinity" mnemonicParsing="false" prefHeight="60.0" prefWidth="60.0" text="6" textAlignment="CENTER" onAction="#operand">
               <font>
                  <Font size="27.0" />
               </font>
               <HBox.margin>
                  <Insets right="10.0" />
               </HBox.margin>
            </Button>
            <Button alignment="TOP_CENTER" minWidth="-Infinity" mnemonicParsing="false" prefHeight="60.0" prefWidth="60.0" text="*" textAlignment="CENTER" onAction="#operator">
               <font>
                  <Font size="27.0" />
               </font>
            </Button>
         </children>
         <VBox.margin>
            <Insets bottom="10.0" />
         </VBox.margin>
      </HBox>
      <HBox alignment="TOP_CENTER" prefHeight="70.0" prefWidth="200.0">
         <children>
            <Button alignment="TOP_CENTER" minWidth="-Infinity" mnemonicParsing="false" prefHeight="60.0" prefWidth="60.0" text="1" textAlignment="CENTER" onAction="#operand">
               <font>
                  <Font size="27.0" />
               </font>
               <HBox.margin>
                  <Insets right="10.0" />
               </HBox.margin>
            </Button>
            <Button alignment="TOP_CENTER" minWidth="-Infinity" mnemonicParsing="false" prefHeight="60.0" prefWidth="60.0" text="2" textAlignment="CENTER" onAction="#operand">
               <font>
                  <Font size="27.0" />
               </font>
               <HBox.margin>
                  <Insets right="10.0" />
               </HBox.margin>
            </Button>
            <Button alignment="TOP_CENTER" minWidth="-Infinity" mnemonicParsing="false" prefHeight="60.0" prefWidth="60.0" text="3" textAlignment="CENTER" onAction="#operand">
               <font>
                  <Font size="27.0" />
               </font>
               <HBox.margin>
                  <Insets right="10.0" />
               </HBox.margin>
            </Button>
            <Button alignment="TOP_CENTER" minWidth="-Infinity" mnemonicParsing="false" prefHeight="60.0" prefWidth="60.0" text="-" textAlignment="CENTER" onAction="#operator">
               <font>
                  <Font size="27.0" />
               </font>
            </Button>
         </children>
         <VBox.margin>
            <Insets bottom="10.0" />
         </VBox.margin>
      </HBox>
      <HBox alignment="TOP_CENTER" prefHeight="70.0" prefWidth="200.0">
         <children>
            <Button alignment="TOP_CENTER" minWidth="-Infinity" mnemonicParsing="false" prefHeight="60.0" prefWidth="130.0" text="0" textAlignment="CENTER" onAction="#operand">
               <font>
                  <Font size="27.0" />
               </font>
               <HBox.margin>
                  <Insets right="10.0" />
               </HBox.margin>
            </Button>
            <Button alignment="TOP_CENTER" minWidth="-Infinity" mnemonicParsing="false" prefHeight="60.0" prefWidth="60.0" text="=" textAlignment="CENTER" onAction="#operator">
               <font>
                  <Font size="27.0" />
               </font>
               <HBox.margin>
                  <Insets right="10.0" />
               </HBox.margin>
            </Button>
            <Button alignment="TOP_CENTER" minWidth="-Infinity" mnemonicParsing="false" prefHeight="60.0" prefWidth="60.0" text="+" textAlignment="CENTER" onAction="#operator">
               <font>
                  <Font size="27.0" />
               </font>
            </Button>
         </children>
         <VBox.margin>
            <Insets bottom="10.0" />
         </VBox.margin>
      </HBox>
   </children>
</VBox>

 

3. Controller.java

package application;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.text.Text;

public class Controller {
	private String operator = "";
	private int x = 0; // 기존의 숫자
	private Model model = new Model();
	
	@FXML
	private Text result;

	@FXML
	private void operand(ActionEvent event) {
		result.setText(result.getText() + ((Button) event.getSource()).getText());
	}
	
	@FXML
	private void operator(ActionEvent event) {
		if(((Button) event.getSource()).getText().equals("=")) {
			result.setText(model.calculator(operator, x, Integer.parseInt(result.getText())) + "");;			
		} else {
			operator = ((Button) event.getSource()).getText();
			x = Integer.parseInt(result.getText());
			result.setText("");
		}
	}
}

4. Model.java

package application;

public class Model {
	
	public int calculator(String operator, int x, int y ) {
		if(operator.contentEquals("+")) {
			return x+y;
		}
		else if(operator.contentEquals("-")) {
			return x-y;
		}
		else if(operator.contentEquals("*")) {
			return x*y;
		}
		else {
			return x / y;
		}
	}
}

 

<결과>

반응형