I want to display a message showing what I selected (Java)

只谈情不闲聊 提交于 2020-01-06 03:08:04

问题


I'm doing a project (something like a restaurant) for collage, and I'm not very good in Java (they didn't really try to teach us). The principle of the project is to show a message saying what radio buttons and check boxes I've selected.

I have 5 radio buttons, they are for selecting a meal, and 5 check boxes for selecting a side dish. I've managed to have the radio buttons work but I don't know how to make the check boxes work...

This is the code for the radio buttons (this goes for all 5):

 private void jRadioButton1ItemStateChanged(java.awt.event.ItemEvent evt) {
if (evt.getSource().equals(jRadioButton1)) { Meal= jRadioButton1.getText(); //Meal is a String }

I tried the same code for the check boxes but it only shows one side dish in the message, even though I selected multiple...

The code for the button that shows the message:

 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
JOptionPane.showMessageDialog(rootPane, "You have chosen:\n" + String.valueOf(Meal) + "\n" + String.valueOf(SideDish)); }

So basically, if anyone is willing to help, please tell me how to make the check boxes work... that every selected check box is shown in the message, like this:

You have chosen:
Pizza //meal
Ketchup //selected side dish #1
Chilli peppers //selected side dish #2
Feta cheese //selected side dish #3

I hope my question is clear...


回答1:


Since you want to display text for zero or more checkboxes, you need to check for each whether it is selected, and concatenate the resulting text, instead of keeping only one option. Moreover, since check boxes can be checked and unchecked independently, it may be better to check their state only at the moment when the button is pressed instead of attempting to keep track of them all the time. E.g.:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

  SideDish = "";
  if (jCheckBox1.getState()))
  {
    SideDish += jCheckBox1.getText();
  }
  ...
  if (jCheckBox5.getState()))
  {
    SideDish += ", " + jCheckBox5.getText();
  }

  JOptionPane.showMessageDialog(rootPane, "You have chosen:\n" + Meal + "\n" + SideDish);
}

This is just an illustration, which won't always display separating commas properly - I will leave the fix to you as an exercise :-)

A more elegant solution would be to use a collection of Strings to collect the side dishes - again, you may try improving the code towards this.

Btw, you don't need String.valueOf() to print Strings so I removed it from the code above. And the Java coding convention is to start variable/field names with lowercase.




回答2:


As you can select multiple checkboxes at the same time, you should collect the checked values in a collection. Set<String> comes to mind as you can select one side dish only once.

Also, you did not have this problem as each selection of the radio button overwrote the previous one, but you will need to remove items from the set if their corresponding checkbox is unticked.

Note: Set<> is an interface, you will need an implementing class to actually use it. In your case a HashSet<> would work (see documentation for the available method for Set and HashSet)

When you display the choice of side dishes, you can enumerate the elements in the set and eother print them one-by-one or collect the result in a string by concatenating the elements.

Note: you might not evenneed the set if you have direct access to the checkboxes: at the time of displaying which side dish is chosen, just check the state of each checkbox and accumulate the choices string as outlined above




回答3:


Here is an example

Lets say you have 4 sidedishes

Vector<JCheckBox> boxes = new Vector<JCheckBox>
boxes.add(checkbox1) .... .add(checkbox4);

and on clicking the button

Vector<String> sideDishes = new Vector<String>();
for(int i=0; i<boxes.size(); i++){
   if(boxes.elementAt(i).isSelected(){
      sideDishes.add(boxes.elementAt(i).getText();
   }
}



回答4:


I am thinking that first you should learn is how to analyze problem and how to use Google or other search engine sites, So try to read this docs and look to examples



来源:https://stackoverflow.com/questions/11104631/i-want-to-display-a-message-showing-what-i-selected-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!