如何从Textfield输入数据 我需要TextField高度的输入数据加倍HEIGHT。 我创建了按钮。当我单击此按钮时,Textfield必须输入日期到变量。 我编写了这个块,但它不起作用。
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>
答案 0 :(得分:0)
如果您使用的是Scene Builder。检查您的方法是否附加到按钮ID。您可以在此处执行此操作。将id作为按钮名称提供给代码,然后单击方法名称或键入方法名称。 如果您尚未创建任何此类方法,请创建它。
碰巧我们忘记链接我们的控制器并在fxml代码中给出id。 这是对这种情况的修复:
此外,代码中的所有静态字段都不应是静态的
private Color colorOfFigure;
private int colorIndex, figureIndex;
private double HEIGHT = 30;
private int WEIGHT = 50;
@FXML
public TextField height;
@FXML
private TextField weight;
@FXML
private ComboBox<String> colorMenue;
@FXML
private ComboBox<String> figureMenue;
@FXML
private Canvas myCanvas;
GraphicsContext gc;
@FXML
Button go;