我想显示一条消息,显示我选择的内容(Java) [英] I want to display a message showing what I selected (Java)

查看:66
本文介绍了我想显示一条消息,显示我选择的内容(Java)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个拼贴画项目(比如一家餐馆),而且我不太擅长Java(他们并没有真正尝试教我们).该项目的原理是显示一条消息,说明我选择了哪些单选按钮和复选框.

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.

我有5个单选按钮,它们用于选择餐点,还有5个复选框,用于选择配菜.我设法使单选按钮起作用,但是我不知道如何使复选框起作用...

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...

这是单选按钮的代码(这适用于所有5个):

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...

推荐答案

由于您要显示零个或多个复选框的文本,因此需要检查每个复选框是否都被选中,并连接结果文本,而不是仅保留它们一种选择.此外,由于可以单独选中和取消选中复选框,所以最好仅在按下按钮时检查它们的状态,而不是一直尝试跟踪它们.例如:

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 :-)

一个更优雅的解决方案是使用Strings集合来收集配菜-同样,您可以尝试为此改进代码.

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.

顺便说一句,您不需要String.valueOf()即可打印String,因此我从上面的代码中将其删除. Java编码约定是以小写字母开头的变量/字段名称.

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.

这篇关于我想显示一条消息,显示我选择的内容(Java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆