我的消息框在此GridPane中不起作用

问题描述 投票:0回答:1

我正在从书中举一个GridPane示例。这本书不是最新的。当我尝试使用Internet时,我会不断得到Scene Builder,这是我想要使用的not。我正在使用NetBeans IDE 8.2,此程序有2个问题。在btnOk_Click()中,我尝试选择Small,Medium,Large AND Thin,Thick。该程序将它们放在同一组中。此外,此MessageBox.show(msg,“订单详细信息”); // ?????可能已过时。 btnCancel_Click()起作用,但是btnOk_Click()不显示任何作用。这是程序:

package gridpane;

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
//import javafx.event.ActionEvent;
//import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
//import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;

public class GridPane_Pizza extends Application {

//TextField
TextField txtName;
TextField txtPhone;
TextField txtAddress;
//RadioButton
RadioButton rdoSmall;
RadioButton rdoMedium;
RadioButton rdoLarge;
//RadioButton
RadioButton rdoThin;
RadioButton rdoThick;
//CheckBox
CheckBox chkPepperoni;
CheckBox chkMushrooms;
CheckBox chkAnchovies;
//private Stage state;
Stage stage;

@Override
public void start(Stage primaryStage) {

    stage = primaryStage;

    //Create Label
    Label lblName = new Label("Enter the name:\t");
    txtName = new TextField();
    txtName.setMinWidth(100);
    txtName.setPrefWidth(200);
    txtName.setMaxWidth(300);
    txtName.setPromptText("Enter the name here:\t");

    //Create phone for label and text
    Label lblPhone = new Label("Enter the phone:\t");
    txtPhone = new TextField();
    txtPhone.setMinWidth(60);
    txtPhone.setPrefWidth(120);
    txtPhone.setMaxWidth(180);
    txtPhone.setPromptText("Enter the phone here:\t");

    //Create address for label and text
    Label lblAddress = new Label("Enter the Address:\t");
    txtAddress = new TextField();
    txtAddress.setMinWidth(100);
    txtAddress.setPrefWidth(200);
    txtAddress.setMaxWidth(300);
    txtAddress.setPromptText("Enter the address here:\t");

    //Create the size pane  ??
    Label lblSize = new Label("Size:\t");  //??  \t
    rdoSmall = new RadioButton("Small");
    rdoMedium = new RadioButton("Medium");
    rdoLarge = new RadioButton("Large");

    rdoMedium.setSelected(true);

    ToggleGroup groupSize = new ToggleGroup();
    rdoSmall.setToggleGroup(groupSize);
    rdoMedium.setToggleGroup(groupSize);
    rdoLarge.setToggleGroup(groupSize);

    VBox paneSize = new VBox(lblSize, rdoSmall, rdoMedium, rdoLarge);

    paneSize.setSpacing(10);

    //Create the crust pane
    Label lblCrust = new Label("Crust");
    rdoThin = new RadioButton("Thin");
    rdoThick = new RadioButton("Thick");

    rdoThin.setSelected(true);

    ToggleGroup groupCrust = new ToggleGroup();
    rdoThin.setToggleGroup(groupSize);
    rdoThick.setToggleGroup(groupSize);


    VBox paneCrust = new VBox(lblCrust, rdoThin, rdoThick);

    paneCrust.setSpacing(10);

    Label lblToppings = new Label("Labels");
    chkPepperoni = new CheckBox("Pepperoni");
    chkMushrooms = new CheckBox("Mushrooms");
    chkAnchovies = new CheckBox("Anchovies");


    VBox paneToppings =  new VBox(lblToppings, chkPepperoni, chkMushrooms, chkAnchovies);

    paneToppings.setSpacing(10);

    Button btnOk = new Button("OK!!");
    btnOk.setPrefWidth(80);
    btnOk.setOnAction(e -> btnOk_Click());  //change to public

    Button btnCancel = new Button("Cancel!!");
    btnCancel.setPrefWidth(80);
    btnCancel.setOnAction(e -> btnCancel_Click());  //change to public

    HBox paneButtons = new HBox(10, btnOk, btnCancel);


    GridPane grid = new GridPane();

    grid.setPadding(new Insets(10));
    grid.setHgap(10);
    grid.setVgap(10);
    grid.setMinWidth(500);
    grid.setPrefWidth(500);
    grid.setMaxWidth(800);


    grid.addRow(0, lblName, txtName);
    grid.addRow(1, lblPhone, txtPhone);
    grid.addRow(2, lblAddress, txtAddress);
    grid.addRow(3, paneSize, paneCrust, paneToppings);

    grid.add(paneButtons, 2, 15);

    GridPane.setHalignment(lblName, HPos.RIGHT);  //grid or GridPane
    //grid.setHalignment(lblPhone, HPos.RIGHT);
    GridPane.setHalignment(lblPhone, HPos.RIGHT);
    GridPane.setHalignment(lblAddress, HPos.RIGHT);

    //grid.setColumnSpan(txtName, 2);
    GridPane.setColumnSpan(txtName, 2);  //grid or GridPane
    GridPane.setColumnSpan(txtPhone, 2);
    GridPane.setColumnSpan(txtAddress, 2);

    ColumnConstraints col1 = new ColumnConstraints();  //??
    col1.setPercentWidth(33);
    ColumnConstraints col2 = new ColumnConstraints();
    col2.setPercentWidth(33);
    ColumnConstraints col3 = new ColumnConstraints();
    col3.setPercentWidth(33);
    grid.getColumnConstraints().addAll(col1, col2, col3);  //??



    //
    Scene scene = new Scene(grid);
    primaryStage.setTitle("Pizza Order");
    primaryStage.setScene(scene);
    primaryStage.setMinWidth(500);
    primaryStage.setMaxWidth(1200); //900
    //primaryStage.setMaxWidth(900); //900
    primaryStage.setMaxHeight(800);
    primaryStage.show();
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
}

//private void btnOk_Click() {  //public
public void btnOk_Click() {  //public    
    //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    String msg = "Customer:\n\n";

    msg += "\t" + txtName.getText() + "\n";
    msg += "\t" + txtPhone.getText() + "\n\n";
    msg += "\t" + txtAddress.getText() + "\n";
    msg += "You have ordered a ";

    if (rdoSmall.isSelected())   //Problem here
        msg += "small ";
    if (rdoMedium.isSelected())
        msg += "medium ";
    if (rdoLarge.isSelected())
        msg += "large ";
    //problem here
    if (rdoThin.isSelected())
        msg += "thin crust pizza with ";
    if (rdoThick.isSelected())
        msg += "thick crust pizza with ";

    String toppings = "";

    toppings = buildToppings(chkPepperoni, toppings);
    toppings = buildToppings(chkMushrooms, toppings);
    toppings = buildToppings(chkAnchovies, toppings);

    if (toppings.equals(""))
        msg += "no toppings.";
    else
        msg += "the following toppings:\n" + toppings;

    MessageBox.show(msg, "Order Details");  //?????
    //msg.show(msg, "Order Details");
    //msg
    //stage.show(msg, "Order Details");  //??????????
}

public String buildToppings(CheckBox chk, String msg)
{
    if (chk.isSelected())
    {
        if (!msg.equals(""))
        {
            msg += ", ";
        }
        msg += chk.getText();
    }
    return msg;
}

public void btnCancel_Click() {

    //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    stage.close();
}
javafx message messagebox gridpane
1个回答
0
投票

所以我可以在您的代码中看到您在此处放置了错误的组变量...

ToggleGroup groupCrust = new ToggleGroup();
rdoThin.setToggleGroup(groupSize);
rdoThick.setToggleGroup(groupSize);

如果希望它们位于不同的组中,则应为groupCrust而不是groupSize。

就按钮的事件处理程序而言,我从未见过以您共享的方式执行此操作...

我会像这样添加点击事件处理程序...

btnOk.addEventHandler(MouseEvent. MOUSE_CLICKED, 
  new EventHandler<MouseEvent>() {
      @Override public void handle(MouseEvent e) {
          // some code
      }
});
© www.soinside.com 2019 - 2024. All rights reserved.