JavaFX按钮不是';不可点击
我有这个按钮。可以用enter键按下它,也可以通过对其进行选项卡选择,但不能单击它,就光标而言,它似乎不存在。我不相信有什么东西能覆盖它,但大多数情况下是可以通过某种方式覆盖的,因为用相同代码创建的按钮是可点击的JavaFX按钮不是';不可点击,java,user-interface,javafx,Java,User Interface,Javafx,我有这个按钮。可以用enter键按下它,也可以通过对其进行选项卡选择,但不能单击它,就光标而言,它似乎不存在。我不相信有什么东西能覆盖它,但大多数情况下是可以通过某种方式覆盖的,因为用相同代码创建的按钮是可点击的 public class Main extends Application { @Override public void start(Stage primaryStage) throws Exception{ primaryStage.setTitle("Mortgage C
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();
}
public类主扩展应用程序{
@凌驾
public void start(Stage primaryStage)引发异常{
primaryStage.setTitle(“抵押计算器”);
GridPane=新建GridPane();
窗格设置对齐(位置中心);
窗格。setHgap(10);
窗格。设置间隙(10);
窗格。设置填充(新插图(25,25,25,25));
场景=新场景(窗格,350325);
Text sceneTitle=新文本(“抵押计算器”);
setFont(Font.Font(“Arial”,fontwweight.NORMAL,20));
添加(场景,0,0,2,1);
标签总额=新标签(“抵押贷款价值:”);
窗格。添加(总计,0,1);
最终文本字段totalField=新文本字段();
添加(totalField,1,1);
标签百分比=新标签(“%利息:”);
窗格。添加(百分比,0,2);
最终文本字段percentField=新文本字段();
窗格。添加(百分比字段,1,2);
标签时间=新标签(“抵押期限:”);
添加(时间,0,3);
final TextField timeField=新TextField();
窗格。添加(时间域,1,3);
最终文本amuntowed=新文本();
窗格。添加(未列出的,1,7);
最终文本付款=新文本();
窗格。添加(付款,1,8);
按钮计算按钮=新按钮(“计算”);
HBox hbox1=新的HBox(10);
hbox1.设置对齐(右下角位置);
hbox1.getChildren().add(calculateButton);
calculateButton.setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent事件){
double principal=double.parseDouble(totalField.getText());
双利息=(double.parseDouble(percentField.getText())/100)/12;
int time=Integer.parseInt(timeField.getText());
整数=时间*12;
双月付款=(本金*(利息*(数学功率((利息+1),金额))/(数学功率((1+利息),金额)-1);
//双倍总付款=;
//amountheed.setText(“欠款金额为:“+总付款”);
payment.setText(“月付款为:$”+(int)Math.ceil(monthlyPayment));
}
});
窗格。添加(hbox1,1,4);
按钮清除按钮=新按钮(“清除”);
HBox HBox=新的HBox(10);
hbox.设置对齐(位置左下);
hbox.getChildren().add(clearButton);
窗格。添加(hbox,1,4);
EventHandler handler1=新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent事件){
totalField.setText(“”);
percentField.setText(“”);
timeField.setText(“”);
付款方式:setText(“”);
}
};
clearButton.setOnAction(手柄1);
初级阶段。场景(场景);
primaryStage.show();
}
}捕鼠器问题
你把“计算”按钮放在HBox上,然后打开HBox。这样的调用将禁用HBox及其所有子项上的鼠标输入。此行为记录在链接的Javadoc中:
如果为true,则该节点(及其所有子节点)对鼠标事件完全透明。为鼠标事件选择目标时,将不考虑mouseTransparent设置为true的节点及其子树
您不需要setMouseTransparent(true)
调用;只需将其移除,“计算”按钮就会如您所期望的那样被点击
重叠组件问题
您还需要确保某些组件不会与其他组件重叠,否则只能单击顶部组件。在您的例子中,包含“清除”按钮的HBox与“计算”按钮重叠
因此,改变:
pane.add(hbox, 1, 4);
致:
调试协助
您可以通过调用layout.setStyle(“-fx background color:green;”)来显示布局的范围,或者通过使用来调试布局。MouseTransport
你把“计算”按钮放在HBox上,然后打开HBox。这样的调用将禁用HBox及其所有子项上的鼠标输入。此行为记录在链接的Javadoc中:
如果为true,则该节点(及其所有子节点)对鼠标事件完全透明。为鼠标事件选择目标时,将不考虑mouseTransparent设置为true的节点及其子树
您不需要setMouseTransparent(true)
调用;只需将其移除,“计算”按钮就会如您所期望的那样被点击
重叠组件问题
您还需要确保某些组件不会与其他组件重叠,否则只能单击顶部组件。在您的例子中,包含“清除”按钮的HBox与“计算”按钮重叠
因此,改变:
pane.add(hbox, 1, 4);
致:
调试协助
您可以通过调用layout.setStyle(“-fx background color:green;”)来显示布局的范围,或者通过使用来调试布局。我删除了该setMouseTransparent,但由于某些原因,该按钮仍然无法单击。clearButton工作正常,但calculate不工作。您的第二个HBox(包含clear按钮)也在重叠calculate按钮。删除该按钮并直接将“清除”按钮添加到网格后,一切正常。我附上了一个截图:OSX10.9,Java8u60。非常感谢兄弟,效果很好。非常感谢您提供的调试提示,这些都很棒。我删除了setMouseTransparent,但由于某些原因,该按钮仍然无法单击。clearButton工作正常,但calculate不工作。您的第二个HBox(包含clear按钮)也在重叠calculate按钮。删除该按钮并直接将“清除”按钮添加到网格后,一切正常。我附加