如何從文本框輸入數據 我需要從TextField高度輸入數據到雙倍高度。 我創建了按鈕。當我點擊這個按鈕時,Textfield必須輸入日期到變量。 我編碼這個塊,但它不起作用。如何從Textfield輸入數據以在Javafx中翻倍?
HEIGHT = 0,0
try {
HEIGHT = Double.parseDouble(height.getText());
}catch(NullPointerException e) {
System.out.println("eror");
}
System.out.println(HEIGHT);
Controller.java
package sample;
import javafx.fxml.FXML;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.scene.paint.Color;
public class Controller{
private static Color colorOfFigure;
private static int colorIndex, figureIndex;
private static double HEIGHT = 30;
private static int WEIGHT = 50;
@FXML
public static TextField height;
@FXML
private static TextField weight;
@FXML
private ComboBox<String> colorMenue;
@FXML
private ComboBox<String> figureMenue;
@FXML
private Canvas myCanvas;
GraphicsContext gc;
@FXML
Button go;
@FXML
public void printFigure() {
//COLOR MENUE
colorMenue.setOnAction(event -> {
colorIndex =
colorMenue.getSelectionModel().getSelectedIndex();
});
//FIGURES MENUE
figureMenue.setOnAction(event -> {
figureIndex = figureMenue.getSelectionModel().getSelectedIndex();
});
printFigure(figureIndex);
System.out.println(HEIGHT);
}
public Color switchColor(int i) {
switch (i) {
case 0:
colorOfFigure = Color.YELLOW;
break;
case 1:
colorOfFigure = Color.GREEN;
break;
case 2:
colorOfFigure = Color.BLACK;
break;
case 3:
colorOfFigure = Color.RED;
break;
case 4:
colorOfFigure = Color.BLUE;
break;
}
return colorOfFigure;
}
void printFigure(int figureIndex) {
gc = myCanvas.getGraphicsContext2D();
gc.setFill(switchColor(colorIndex));
gc.setStroke(switchColor(colorIndex));
switch (figureIndex) {
case 0:
gc.fillOval(187, 160, 100, HEIGHT);
break;
case 1:
gc.fillOval(187, 160, 100, HEIGHT);
break;
case 2:
gc.fillRect(187, 160, 100, 100);
break;
case 3:
gc.fillPolygon(new double[]{10, 30, 10},
new double[]{210, 210, 240, 240,}, 4);
case 4:
gc.fillOval(187, 160, 100, 100);
break;
}
}
@FXML
public void cleanCanvas() {
gc.setFill(Color.LAVENDER);
gc.fillRect(0, 0, myCanvas.getWidth(), myCanvas.getHeight());
}
@FXML
void heightAction(){
try {
HEIGHT = Double.parseDouble(height.getText());
}catch(NullPointerException e) {
System.out.println("eror");
}
}
/*
public void initialize() {
try {
height.setOnAction(event -> HEIGHT = Double.parseDouble(height.getText()));
} catch (NullPointerException e) {
System.out.println(height);
}
}
*/
}
我FXML文件
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.String?>
<?import javafx.collections.FXCollections?>
<?import javafx.scene.canvas.Canvas?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" prefHeight="400.0" prefWidth="700.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<Button layoutX="26.0" layoutY="306.0" mnemonicParsing="false" onAction="#printFigure" prefHeight="26.0" prefWidth="158.0" text="print" />
<ComboBox fx:id="figureMenue" layoutX="34.0" layoutY="52.0" prefWidth="150.0" promptText=" none">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="Apple" />
<String fx:value="Orange" />
<String fx:value="Pear" />
<String fx:value="Triq" />
</FXCollections>
</items>
</ComboBox>
<Label layoutX="34.0" layoutY="26.0" text="Choose the figure" />
<ComboBox fx:id="colorMenue" layoutX="34.0" layoutY="135.0" prefWidth="150.0" promptText=" none">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="Yellow" />
<String fx:value="Green" />
<String fx:value="Black" />
<String fx:value="Red" />
<String fx:value="Blue" />
</FXCollections>
</items>
</ComboBox>
<Label layoutX="34.0" layoutY="111.0" text="Choose the color" />
<Canvas fx:id="myCanvas" height="400.0" layoutX="209.0" layoutY="2.0" width="499.0" />
<Separator layoutX="206.0" layoutY="-11.0" orientation="VERTICAL" prefHeight="365.0" prefWidth="6.0" />
<Label layoutX="71.0" layoutY="164.0" text="figure's size" />
<Button layoutX="26.0" layoutY="341.0" mnemonicParsing="false" onAction="#cleanCanvas" prefHeight="25.0" prefWidth="158.0" text="clean" textFill="#f20000" />
<TextField fx:id="height" onAction="#heightAction" layoutX="32.0" layoutY="188.0" prefHeight="26.0" prefWidth="69.0" text="HEIGHT" />
<TextField fx:id="weight" layoutX="115.0" layoutY="188.0" prefHeight="26.0" prefWidth="69.0" text="WEIGHT" />
<TextField alignment="CENTER" layoutX="30.0" layoutY="257.0" prefHeight="26.0" prefWidth="69.0" text="X" />
<TextField alignment="CENTER" layoutX="118.0" layoutY="256.0" prefHeight="26.0" prefWidth="69.0" text="Y" />
<Label layoutX="50.0" layoutY="235.0" text="figure's position" />
</children>
</AnchorPane>
這是什麼,所以當它不工作「? – PendingValue
HEIGHT = 0,並在終端打印「eror」(try -catch) –
您是否使用場景構建器? – minigeek