CHU1TA26 6 年之前
父節點
當前提交
e6bfa35d1e
共有 3 個檔案被更改,包括 41 行新增17 行删除
  1. 9
    2
      pom.xml
  2. 16
    4
      src/main/java/rocks/zipcode/calcskin/CalcEngine.java
  3. 16
    11
      src/main/java/rocks/zipcode/calcskin/CalcSkin.java

+ 9
- 2
pom.xml 查看文件

@@ -13,8 +13,8 @@
13 13
                 <groupId>org.apache.maven.plugins</groupId>
14 14
                 <artifactId>maven-compiler-plugin</artifactId>
15 15
                 <configuration>
16
-                    <source>1.7</source>
17
-                    <target>1.7</target>
16
+                    <source>1.8</source>
17
+                    <target>1.8</target>
18 18
                 </configuration>
19 19
             </plugin>
20 20
         </plugins>
@@ -25,6 +25,13 @@
25 25
             <artifactId>junit</artifactId>
26 26
             <version>4.0</version>
27 27
         </dependency>
28
+        <!-- https://mvnrepository.com/artifact/com.zenjava/javafx-maven-plugin -->
29
+        <dependency>
30
+            <groupId>com.zenjava</groupId>
31
+            <artifactId>javafx-maven-plugin</artifactId>
32
+            <version>8.8.3</version>
33
+        </dependency>
34
+
28 35
     </dependencies>
29 36
 
30 37
 

+ 16
- 4
src/main/java/rocks/zipcode/calcskin/CalcEngine.java 查看文件

@@ -6,18 +6,30 @@ public class CalcEngine {
6 6
     }
7 7
 
8 8
     public double add(double v, double v1) {
9
-        return Double.NaN;
9
+        return v + v1;
10 10
     }
11 11
 
12 12
     public double subtract(double v, double v1) {
13
-        return Double.NaN;
13
+        return v-v1;
14 14
     }
15 15
 
16 16
     public double multiply(double v, double v1) {
17
-        return Double.NaN;
17
+        return v * v1;
18 18
     }
19 19
 
20 20
     public double divide(double v, double v1) {
21
-        return Double.NaN;
21
+        return v/v1;
22 22
     }
23
+    public double square ( double v){
24
+        return Math.pow(v , 2.0);
25
+    }
26
+    public double root(double v){return Math.sqrt(v); }
27
+
28
+    public double expo ( double v , double v1){
29
+        return Math.pow(v,v1);
30
+    }
31
+
32
+
33
+
34
+
23 35
 }

+ 16
- 11
src/main/java/rocks/zipcode/calcskin/CalcSkin.java 查看文件

@@ -12,7 +12,6 @@ import javafx.scene.input.KeyEvent;
12 12
 import javafx.scene.layout.*;
13 13
 import javafx.stage.Stage;
14 14
 import javafx.stage.StageStyle;
15
-import rocks.zipcode.calcskin.CalcEngine;
16 15
 
17 16
 import java.util.HashMap;
18 17
 import java.util.Map;
@@ -24,9 +23,9 @@ public class CalcSkin extends Application {
24 23
         launch(args);
25 24
     }
26 25
     private static final String[][] template = {
27
-            { "7", "8", "9", "/" },
28
-            { "4", "5", "6", "*" },
29
-            { "1", "2", "3", "-" },
26
+            { "7", "8", "9", "/", "x^2" },
27
+            { "4", "5", "6", "*",  "rt" },
28
+            { "1", "2", "3", "-",  "x^y" },
30 29
             { "0", "c", "=", "+" }
31 30
     };
32 31
 
@@ -36,7 +35,7 @@ public class CalcSkin extends Application {
36 35
     private DoubleProperty currentValue = new SimpleDoubleProperty();
37 36
     private CalcEngine calcEngine = new CalcEngine();
38 37
 
39
-    private enum Op { NOOP, ADD, SUBTRACT, MULTIPLY, DIVIDE }
38
+    private enum Op { NOOP, ADD, SUBTRACT, MULTIPLY, DIVIDE, SQUARE, ROOT, EXPO }
40 39
 
41 40
     private Op curOp   = Op.NOOP;
42 41
     private Op stackOp = Op.NOOP;
@@ -60,7 +59,7 @@ public class CalcSkin extends Application {
60 59
     private VBox createLayout(TextField screen, TilePane buttons) {
61 60
         final VBox layout = new VBox(20);
62 61
         layout.setAlignment(Pos.CENTER);
63
-        layout.setStyle("-fx-background-color: silver; -fx-padding: 20; -fx-font-size: 20;");
62
+        layout.setStyle("-fx-background-color: green; -fx-padding: 20; -fx-font-size: 20;");
64 63
         layout.getChildren().setAll(screen, buttons);
65 64
         handleAccelerators(layout);
66 65
         screen.prefWidthProperty().bind(buttons.widthProperty());
@@ -81,7 +80,7 @@ public class CalcSkin extends Application {
81 80
 
82 81
     private TextField createScreen() {
83 82
         final TextField screen = new TextField();
84
-        screen.setStyle("-fx-background-color: aquamarine;");
83
+        screen.setStyle("-fx-background-color: black;");
85 84
         screen.setAlignment(Pos.CENTER_RIGHT);
86 85
         screen.setEditable(false);
87 86
         screen.textProperty().bind(Bindings.format("%.0f", currentValue));
@@ -127,12 +126,15 @@ public class CalcSkin extends Application {
127 126
             case "-": triggerOp.set(Op.SUBTRACT); break;
128 127
             case "*": triggerOp.set(Op.MULTIPLY); break;
129 128
             case "/": triggerOp.set(Op.DIVIDE);   break;
129
+            case "x^2": triggerOp.set(Op.SQUARE);   break;
130
+            case "rt": triggerOp.set(Op.ROOT);   break;
131
+            case "x^y": triggerOp.set(Op.EXPO);   break;
130 132
         }
131 133
         return triggerOp;
132 134
     }
133 135
 
134 136
     private void makeOperandButton(Button button, final ObjectProperty<Op> triggerOp) {
135
-        button.setStyle("-fx-base: lightgray;");
137
+        button.setStyle("-fx-base: lightblue;");
136 138
         button.setOnAction(new EventHandler<ActionEvent>() {
137 139
             @Override
138 140
             public void handle(ActionEvent actionEvent) {
@@ -143,7 +145,7 @@ public class CalcSkin extends Application {
143 145
 
144 146
     private Button makeStandardButton(String s) {
145 147
         Button button = new Button(s);
146
-        button.setStyle("-fx-base: beige;");
148
+        button.setStyle("-fx-base: pink;");
147 149
         accelerators.put(s, button);
148 150
         button.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
149 151
         return button;
@@ -166,7 +168,7 @@ public class CalcSkin extends Application {
166 168
     }
167 169
 
168 170
     private void makeClearButton(Button button) {
169
-        button.setStyle("-fx-base: mistyrose;");
171
+        button.setStyle("-fx-base: orange;");
170 172
         button.setOnAction(new EventHandler<ActionEvent>() {
171 173
             @Override
172 174
             public void handle(ActionEvent actionEvent) {
@@ -176,7 +178,7 @@ public class CalcSkin extends Application {
176 178
     }
177 179
 
178 180
     private void makeEqualsButton(Button button) {
179
-        button.setStyle("-fx-base: ghostwhite;");
181
+        button.setStyle("-fx-base: violet;");
180 182
         button.setOnAction(new EventHandler<ActionEvent>() {
181 183
             @Override
182 184
             public void handle(ActionEvent actionEvent) {
@@ -185,6 +187,9 @@ public class CalcSkin extends Application {
185 187
                     case SUBTRACT: currentValue.set(calcEngine.subtract(previousValue.get(), currentValue.get())); break;
186 188
                     case MULTIPLY: currentValue.set(calcEngine.multiply(previousValue.get(), currentValue.get())); break;
187 189
                     case DIVIDE:   currentValue.set(calcEngine.divide(previousValue.get(), currentValue.get())); break;
190
+                    case ROOT:   currentValue.set(calcEngine.root( currentValue.get())); break;
191
+                    case SQUARE:   currentValue.set(calcEngine.square( currentValue.get())); break;
192
+                    case EXPO:   currentValue.set(calcEngine.expo(previousValue.get(), currentValue.get())); break;
188 193
                 }
189 194
             }
190 195
         });