我正在构建一个计算器,我需要为按钮执行常规事件处理程序。以下问题阻止了我:
谢谢
这是我到目前为止所做的,但它不起作用:
scene.addEventFilter(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent mouseEvent) {
tres.pressedProperty().addListener((o, old, newValue) ->
Result.setText("3"));
DisplayingText = Result.getText();
System.out.println(DisplayingText);
if(Result.getText().equals("3"))
{
System.out.println("mouse click detected! ");
}
}
});
答案 0 :(得分:0)
您可以使用按钮javafx.scene.control.Button
对场景进行数字和操作。
按下按钮的操作句柄如下:
button.setOnAction((ActionEvent e) -> {
displayingText = result.getText();
});
答案 1 :(得分:0)
此应用程序模拟了一个淡化计算器。它显示了如何使用TextField
的{{1}}。它还演示了如何使用一个appendText
来处理多个EventHandler
。
主要
Nodes
FXML
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* @author blj0011
*/
public class WateredDownCalCulator 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);
}
}
控制器
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>
<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.111" fx:controller="watereddowncalculator.FXMLDocumentController">
<children>
<VBox prefHeight="200.0" prefWidth="100.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<TextField fx:id="tfDisplay">
<VBox.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</VBox.margin>
</TextField>
<StackPane prefHeight="150.0" prefWidth="200.0">
<children>
<GridPane maxHeight="100.0" maxWidth="100.0" prefHeight="50.0" prefWidth="50.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="50.0" prefWidth="50.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="50.0" prefWidth="50.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints vgrow="SOMETIMES" />
<RowConstraints vgrow="SOMETIMES" />
<RowConstraints vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#handleButtonAction" text="1">
<GridPane.margin>
<Insets bottom="2.0" left="2.0" right="2.0" top="2.0" />
</GridPane.margin>
</Button>
<Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#handleButtonAction" text="2" GridPane.columnIndex="1">
<GridPane.margin>
<Insets bottom="2.0" left="2.0" right="2.0" top="2.0" />
</GridPane.margin>
</Button>
<Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#handleButtonAction" text="3" GridPane.rowIndex="1">
<GridPane.margin>
<Insets bottom="2.0" left="2.0" right="2.0" top="2.0" />
</GridPane.margin>
</Button>
<Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#handleButtonAction" text="+" GridPane.columnIndex="1" GridPane.rowIndex="1">
<GridPane.margin>
<Insets bottom="2.0" left="2.0" right="2.0" top="2.0" />
</GridPane.margin>
</Button>
<Button maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#handleButtonAction" text="=" GridPane.columnSpan="2" GridPane.rowIndex="2" />
</children>
</GridPane>
</children>
</StackPane>
</children>
</VBox>
</children>
</AnchorPane>
Pure Code Version
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
/**
*
* @author blj0011
*/
public class FXMLDocumentController implements Initializable
{
@FXML TextField tfDisplay;//TextField create using FXML
//Method use the handle the different buttons' action.
@FXML
private void handleButtonAction(ActionEvent event)
{
Button tempButton = ((Button)event.getSource());//Get the Button that is being pressed
String currentButtonPress = tempButton.getText();//Get the text of the button that is being pressed
//Depending on the Button text do some action
switch(currentButtonPress)
{
case "=":
//do some calculation
break;
case "+":
tfDisplay.appendText(currentButtonPress);
break;
default:
tfDisplay.appendText(currentButtonPress);
}
}
@Override
public void initialize(URL url, ResourceBundle rb)
{
// TODO
}
}
您可能希望以不同的方式处理import javafx.application.*;
import javafx.event.*;
import javafx.geometry.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.*;
/**
*
* @author Sedrick
*/
public class JavaFXApplication60 extends Application {
@Override
public void start(Stage primaryStage)
{
AnchorPane root = new AnchorPane();
VBox vBox = new VBox();
AnchorPane.setBottomAnchor(vBox, 0.0);
AnchorPane.setTopAnchor(vBox, 0.0);
AnchorPane.setRightAnchor(vBox, 0.0);
AnchorPane.setLeftAnchor(vBox, 0.0);
root.getChildren().add(vBox);
TextField display = new TextField();
display.setEditable(false);//Don't allow the TextField to be editable.
vBox.getChildren().add(display);
VBox.setMargin(display, new Insets(10, 10, 10, 10));
StackPane stackPane = new StackPane();
stackPane.setPrefSize(200, 150);
vBox.getChildren().add(stackPane);
GridPane gridPane = new GridPane();
//gridPane.gridLinesVisibleProperty().set(true);
gridPane.setMinSize(50, 50);
gridPane.setPrefSize(50, 50);
gridPane.setMaxSize(100, 100);
gridPane.setVgap(2);
gridPane.setHgap(2);
RowConstraints rowConstraints = new RowConstraints();
rowConstraints.setVgrow(Priority.SOMETIMES);
gridPane.getRowConstraints().addAll(rowConstraints, rowConstraints, rowConstraints);
ColumnConstraints columnConstraints = new ColumnConstraints();
columnConstraints.setHgrow(Priority.SOMETIMES);
columnConstraints.setMinWidth(50);
columnConstraints.setPrefWidth(50);
gridPane.getColumnConstraints().addAll(columnConstraints, columnConstraints);
stackPane.getChildren().add(gridPane);
//Create Buttons
Button btn1 = new Button("1");
Button btn2 = new Button("2");
Button btn3 = new Button("3");
Button btnPlus = new Button("+");
Button btnEqual = new Button("=");
btn1.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
btn2.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
btn3.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
btnPlus.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
btnEqual.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
//Create event handler
EventHandler handle = new EventHandler() {
@Override
public void handle(Event event)
{
Button tempButton = ((Button) event.getSource());//Get the Button that is being pressed
String currentButtonPress = tempButton.getText();//Get the text of the button that is being pressed
//Depending on the Button text do some action
switch (currentButtonPress) {
case "=":
display.setText("results!");
break;
case "+":
display.appendText(currentButtonPress);
break;
default:
display.appendText(currentButtonPress);
}
}
};
//Set buttons' handlers;
btn1.setOnAction(handle);
btn2.setOnAction(handle);
btn3.setOnAction(handle);
btnPlus.setOnAction(handle);
btnEqual.setOnAction(handle);
gridPane.add(btn1, 0, 0, 1, 1);
gridPane.add(btn2, 1, 0, 1, 1);
gridPane.add(btn3, 0, 1, 1, 1);
gridPane.add(btnPlus, 1, 1, 1, 1);
gridPane.add(btnEqual, 0, 2, 2, 1);
Scene scene = new Scene(root, 300, 220);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}
操作。
答案 2 :(得分:0)
@Arturo,我记得你的申请。
对于#1子问题:没有想法,不清楚最终目标。
对于#2子问题:您已在结果字段中显示了值。
对于#3子问题:要附加新的按下值,您可以执行下一个:
tres.pressedProperty().addListener((o, old, newValue) -> {
if (newValue) {
if ("0".equals(Result.getText()) {
Result.setText("3");
} else {
Result.appendText("3");
}
}
});
结果你会得到:&#34; 11113&#34;
修改:从第一个零添加保护。为避免每个按钮上的重复,您可以将if / else提取到私有方法中并调用updateResult(&#34; 3&#34;);