JavaFX Button无法单击 [英] JavaFX Button isn't clickable

查看:795
本文介绍了JavaFX Button无法单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这个按钮。可以使用回车键按下它,可以通过Tab键选择它,但不能点击它,并且就光标而言似乎不存在。我不相信有什么东西可以覆盖它,但是大多数都是如此,因为使用相同代码创建的按钮是可点击的。

  public class Main extends Application {
@Override
public void start(Stage primaryStage)throws Exception {
primaryStage.setTitle(按揭计算器);

GridPane pane = new GridPane();
pane.setAlignment(Pos.CENTER);
pane.setHgap(10);
pane.setVgap(10);
pane.setPadding(new Insets(25,25,25,25));
场景场景=新场景(窗格,350,325);

文本sceneTitle =新文本(按揭计算器);
sceneTitle.setFont(Font.font(Arial,FontWeight.NORMAL,20));
pane.add(sceneTitle,0,0,2,1);
标签总数=新标签(抵押贷款的价值:);
pane.add(total,0,1);
final TextField totalField = new TextField();
pane.add(totalField,1,1);
标签百分比=新标签(%兴趣:);
pane.add(百分比,0,2);
final TextField percentField = new TextField();
pane.add(percentField,1,2);
标签时间=新标签(抵押期限:);
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);

按钮calculateButton = new按钮(计算);
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 =时间* 12;

double monthlyPayment =(校长*(兴趣*(Math.pow((兴趣+ 1),numPayment))))/(Math.pow((1 +兴趣), numPayment) - 1);
// double totalPayment =;
//amountOwed.setText(欠款是:+ totalPayment);
payment.setText(每月付款是:$ +(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();
}

}

解决方案

mouseTransparent问题



您将计算按钮放在HBox中,然后调用



重叠组件问题



您还需要确保不要将某些组件与其他组件重叠,否则只需重叠组件将是可点击的。在您的情况下,包含清除按钮的HBox与计算按钮重叠。



因此更改:

  pane.add(hbox,1,4); 

收件人:

  pane.add(clearButton,1,4); 

调试协助



您可以通过调用layout.setStyle( - fx-background-color:green;)来调试布局,以显示布局的范围,或者使用 ScenicView


So I have this button. It can be pressed with the enter key, and can be selected through tabbing to it, but it cannot be clicked on, and doesn't appear to exist as far as the cursor is concerned. I don't believe anything is covering it, but it most be covered some how, as a button created with identical code is clickable.

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();
}

}

解决方案

mouseTransparent Issue

You put the "Calculate" button in a HBox and then you call setMouseTransparent(true) on the HBox. Such a call will disable mouse input on the HBox and all it's children. This behavior is documented in the linked Javadoc:

If true, this node (together with all its children) is completely transparent to mouse events. When choosing target for mouse event, nodes with mouseTransparent set to true and their subtrees won't be taken into account.

You don't need the setMouseTransparent(true) call; just remove it, and the "Calculate" button will be clickable as you expect.

Overlapping Components Issue

You also need to ensure that you don't overlap some components with others otherwise only the top components will be clickable. In your case, the HBox containing the "Clear" button is overlapping the "Calculate" button.

So change:

pane.add(hbox, 1, 4);

To:

pane.add(clearButton, 1, 4);

Debugging Assistance

You can debug layouts either by calling layout.setStyle("-fx-background-color: green;") to show the extent of the layout, or by using ScenicView.

这篇关于JavaFX Button无法单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆