问题描述
我正在从书中举一个 GridPane 示例.这本书不是最新的.当我尝试使用Internet时,我会不断获得 Scene Builder ,这不是我要使用的 .我正在使用 NetBeans IDE 8.2,该程序有2个问题.在 btnOk_Click()中,我尝试选择小,中,大 和 薄,厚.该程序将它们放在同一组中.另外,此 MessageBox.show(msg,订单详细信息"); //??????可能已过时. btnCancel_Click()有效,但是 btnOk_Click()没有任何作用.这是程序:
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();
}
推荐答案
所以我可以在您的代码中看到您在此处放置了错误的组变量...
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);
如果您希望它们位于不同的组中,则应为groupCrust而不是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
}
});
在 MessageBox 方面,看起来这例如您可能要根据所使用的版本尝试发出警报.如果要在上面实现它,则应参考此代码 https://stackoverflow.com/a/36137669/11547889 您的代码
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
这篇关于我的消息框在此GridPane中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!