将 VBox 和 StackPane 放入另一个 StackPane 后按钮不起作用
Button isn't working after putting a VBox and a StackPane into another StackPane
我试图让 TextField
在屏幕中间,同时在它周围有一个 Circle
并且按钮在圆圈下方。所以我用圆圈和按钮做了一个 VBox
,用 TextField
做了一个 StackPane
。我做了另一个 StackPane
将 VBox
和 StackPane
放在同一个场景中,但现在按钮不起作用。我真的不知道是什么原因造成的。
以下是我到目前为止的代码。
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package circleandtextbox;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
/**
*
* @author diego
*/
public class CircleAndTextBox extends Application {
@Override
public void start(Stage primaryStage) {
int programX = 400;
int programY = 400;
int circleRadius = 100;
Circle circle = new Circle(programX / 2,programY / 2,circleRadius);
circle.setStroke(Color.RED);
circle.setStrokeWidth(3);
circle.setFill(Color.TRANSPARENT);
TextField input = new TextField();
input.setMaxWidth(100);
Button btn = new Button();
btn.setText("Change the size of the circle");
btn.setOnAction((ActionEvent event) -> {
System.out.println("It worked!");
});
VBox root = new VBox();
root.setAlignment(Pos.CENTER);
root.setSpacing(10);
root.getChildren().addAll(circle,btn);
StackPane textBox = new StackPane();
textBox.setAlignment(Pos.CENTER);
textBox.getChildren().add(input);
StackPane together = new StackPane();
together.getChildren().addAll(root,textBox);
Scene scene = new Scene(together, programX, programY);
primaryStage.setTitle("Circle code");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
你的第二个 StackPane
覆盖了你的 VBox
,隐藏了它的 Button
。看效果,改
together.getChildren().addAll(root, textBox);
到
together.getChildren().addAll(textBox , root);
当然,现在TextField
都涵盖了。相反,对圆和字段使用单个 StackPane
:
StackPane textBox = new StackPane();
textBox.setAlignment(Pos.CENTER);
textBox.getChildren().addAll(circle, input);
然后使用按钮将结果添加到 VBox
。
VBox root = new VBox();
root.setAlignment(Pos.CENTER);
root.setSpacing(10);
root.setPadding(new Insets(16));
root.getChildren().addAll(textBox, btn);
也可以考虑用一个Spinner
and/orSlider
,看到here,来控制大小。测试代码:
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
/**
* @author diego
* @see
* @see
*/
public class CircleAndTextBox extends Application {
@Override
public void start(Stage primaryStage) {
int programX = 400;
int programY = 400;
int circleRadius = 100;
Circle circle = new Circle(programX / 2,programY / 2,circleRadius);
circle.setStroke(Color.RED);
circle.setStrokeWidth(3);
circle.setFill(Color.TRANSPARENT);
TextField input = new TextField();
input.setMaxWidth(100);
Button btn = new Button();
btn.setText("Change the size of the circle");
btn.setOnAction((ActionEvent event) -> {
System.out.println("It worked!");
});
StackPane textBox = new StackPane();
textBox.setAlignment(Pos.CENTER);
textBox.getChildren().addAll(circle, input);
VBox root = new VBox();
root.setAlignment(Pos.CENTER);
root.setSpacing(10);
root.setPadding(new Insets(16));
root.getChildren().addAll(textBox, btn);
Scene scene = new Scene(root);
primaryStage.setTitle("Circle code");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
我试图让 TextField
在屏幕中间,同时在它周围有一个 Circle
并且按钮在圆圈下方。所以我用圆圈和按钮做了一个 VBox
,用 TextField
做了一个 StackPane
。我做了另一个 StackPane
将 VBox
和 StackPane
放在同一个场景中,但现在按钮不起作用。我真的不知道是什么原因造成的。
以下是我到目前为止的代码。
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package circleandtextbox;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
/**
*
* @author diego
*/
public class CircleAndTextBox extends Application {
@Override
public void start(Stage primaryStage) {
int programX = 400;
int programY = 400;
int circleRadius = 100;
Circle circle = new Circle(programX / 2,programY / 2,circleRadius);
circle.setStroke(Color.RED);
circle.setStrokeWidth(3);
circle.setFill(Color.TRANSPARENT);
TextField input = new TextField();
input.setMaxWidth(100);
Button btn = new Button();
btn.setText("Change the size of the circle");
btn.setOnAction((ActionEvent event) -> {
System.out.println("It worked!");
});
VBox root = new VBox();
root.setAlignment(Pos.CENTER);
root.setSpacing(10);
root.getChildren().addAll(circle,btn);
StackPane textBox = new StackPane();
textBox.setAlignment(Pos.CENTER);
textBox.getChildren().add(input);
StackPane together = new StackPane();
together.getChildren().addAll(root,textBox);
Scene scene = new Scene(together, programX, programY);
primaryStage.setTitle("Circle code");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
你的第二个 StackPane
覆盖了你的 VBox
,隐藏了它的 Button
。看效果,改
together.getChildren().addAll(root, textBox);
到
together.getChildren().addAll(textBox , root);
当然,现在TextField
都涵盖了。相反,对圆和字段使用单个 StackPane
:
StackPane textBox = new StackPane();
textBox.setAlignment(Pos.CENTER);
textBox.getChildren().addAll(circle, input);
然后使用按钮将结果添加到 VBox
。
VBox root = new VBox();
root.setAlignment(Pos.CENTER);
root.setSpacing(10);
root.setPadding(new Insets(16));
root.getChildren().addAll(textBox, btn);
也可以考虑用一个Spinner
and/orSlider
,看到here,来控制大小。测试代码:
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
/**
* @author diego
* @see
* @see
*/
public class CircleAndTextBox extends Application {
@Override
public void start(Stage primaryStage) {
int programX = 400;
int programY = 400;
int circleRadius = 100;
Circle circle = new Circle(programX / 2,programY / 2,circleRadius);
circle.setStroke(Color.RED);
circle.setStrokeWidth(3);
circle.setFill(Color.TRANSPARENT);
TextField input = new TextField();
input.setMaxWidth(100);
Button btn = new Button();
btn.setText("Change the size of the circle");
btn.setOnAction((ActionEvent event) -> {
System.out.println("It worked!");
});
StackPane textBox = new StackPane();
textBox.setAlignment(Pos.CENTER);
textBox.getChildren().addAll(circle, input);
VBox root = new VBox();
root.setAlignment(Pos.CENTER);
root.setSpacing(10);
root.setPadding(new Insets(16));
root.getChildren().addAll(textBox, btn);
Scene scene = new Scene(root);
primaryStage.setTitle("Circle code");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}