我试图让用户在 JavaFX ListView 上选择尽可能多的项目。然而我使用的方法似乎没有考虑到多种选择的可能性。
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.layout.HBox;
import javafx.geometry.Pos;
import javafx.geometry.Insets;
import javafx.scene.control.Label;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListView;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.Alert.AlertType;
/**
* Skateboard Designer
*
* @author Emanuel "Manny" Luna
*/
public class SkateboardDesigner_Luna extends Application
{
public static void main(String[] args)
{
// Launch the application.
launch(args);
}
@Override
public void start(Stage primaryStage)
{
// *********************************************
// Set up components.
// *********************************************
// Build the Decks ComboBox.
ComboBox<String> deckComboBox = new ComboBox<>();
deckComboBox.getItems().addAll("The Master Thrasher $60",
"The Dictator $45",
"The Street King $50");
// Label to prompt the user to select a deck
Label deckPromptLabel = new Label("Select a Deck");
VBox deckVBox = new VBox(10, deckPromptLabel, deckComboBox);
// Build the Truck Assemblies ComboBox.
ComboBox<String> truckComboBox = new ComboBox<>();
truckComboBox.getItems().addAll("7.75 inch axle $35",
"8 inch axle $40",
"8.5 inch axle $45");
// Label to prompt the user to select a truck assembly
Label truckPromptLabel = new Label("Select a Truck Assembly");
VBox truckVBox = new VBox(10, truckPromptLabel, truckComboBox);
// Build the Wheels ComboBox.
ComboBox<String> wheelComboBox = new ComboBox<>();
wheelComboBox.getItems().addAll("51mm $20",
"55mm $22",
"58mm $24",
"61mm $28");
// Label to prompt the user to select wheels
Label wheelPromptLabel = new Label("Select Wheels");
VBox wheelVBox = new VBox(10, wheelPromptLabel, wheelComboBox);
// Build the Misc ListView.
ListView<String> miscListView = new ListView<>();
miscListView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
miscListView.getItems().addAll("Grip Tape $10",
"Bearings $30",
"Riser Pads $2",
"Nuts & Bolts Kit $3");
// Label to prompt the user to select misc parts
Label miscPromptLabel = new Label("Select Misc Parts");
VBox miscVBox = new VBox(10, miscPromptLabel, miscListView);
HBox partsHBox = new HBox(10, deckVBox, truckVBox, wheelVBox, miscVBox);
// Create the output label for total cost.
Label costDescriptor = new Label("Cost:");
Label costOutputLabel = new Label("0.00");
HBox costHBox = new HBox(10, costDescriptor, costOutputLabel);
costHBox.setAlignment(Pos.CENTER);
// Create the Calculate button.
Button calcButton = new Button("Calculate Cost");
// Put everything into a VBox
VBox mainVBox = new VBox(10, partsHBox, costHBox, calcButton);
mainVBox.setAlignment(Pos.CENTER);
mainVBox.setPadding(new Insets(10));
// *********************************************
// Register the event handler for the button here.
// *********************************************
calcButton.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent arg0)
{
try {
double subTotal = 0;
double taxes = 0;
double total = 0;
// First, the function will check to see what the
// user has chosen in the combo box's.
String deck = deckComboBox.getSelectionModel().getSelectedItem();
String truck = truckComboBox.getSelectionModel().getSelectedItem();
String wheels = wheelComboBox.getSelectionModel().getSelectedItem();
String misc = miscListView.getSelectionModel().getSelectedItem();
// Second, the function checks what has been selected
if (deck.equals("The Master Thrasher $60"))
{
subTotal += 60;
}
else if (deck.equals("The Dictator $45"))
{
subTotal += 45;
}
else if (deck.equals("The Street King $50"))
{
subTotal += 50;
}
if (truck.equals("7.75 inch axle $35"))
{
subTotal += 35;
}
else if (truck.equals("8 inch axle $40"))
{
subTotal += 40;
}
else if (truck.equals("8.5 inch axle $45"))
{
subTotal += 45;
}
if (wheels.equals("51mm $20"))
{
subTotal += 20;
}
else if (wheels.equals("58mm $24"))
{
subTotal += 24;
}
else if (wheels.equals("61mm $28"))
{
subTotal += 28;
}
// Here's the mistake
if(misc.equals(""))
{
subTotal += 10;
}
if (misc.equals("Bearings $30"))
{
subTotal += 30;
}
if (misc.equals("Riser Pads $2"))
{
subTotal += 2;
}
if (misc.equals("Nuts & Bolts Kit $3"))
{
subTotal += 3;
}
taxes = subTotal * 0.07;
total = subTotal + taxes;
costDescriptor.setText(String.format("Sub Total:\nTax:\nTotal:"));
costOutputLabel.setText(String.format("$%.2f%n$%.2f%n$%.2f", subTotal, taxes, total));
}
catch(Exception e)
{
Alert a = new Alert(AlertType.ERROR, "Please pick one item from each category.");
a.setTitle("Error Message");
a.show();
}
}
});
// *********************************************
// Show the GUI!
// *********************************************
// Add the main VBox to a scene.
Scene scene = new Scene(mainVBox);
// Set the scene to the stage and display it.
primaryStage.setScene(scene);
primaryStage.show();
}
}
使用“misc”时,它仅采用用户按下的第一个选项。我怎样才能让它在ListView中添加多个项目以及知道用户检查了几个项目然后添加价格的方法?
尝试使用 GetSelectedItems (而不是 GetSelectedItem),因为您的代码将返回 MultipleSelectionModel
这是堆栈溢出的一个示例