我在JavaFx中看到过Rectangle的例子。但是请任何人可以在输出窗口/场景中提供示例,如果用户放置了所需的宽度和高度,则应自动生成矩形。 这是我的例子
VBox vb = new VBox(20);
HBox h1 = new HBox(7);
HBox h2 = new HBox(7);
Label lebel1 = new Label("X:");
Label lebel2 = new Label("Y:");
TextField txt1 = new TextField();
TextField txt2 = new TextField();
//Converting textfield to integer only
ChangeListener<String> forceNumberListener = (observable, oldValue, newValue) -> {
if (!newValue.matches("\\d*"))
((StringProperty) observable).set(oldValue);
};
txt1.textProperty().addListener(forceNumberListener);
txt2.textProperty().addListener(forceNumberListener);
double width = Double.parseDouble(txt1.getText());
double height = Double.parseDouble(txt2.getText());
Rectangle rect1 = new Rectangle();
rect1.setHeight(height);
rect1.setWidth(width);
h1.getChildren().addAll(lebel1, txt1);
h2.getChildren().addAll(lebel2, txt2);
vb.getChildren().addAll(h1,h2,rect1);
如果用户将“x”中的任何整数值设置为width,将“y”设置为height,则应在字段下方生成Rectangle。但是这段代码错了,我不知道其他方法。请
非常感谢
答案 0 :(得分:1)
您应该使用TextField的onkeyreleased事件处理程序。在此应用程序中,如果两个TextField都输入了数字,则会生成一个矩形。两个TextField都有一个事件处理程序,如果其中一个文本被更改,它会执行相同的操作。这不会捕获任何非双重值。
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
/**
*
* @author blj0011
*/
public class JavaFXApplication104 extends Application
{
@Override
public void start(Stage primaryStage)
{
BorderPane root = new BorderPane();
VBox vbox = new VBox();
vbox.setMinWidth(100);
TextField textfield1 = new TextField();
TextField textfield2 = new TextField();
textfield1.setPrefWidth(50);
textfield1.setPromptText("Enter height");
textfield1.setOnKeyReleased(new EventHandler<KeyEvent>(){
@Override
public void handle(KeyEvent event)
{
if(textfield1.getText().length() > 0 && textfield2.getText().length() > 0)
{
Rectangle rectangle = new Rectangle();
rectangle.setHeight(Double.parseDouble(textfield1.getText()));
rectangle.setWidth(Double.parseDouble(textfield2.getText()));
rectangle.setFill(Color.BLUE);
root.setCenter(rectangle);
}
}
});
textfield2.setPrefWidth(100);
textfield2.setPromptText("Enter length");
textfield2.setOnKeyReleased(new EventHandler<KeyEvent>(){
@Override
public void handle(KeyEvent event)
{
if(textfield1.getText().length() > 0 && textfield2.getText().length() > 0)
{
Rectangle rectangle = new Rectangle();
rectangle.setHeight(Double.parseDouble(textfield1.getText()));
rectangle.setWidth(Double.parseDouble(textfield2.getText()));
rectangle.setFill(Color.BLUE);
root.setCenter(rectangle);
}
}
});
vbox.getChildren().addAll(textfield1, textfield2);
root.setLeft(vbox);
Scene scene = new Scene(root, 500, 500);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}