所以我有这个按钮。它可以用回车键按下,并可以通过Tab键选择它,但不能点击它,并且就光标而言似乎不存在。我不相信任何东西都可以覆盖它,但大部分内容都是如此,因为使用相同代码创建的按钮是可点击的。
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
primaryStage.setTitle("Mortgage Calculator");
GridPane pane = new GridPane();
pane.setAlignment(Pos.CENTER);
pane.setHgap(10);
pane.setVgap(10);
pane.setPadding(new Insets(25, 25, 25, 25));
Scene scene = new Scene(pane, 350, 325);
Text sceneTitle = new Text("Mortgage Calculator");
sceneTitle.setFont(Font.font("Arial", FontWeight.NORMAL, 20));
pane.add(sceneTitle, 0, 0, 2, 1);
Label total = new Label("Value of your Mortgage:");
pane.add(total, 0, 1);
final TextField totalField = new TextField();
pane.add(totalField, 1, 1);
Label percent = new Label("% Interest:");
pane.add(percent, 0, 2);
final TextField percentField = new TextField();
pane.add(percentField, 1, 2);
Label time = new Label("Length of mortgage:");
pane.add(time, 0, 3);
final TextField timeField = new TextField();
pane.add(timeField, 1, 3);
final Text amountOwed = new Text();
pane.add(amountOwed, 1, 7);
final Text payment = new Text();
pane.add(payment, 1, 8);
Button calculateButton = new Button("Calculate");
HBox hbox1 = new HBox(10);
hbox1.setAlignment(Pos.BOTTOM_RIGHT);
hbox1.getChildren().add(calculateButton);
calculateButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
double principal = Double.parseDouble(totalField.getText());
double interest = (Double.parseDouble(percentField.getText()) / 100) / 12;
int time = Integer.parseInt(timeField.getText());
int numPayment = time * 12;
double monthlyPayment = (principal * (interest * (Math.pow((interest + 1), numPayment)))) / (Math.pow((1 + interest), numPayment) - 1);
//double totalPayment = ;
//amountOwed.setText("Amount owed is: " + totalPayment);
payment.setText("Monthly payment is: $" + (int)Math.ceil(monthlyPayment));
}
});
pane.add(hbox1, 1, 4);
Button clearButton = new Button("Clear");
HBox hbox = new HBox(10);
hbox.setAlignment(Pos.BOTTOM_LEFT);
hbox.getChildren().add(clearButton);
pane.add(hbox, 1, 4);
EventHandler<ActionEvent> handler1 = new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
totalField.setText("");
percentField.setText("");
timeField.setText("");
payment.setText("");
}
};
clearButton.setOnAction(handler1);
primaryStage.setScene(scene);
primaryStage.show();
}
}
答案 0 :(得分:6)
mouseTransparent问题
您将“计算”按钮放在HBox中,然后在HBox上调用setMouseTransparent(true)
。这样的调用将禁用HBox 及其所有孩子的鼠标输入。链接的Javadoc中记录了此行为:
如果为true,则此节点(及其所有子节点)对鼠标事件完全透明。选择鼠标事件的目标时,将mouseTransparent设置为true的节点及其子树将不予考虑。
您不需要setMouseTransparent(true)
电话;只需删除它,“计算”按钮就可以按照您的预期进行点击。
重叠组件问题
您还需要确保不要将某些组件与其他组件重叠,否则只有顶级组件可以单击。在您的情况下,包含“清除”按钮的HBox与“计算”按钮重叠。
所以改变:
pane.add(hbox, 1, 4);
要:
pane.add(clearButton, 1, 4);
调试协助
您可以通过调用layout.setStyle(“ - fx-background-color:green;”)来调试布局,以显示布局的范围,或者使用ScenicView。