2017-03-29 1 views
0

안녕하세요 모두 버튼에서 ActionEvent를 만들고 TextField에 버튼 이름을 입력해야합니다. 내가 가지고있는 것은 일종의 계산기이지만 버튼을 누르면 TextField가 채워지지 않습니다. 내가 원하는 것의 예를 들면 : 버튼 하나에 numer가 있고 그것을 누르면 TextField에 숫자 1이 나타납니다. 이것은 내가 무엇을 가지고 지금까지 :ActionEvent 버튼 - TextField JavaFX

AppCalculator 클래스 :

package appcalculator; 

import javafx.application.Application; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.stage.Stage; 

public class AppCalculator extends Application { 

@Override 
public void start(Stage stage) throws Exception { 
    Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml")); 

    Scene scene = new Scene(root); 

    stage.setScene(scene); 
    stage.show(); 
} 

public static void main(String[] args) { 
    launch(args); 
} 

} 

FXMLDocumentController :

package appcalculadora; 

import java.net.URL; 
import java.util.ResourceBundle; 
import javafx.event.ActionEvent; 
import javafx.fxml.FXML; 
import javafx.fxml.Initializable; 
import javafx.scene.control.TextField; 

public class FXMLDocumentController implements Initializable { 


TextField texto = new TextField(); 


@FXML 
private void btnOne(ActionEvent event) { 
    texto.setText("1"); 

} 

@FXML 
private void btnTwo(ActionEvent event) { 
    texto.setText("2"); 
} 

@FXML 
private void btnThree(ActionEvent event) { 
    texto.setText("3"); 
} 

@FXML 
private void btnFour(ActionEvent event) { 
    texto.setText("4"); 
} 

@FXML 
private void btnFive(ActionEvent event) { 
    texto.setText("5"); 
} 

@FXML 
private void btnSix(ActionEvent event) { 
    texto.setText("6"); 
} 

@FXML 
private void btnSevent(ActionEvent event) { 
    texto.setText("7"); 
} 

@FXML 
private void btnEight(ActionEvent event) { 
    texto.setText("8"); 
} 

@FXML 
private void btnNine(ActionEvent event) { 
    texto.setText("9"); 
} 

@FXML 
private void btnZero(ActionEvent event) { 
    texto.setText("0"); 
} 

@FXML 
private void btnPoint(ActionEvent event) { 
    texto.setText("."); 
} 

@FXML 
private void btnPlus(ActionEvent event) { 
    texto.setText("+"); 
} 

@FXML 
private void btnMinus(ActionEvent event) { 
    texto.setText("-"); 
} 

@FXML 
private void btnBy(ActionEvent event) { 
    texto.setText("*"); 
} 

@FXML 
private void btnDivided(ActionEvent event) { 
    texto.setText("/"); 
} 

@FXML 
private void btnErrase(ActionEvent event){ 
    texto.deletePreviousChar(); 
} 

@FXML 
private void btnTotal(ActionEvent event){ 
    texto.getText(); 
} 

@Override 
public void initialize(URL url, ResourceBundle rb) { 
    // TODO 
}  

} 

그리고 내 각각의 FXML 문서

+1

FXML은 어디에 있습니까? 'texto'는 어디서나 UI에 표시되는 것처럼 보이지 않으므로 버튼을 눌러도 결과가 표시되지 않으면 놀랄만 한 일이 아닙니다. –

+0

또한 많은 개인 메서드가있는 대신 하나의 메서드로 대부분의 단추를 처리 할 수 ​​있습니다. – Sedrick

답변

1

답변에 도움이 될 수 있습니다. @ James_D처럼 '@FXML private TextField texto;가 없습니다.' 또한 대부분의 방법을 없애 버릴 수 있습니다.

주 :

import javafx.application.Application; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.stage.Stage; 

/** 
* 
* @author blj0011 
*/ 
public class JavaFXApplication58 extends Application 
{ 

    @Override 
    public void start(Stage stage) throws Exception 
    { 
     Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml")); 

     Scene scene = new Scene(root); 

     stage.setScene(scene); 
     stage.show(); 
    } 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) 
    { 
     launch(args); 
    } 
} 

컨트롤러 :

import java.net.URL; 
import java.util.ResourceBundle; 
import javafx.event.ActionEvent; 
import javafx.fxml.FXML; 
import javafx.fxml.Initializable; 
import javafx.scene.control.*; 

/** 
* 
* @author blj0011 
*/ 
public class FXMLDocumentController implements Initializable 
{ 

    @FXML 
    private TextField texto; 


    //Use this approach to replace most of your private button handlers! 
    @FXML 
    private void handleButtonAction(ActionEvent event) 
    { 
     Button button = (Button)event.getSource(); 
     texto.appendText(button.getText()); 
    } 

    @Override 
    public void initialize(URL url, ResourceBundle rb) 
    { 
     // TODO 
    }  
} 

FXML :

012,351,641 여기

샘플 프로그램
<?xml version="1.0" encoding="UTF-8"?> 

<?import javafx.scene.control.Button?> 
<?import javafx.scene.control.TextField?> 
<?import javafx.scene.layout.AnchorPane?> 

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.111" fx:controller="javafxapplication58.FXMLDocumentController"> 
    <children> 
     <Button fx:id="btnOne" layoutX="126" layoutY="90" onAction="#handleButtonAction" text="1" /> 
     <Button fx:id="btnTwo" layoutX="172.0" layoutY="90.0" onAction="#handleButtonAction" text="2" /> 
     <TextField fx:id="texto" layoutX="86.0" layoutY="43.0" /> 
    </children> 
</AnchorPane> 
-2
public void actionPerformed(ActionEvent e) { 

String x = TextFieldName.getText(); 
if(e.getSource() == ButtonName) 
TextFieldName.setText(""); 
관련 문제