警报确认,javaFx
我在做这个任务,我做了一个组合框,里面装满了百吉饼,另一个装满了奶油奶酪,为了添加或删除项目,必须选择一个单选按钮。除了这一部分,我几乎什么都要做。如果我单击“是”,它将删除,如果我单击“否”,它将不会删除项目。当我单击“否”时,它会起作用。它不会删除,并且一切正常。但是,当我单击“是”“删除”时,警报会一直保持,直到我再次按“否”为止。 我正在使用JavaFx14警报确认,javaFx,java,javafx,Java,Javafx,我在做这个任务,我做了一个组合框,里面装满了百吉饼,另一个装满了奶油奶酪,为了添加或删除项目,必须选择一个单选按钮。除了这一部分,我几乎什么都要做。如果我单击“是”,它将删除,如果我单击“否”,它将不会删除项目。当我单击“否”时,它会起作用。它不会删除,并且一切正常。但是,当我单击“是”“删除”时,警报会一直保持,直到我再次按“否”为止。 我正在使用JavaFx14 public int deleteItem() { int indexInteger = 0;
public int deleteItem()
{
int indexInteger = 0;
String deleteString;
if(bagelRadioButton.isSelected()) {
indexInteger = bagelComboBox.getSelectionModel().getSelectedIndex();
deleteString = (String) bagelComboBox.getItems().get(indexInteger);
if(indexInteger != -1 && indexInteger != 0)
{
alertMethod();
AlertDialog("","","");
bagelComboBox.getSelectionModel().select(0);
}
}
if(creamRadioButton.isSelected()) {
indexInteger = creamComboBox.getSelectionModel().getSelectedIndex();
deleteString = (String) creamComboBox.getItems().get(indexInteger);
if(indexInteger != -1 && indexInteger != 0)
{
alertMethod();
AlertDialog("","","");
creamComboBox.getSelectionModel().select(0);
}
}
return indexInteger;
}
public void alertMethod() {
String outPutListString;
boolean confirmBoolean = false;
outPutListString = "Do you wish to delete this Item?";
confirmBoolean = AlertConformation("", "Yes or No", outPutListString);
if(confirmBoolean) {
if(bagelRadioButton.isSelected())
{
bagelComboBox.getItems().remove(deleteItem());
}
else
{
creamComboBox.getItems().remove(deleteItem());
}
}
else {
addDialog("","","Nothing Was Deleted");
bagelComboBox.getSelectionModel().select(0);
creamComboBox.getSelectionModel().select(0);
}
}
public boolean AlertConformation(String titleString, String headerString, String messageString)
{
boolean confirmBoolean = false;
Alert alert = new Alert(AlertType.CONFIRMATION);
alert.setTitle(titleString);
alert.setHeaderText(headerString);
alert.setContentText(messageString);
ButtonType yesButton = new ButtonType("Yes", ButtonBar.ButtonData.YES);
ButtonType noButton = new ButtonType("No", ButtonBar.ButtonData.NO);
alert.getButtonTypes().setAll(yesButton, noButton);
Optional <ButtonType> resultButtonType = alert.showAndWait();
if(resultButtonType.get() == yesButton) {
confirmBoolean = true;
}
return confirmBoolean;
}
public int deleteItem()
{
int indexInteger=0;
字符串删除字符串;
if(bagelRadioButton.isSelected()){
indexInteger=bagelComboBox.getSelectionModel().getSelectedIndex();
deleteString=(String)bagelComboBox.getItems().get(indexInteger);
if(indexInteger!=-1&&indexInteger!=0)
{
alertMethod();
AlertDialog(“,”,“);
bagelComboBox.getSelectionModel().select(0);
}
}
if(creamRadioButton.isSelected()){
indexInteger=creamComboBox.getSelectionModel().getSelectedIndex();
deleteString=(String)creamComboBox.getItems().get(indexInteger);
if(indexInteger!=-1&&indexInteger!=0)
{
alertMethod();
AlertDialog(“,”,“);
creamComboBox.getSelectionModel().select(0);
}
}
回归指数积分;
}
公共方法(){
字符串outPutListString;
布尔confirmBoolean=false;
outPutListString=“是否要删除此项目?”;
confirmBoolean=AlertConformation(“,“是或否”,outPutListString);
if(confirmBoolean){
if(bagelRadioButton.isSelected())
{
bagelComboBox.getItems().remove(deleteItem());
}
其他的
{
creamComboBox.getItems().remove(deleteItem());
}
}
否则{
addDialog(“,”,“未删除任何内容”);
bagelComboBox.getSelectionModel().select(0);
creamComboBox.getSelectionModel().select(0);
}
}
公共布尔警报构造(字符串标题字符串、字符串标题字符串、字符串消息字符串)
{
布尔confirmBoolean=false;
警报警报=新警报(警报类型.确认);
警报。设置标题(标题串);
alert.setHeaderText(headerString);
alert.setContentText(messageString);
ButtonType yesButton=新的ButtonType(“是”,ButtonBar.ButtonData.Yes);
ButtonType noButton=新的ButtonType(“否”,ButtonBar.ButtonData.No);
alert.getButtonTypes().setAll(yesButton,noButton);
可选的resultButtonType=alert.showAndWait();
if(resultButtonType.get()==yesButton){
confirmBoolean=true;
}
返回confirmoolean;
}
这就是全部代码
import java.util.Optional;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBar;
import javafx.scene.control.ButtonType;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Stage;
/**
*
* @
*/
public class BagelStop extends Application implements EventHandler<ActionEvent>
{
// create the GridPaneObject
GridPane mainGridPane = new GridPane();
Label companyNameLabel = new Label("Bagel Stop");
TextField addTextField = new TextField();
Button addButton = new Button("Add");
Button deleteButton = new Button("Del.");
Button displayButton = new Button("Display");
ToggleGroup foodToggleGroup = new ToggleGroup();
//Bagel observablelist and comboBox
ObservableList<String> bagelTypeList = FXCollections.observableArrayList("Bagels", "Plain" , "Egg" , "Rye", "Salt" , "Blueberry" , "Garlic", "Onion", "Sesame", "Poppy Seed");
ComboBox<String> bagelComboBox = new ComboBox<String>(bagelTypeList);
RadioButton bagelRadioButton = new RadioButton();
ObservableList<String> creamCheeseList = FXCollections.observableArrayList("CreamCheese", "Plain" , "Herb","Garlic");
ComboBox<String> creamComboBox = new ComboBox<String>(creamCheeseList);
RadioButton creamRadioButton = new RadioButton();
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
public BagelStop(){
// Call these methods
designClass();
addAction();
}
public void designClass()
{
//Scene design
mainGridPane.setHgap(10);
mainGridPane.setVgap(10);
mainGridPane.setPadding(new Insets(10, 10, 10, 10));
companyNameLabel.setFont(Font.font(" Monaco", 30));
companyNameLabel.setTextFill(Color.DARKOLIVEGREEN);
bagelComboBox.getSelectionModel().select(0);
creamComboBox.getSelectionModel().select(0);
bagelRadioButton.setToggleGroup(foodToggleGroup);
creamRadioButton.setToggleGroup(foodToggleGroup);
mainGridPane.add(companyNameLabel, 2, 0);
mainGridPane.add(bagelComboBox, 1, 1);
mainGridPane.add(bagelRadioButton, 0, 1);
mainGridPane.add(creamComboBox, 3, 1);
mainGridPane.add(creamRadioButton, 4, 1);
mainGridPane.add(addTextField, 2, 2);
mainGridPane.add(addButton, 2, 4);
mainGridPane.add(deleteButton, 2, 5);
mainGridPane.add(displayButton, 2, 6);
}
public void addAction() {
addButton.setOnAction(this);
deleteButton.setOnAction(this);
}
@Override
public void handle(ActionEvent event)
{
Object sourceObject = event.getSource();
if(sourceObject == addButton)
{
addItem();
}
else if(sourceObject== deleteButton)
{
deleteItem();
}
else {
display();
}
}
public void display() {
}
////
public int deleteItem()
{
int indexInteger = 0;
String deleteString;
if(bagelRadioButton.isSelected()) {
indexInteger = bagelComboBox.getSelectionModel().getSelectedIndex();
deleteString = (String) bagelComboBox.getItems().get(indexInteger);
if(indexInteger != -1 && indexInteger != 0)
{
alertMethod();
AlertDialog("","","");
bagelComboBox.getSelectionModel().select(0);
}
}
if(creamRadioButton.isSelected()) {
indexInteger = creamComboBox.getSelectionModel().getSelectedIndex();
deleteString = (String) creamComboBox.getItems().get(indexInteger);
if(indexInteger != -1 && indexInteger != 0)
{
alertMethod();
AlertDialog("","","");
creamComboBox.getSelectionModel().select(0);
}
}
return indexInteger;
}
public void alertMethod() {
String outPutListString;
boolean confirmBoolean = false;
outPutListString = "Do you wish to delete this Item?";
confirmBoolean = AlertConformation("", "Yes or No", outPutListString);
if(confirmBoolean) {
if(bagelRadioButton.isSelected())
{
bagelComboBox.getItems().remove(deleteItem());
}
else
{
creamComboBox.getItems().remove(deleteItem());
}
}
else {
addDialog("","","Nothing Was Deleted");
bagelComboBox.getSelectionModel().select(0);
creamComboBox.getSelectionModel().select(0);
}
}
////
public void addItem() {
String foodString = addTextField.getText();
boolean foundBoolean = false;
int indexInteger = 0;
String checkString;
if(bagelRadioButton.isSelected()) {
while(!foundBoolean && indexInteger < bagelComboBox.getItems().size())
{
checkString = (String)bagelComboBox.getItems().get(indexInteger);
if(foodString.equalsIgnoreCase(checkString)){
foundBoolean = true;
}
else
{
indexInteger++;
}
}
if (foundBoolean)
{
AlertDialog(foodString,"Food","This food item is already in the list");
addTextField.setText("");
}
else
{
bagelTypeList.addAll(foodString);
addDialog(foodString,"","This food item is added to the list");
addTextField.setText("");
}
}
if(creamRadioButton.isSelected()) {
while(!foundBoolean && indexInteger < creamComboBox.getItems().size()) {
checkString =(String)creamComboBox.getItems().get(indexInteger);
if(foodString.equalsIgnoreCase(checkString))
{
foundBoolean = true;
}
else
{
indexInteger++;
}
}
if (foundBoolean)
{
AlertDialog(foodString,"Food","This food item is already in the list");
addTextField.setText("");
}
else
{
creamCheeseList.addAll(foodString);
addDialog(foodString,"","This food item is added to the list");
addTextField.setText("");
}
}
}
public boolean AlertConformation(String titleString, String headerString, String messageString)
{
boolean confirmBoolean = false;
Alert alert = new Alert(AlertType.CONFIRMATION);
alert.setTitle(titleString);
alert.setHeaderText(headerString);
alert.setContentText(messageString);
ButtonType yesButton = new ButtonType("Yes", ButtonBar.ButtonData.YES);
ButtonType noButton = new ButtonType("No", ButtonBar.ButtonData.NO);
alert.getButtonTypes().setAll(yesButton, noButton);
Optional <ButtonType> resultButtonType = alert.showAndWait();
if(resultButtonType.get() == yesButton) {
confirmBoolean = true;
}
return confirmBoolean;
}
public void AlertDialog(String titleString, String headerString, String messageString)
{
Alert alert = new Alert(AlertType.ERROR);
alert.setTitle(titleString);
alert.setHeaderText(headerString);
alert.setContentText(messageString);
alert.showAndWait();
}
public void addDialog(String titleString, String headerString, String messageString)
{
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle(titleString);
alert.setHeaderText(headerString);
alert.setContentText(messageString);
alert.showAndWait();
}
@Override
public void start(Stage primaryStage) throws Exception
{
primaryStage.setTitle("Bagel Stop");
Scene scene = new Scene(mainGridPane, 500,700);
primaryStage.setScene(scene);
primaryStage.show();
}
}
import java.util.Optional;
导入javafx.application.application;
导入javafx.collections.FXCollections;
导入javafx.collections.ObservableList;
导入javafx.event.ActionEvent;
导入javafx.event.EventHandler;
导入javafx.geometry.Insets;
导入javafx.scene.scene;
导入javafx.scene.control.Alert;
导入javafx.scene.control.Button;
导入javafx.scene.control.ButtonBar;
导入javafx.scene.control.ButtonType;
导入javafx.scene.control.ComboBox;
导入javafx.scene.control.Label;
导入javafx.scene.control.RadioButton;
导入javafx.scene.control.TextField;
导入javafx.scene.control.ToggleGroup;
导入javafx.scene.control.Alert.AlertType;
导入javafx.scene.layout.GridPane;
导入javafx.scene.paint.Color;
导入javafx.scene.text.Font;
导入javafx.stage.stage;
/**
*
* @
*/
公共类BagelStop扩展应用程序实现EventHandler
{
//创建GridPaneObject
GridPane mainGridPane=新建GridPane();
标签公司名称标签=新标签(“百吉饼站”);
TextField addTextField=新建TextField();
按钮添加按钮=新按钮(“添加”);
按钮删除按钮=新按钮(“删除”);
按钮显示按钮=新按钮(“显示”);
ToggleGroup foodToggleGroup=新的ToggleGroup();
//面包圈和组合盒
ObservableList bagelTypeList=FXCollections.observableArrayList(“百吉饼”、“原味”、“鸡蛋”、“黑麦”、“盐”、“蓝莓”、“大蒜”、“洋葱”、“芝麻”、“罂粟籽”);
ComboBox bagelComboBox=新的ComboBox(bagelTypeList);
RadioButton bagelRadioButton=新RadioButton();
ObservableList creamCheeseList=FXCollections.observableArrayList(“奶油干酪”、“普通”、“香草”、“大蒜”);
ComboBox奶油ComboBox=新的ComboBox(奶油干酪列表);
RadioButton creamRadioButton=新RadioButton();
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args)
{
发射(args);
}
公共面包架(){
//调用这些方法
设计类();
addAction();
}
公共设计类()
{
//场景设计
主网格窗格。setHgap(10);
mainGridPane.setVgap(10);
设置填充(新的插图(10,10,10,10));
companynamelab.setFont(Font.Font(“摩纳哥”,30));
companyNameLabel.setTextFill(颜色为深绿色);
bagelComboBox.getSelectionModel().select(0);
creamComboBox.getSelectionModel().select(0);
bagelRadioButton.setToggleGroup(foodToggleGroup);
creamRadioButton.setToggleGroup(foodToggleGroup);
添加(companyNameLabel,2,0);
添加(bagelComboBox,1,1);
添加(bagelRadioButton,0,1);
添加(creamComboBox,3,1);
主网格窗格。添加(按钮,4,1);
添加(addTextField,2,2);
主网格窗格
import java.util.Optional;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBar;
import javafx.scene.control.ButtonType;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Stage;
/**
*
* @author eduardo_gamboacruz
*/
public class BagelStop extends Application implements EventHandler<ActionEvent>
{
// create the GridPaneObject
GridPane mainGridPane = new GridPane();
Label companyNameLabel = new Label("Bagel Stop");
TextField addTextField = new TextField();
Button addButton = new Button("Add");
Button deleteButton = new Button("Del.");
Button displayButton = new Button("Display");
ToggleGroup foodToggleGroup = new ToggleGroup();
//Bagel observablelist and comboBox
ObservableList<String> bagelTypeList = FXCollections.observableArrayList("Bagels", "Plain" , "Egg" , "Rye", "Salt" , "Blueberry" , "Garlic", "Onion", "Sesame", "Poppy Seed");
ComboBox<String> bagelComboBox = new ComboBox<String>(bagelTypeList);
RadioButton bagelRadioButton = new RadioButton();
ObservableList<String> creamCheeseList = FXCollections.observableArrayList("CreamCheese", "Plain" , "Herb","Garlic");
ComboBox<String> creamComboBox = new ComboBox<String>(creamCheeseList);
RadioButton creamRadioButton = new RadioButton();
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
public BagelStop(){
// Call these methods
designClass();
addAction();
}
public void designClass()
{
//Scene design
mainGridPane.setHgap(10);
mainGridPane.setVgap(10);
mainGridPane.setPadding(new Insets(10, 10, 10, 10));
companyNameLabel.setFont(Font.font(" Monaco", 30));
companyNameLabel.setTextFill(Color.DARKOLIVEGREEN);
bagelComboBox.getSelectionModel().select(0);
creamComboBox.getSelectionModel().select(0);
bagelRadioButton.setToggleGroup(foodToggleGroup);
creamRadioButton.setToggleGroup(foodToggleGroup);
mainGridPane.add(companyNameLabel, 2, 0);
mainGridPane.add(bagelComboBox, 1, 1);
mainGridPane.add(bagelRadioButton, 0, 1);
mainGridPane.add(creamComboBox, 3, 1);
mainGridPane.add(creamRadioButton, 4, 1);
mainGridPane.add(addTextField, 2, 2);
mainGridPane.add(addButton, 2, 4);
mainGridPane.add(deleteButton, 2, 5);
mainGridPane.add(displayButton, 2, 6);
}
public void addAction() {
addButton.setOnAction(this);
deleteButton.setOnAction(this);
}
@Override
public void handle(ActionEvent event)
{
Object sourceObject = event.getSource();
if(sourceObject == addButton)
{
addItem();
}
else if(sourceObject== deleteButton)
{
deleteItem();
}
else {
display();
}
}
public void display() {
}
////
public void deleteItem()
{
int indexInteger = 0;
String deleteString = null;
String outPutListString;
boolean confirmBoolean = false;
outPutListString = "Do you wish to delete this Item?";
confirmBoolean = AlertConformation("", outPutListString , deleteString);
if(bagelRadioButton.isSelected()) {
indexInteger = bagelComboBox.getSelectionModel().getSelectedIndex();
deleteString = (String) bagelComboBox.getItems().get(indexInteger);
if(indexInteger != -1 && indexInteger != 0){
if(confirmBoolean) {
bagelComboBox.getItems().remove(indexInteger);
}
else {
addDialog("","","Nothing Was Deleted");
bagelComboBox.getSelectionModel().select(0);
}
}
}
if(creamRadioButton.isSelected()) {
indexInteger = creamComboBox.getSelectionModel().getSelectedIndex();
deleteString = (String) creamComboBox.getItems().get(indexInteger);
if(indexInteger != -1 && indexInteger != 0){
if(confirmBoolean) {
creamComboBox.getItems().remove(indexInteger);
}
else {
addDialog("","","Nothing Was Deleted");
creamComboBox.getSelectionModel().select(0);
}
}
}
}
////
public void addItem() {
String foodString = addTextField.getText();
boolean foundBoolean = false;
int indexInteger = 0;
String checkString;
if(bagelRadioButton.isSelected()) {
while(!foundBoolean && indexInteger < bagelComboBox.getItems().size())
{
checkString = (String)bagelComboBox.getItems().get(indexInteger);
if(foodString.equalsIgnoreCase(checkString)){
foundBoolean = true;
}
else
{
indexInteger++;
}
}
if (foundBoolean)
{
AlertDialog(foodString,"Food","This food item is already in the list");
addTextField.setText("");
}
else
{
bagelTypeList.addAll(foodString);
addDialog(foodString,"","This food item is added to the list");
addTextField.setText("");
}
}
if(creamRadioButton.isSelected()) {
while(!foundBoolean && indexInteger < creamComboBox.getItems().size()) {
checkString =(String)creamComboBox.getItems().get(indexInteger);
if(foodString.equalsIgnoreCase(checkString))
{
foundBoolean = true;
}
else
{
indexInteger++;
}
}
if (foundBoolean)
{
AlertDialog(foodString,"Food","This food item is already in the list");
addTextField.setText("");
}
else
{
creamCheeseList.addAll(foodString);
addDialog(foodString,"","This food item is added to the list");
addTextField.setText("");
}
}
}
public boolean AlertConformation(String titleString, String headerString, String messageString)
{
boolean confirmBoolean = false;
Alert alert = new Alert(AlertType.CONFIRMATION);
alert.setTitle(titleString);
alert.setHeaderText(headerString);
alert.setContentText(messageString);
ButtonType yesButton = new ButtonType("Yes", ButtonBar.ButtonData.YES);
ButtonType noButton = new ButtonType("No", ButtonBar.ButtonData.NO);
alert.getButtonTypes().setAll(yesButton, noButton);
Optional <ButtonType> resultButtonType = alert.showAndWait();
if(resultButtonType.get() == yesButton) {
confirmBoolean = true;
}
return confirmBoolean;
}
public void AlertDialog(String titleString, String headerString, String messageString)
{
Alert alert = new Alert(AlertType.ERROR);
alert.setTitle(titleString);
alert.setHeaderText(headerString);
alert.setContentText(messageString);
alert.showAndWait();
}
public void addDialog(String titleString, String headerString, String messageString)
{
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle(titleString);
alert.setHeaderText(headerString);
alert.setContentText(messageString);
alert.showAndWait();
}
@Override
public void start(Stage primaryStage) throws Exception
{
primaryStage.setTitle("Bagel Stop");
Scene scene = new Scene(mainGridPane, 500,700);
primaryStage.setScene(scene);
primaryStage.show();
}
}