问题
I am taking a GridPane example from an book. The book is not the most recent. When I try to use the Internet I keep getting Scene Builder which is not what I want to use. I am using NetBeans IDE 8.2, and I have 2 problems with this program. Within the btnOk_Click() I am trying to choose Small, Medium, Large AND Thin, Thick. The program puts them in the same group. Also, this MessageBox.show(msg, "Order Details"); //????? may be out dated. The btnCancel_Click() works, but the btnOk_Click() does not show any work. Here is the program:
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();
}
回答1:
So I can see in your code that you put the wrong group variable here...
ToggleGroup groupCrust = new ToggleGroup();
rdoThin.setToggleGroup(groupSize);
rdoThick.setToggleGroup(groupSize);
It should be groupCrust instead of groupSize if you want them to be in different groups.
In terms of the event handlers for the buttons, I have never seen doing that in the way you shared...
I would add the click event handlers like this...
btnOk.addEventHandler(MouseEvent. MOUSE_CLICKED,
new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent e) {
// some code
}
});
In terms of the MessageBox, looking at this seems like you might want to try to Alert depending on the version you are using. You should refer to this code https://stackoverflow.com/a/36137669/11547889 if you want to implement it on your code
来源:https://stackoverflow.com/questions/59619393/my-messagebox-does-not-work-in-this-gridpane