如何在Javafx中从Textfield向double输入数据?
如何从文本字段输入数据 我需要从文本字段高度到双倍高度的输入数据。 我创建了一个按钮。单击此按钮时,Textfield必须将日期输入变量。 我对这个块进行了编码,但它不起作用如何在Javafx中从Textfield向double输入数据?,java,javafx,textfield,Java,Javafx,Textfield,如何从文本字段输入数据 我需要从文本字段高度到双倍高度的输入数据。 我创建了一个按钮。单击此按钮时,Textfield必须将日期输入变量。 我对这个块进行了编码,但它不起作用 HEIGHT = 0,0 try { HEIGHT = Double.parseDouble(height.getText()); }catch(NullPointerException e) { System.out.println("eror"); } System.out
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);
}
}
*/
}
包装样品;
导入javafx.fxml.fxml;
导入javafx.scene.canvas.canvas;
导入javafx.scene.canvas.GraphicsContext;
导入javafx.scene.control.Button;
导入javafx.scene.control.ComboBox;
导入javafx.scene.control.TextField;
导入javafx.scene.paint.Color;
公共类控制器{
私有静态彩色图形;
私有静态int colorIndex,figureIndex;
私人静态双倍高度=30;
私有静态整数权重=50;
@FXML
公共静态文本字段高度;
@FXML
私有静态文本字段权重;
@FXML
专用组合框颜色菜单;
@FXML
专用组合框图形;
@FXML
私人帆布;
GraphicsContext gc;
@FXML
按钮去;
@FXML
公共图(){
//彩色菜单
colorMenue.setOnAction(事件->{
颜色指数=
colorMenue.getSelectionModel().getSelectedIndex();
});
//数字菜单
figureMenue.setOnAction(事件->{
figureIndex=figureMenue.getSelectionModel().getSelectedIndex();
});
打印图形(图索引);
系统输出打印LN(高度);
}
公共颜色开关颜色(int i){
开关(一){
案例0:
colorOfFigure=Color.YELLOW;
打破
案例1:
colorOfFigure=Color.GREEN;
打破
案例2:
colorOfFigure=Color.BLACK;
打破
案例3:
colorOfFigure=Color.RED;
打破
案例4:
colorOfFigure=Color.BLUE;
打破
}
返回图的颜色;
}
无效打印图形(整数图形索引){
gc=myCanvas.getGraphicsContext2D();
gc.setFill(switchColor(colorIndex));
gc.设定行程(开关颜色(颜色指数));
开关(图索引){
案例0:
gc.fillOval(187160100,高度);
打破
案例1:
gc.fillOval(187160100,高度);
打破
案例2:
gc.fillRect(187160100100);
打破
案例3:
gc.fillPolygon(新的双[]{10,30,10},
新的双[{210、210、240、240、}、4];
案例4:
gc.Fillova(187160100100);
打破
}
}
@FXML
公共画布(){
gc.setFill(颜色:薰衣草色);
gc.fillRect(0,0,myCanvas.getWidth(),myCanvas.getHeight());
}
@FXML
无效高度动作(){
试一试{
HEIGHT=Double.parseDouble(HEIGHT.getText());
}捕获(NullPointerException e){
系统输出打印项次(“eror”);
}
}
/*
公共无效初始化(){
试一试{
height.setOnAction(事件->height=Double.parseDouble(height.getText());
}捕获(NullPointerException e){
系统输出打印LN(高度);
}
}
*/
}
我的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>
如果您使用的是场景生成器。检查是否将方法附加到按钮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;
private Color图形;
私有int颜色索引,figureIndex;
私人双倍高度=30;
私有整数权重=50;
@FXML
公共文本字段高度;
@FXML
私有文本字段权重;
@FXML
专用组合框颜色菜单;
@FXML
专用组合框图形;
@FXML
私人帆布;
GraphicsContext gc;
@FXML
按钮去;
当它“不工作”时,它是什么?高度=0,并且在终端打印“eror”(try-catch)中,您使用的是场景生成器吗?什么是HEIGHT.getText()内容?@minigeek,是的。我用它来做。我的fxml文件,但请尝试捕获显示错误message@VadimMarchenko你能把你的控制器类代码粘贴到这里吗(有问题的或粘贴的)是的,我已经添加了it@VadimMarchenko为什么要将操作添加到textfield。Textfield不会对操作执行任何操作。请创建一个新按钮并将操作应用于按钮not Textfield;)@瓦丁马琴科:哦,我好像迟到了