2017-03-07 13 views
0

テキストフィールドからデータを入力するには TextFieldの高さから入力データをHEIGHTにする必要があります。 ボタンを作成しました。このボタンをクリックすると、Textfieldは日付を変数に入力する必要があります。 このブロックをコーディングしますが、機能しません。JavafxでTextfieldからdouble型にデータを入力するには?

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> 
+1

静的ではないでしょうか? – PendingValue

+0

HEIGHT = 0、ターミナルプリントで "eror"(試してみる) –

+0

シーンビルダを使用していますか? – minigeek

答えて

0

あなたがシーンBuilderを使用している場合。メソッドがButton idに関連付けられているかどうかを確認してください。ここで実行できます。コードで指定したidをbutton-nameとし、メソッド名をクリックするか、メソッド名を入力します。 作成していない場合は作成してください。

私たちはコントローラーをリンクして、fxmlコードでIDを渡すことを忘れてしまいます。ここ は、このような場合のための修正です:

enter image description here

また、あなたのコード内のすべての静的フィールドは、それが動作しない "ときに、それを何

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; 
+0

私はそれを行います。私のfxmlファイルメッセージ –

+0

@VadimMarchenkoコントローラのクラスコードをここに貼り付けることができますか(質問またはペーストビン) – minigeek

+0

はい、私はそれを追加しました –

関連する問題