如何使用开关为 JComboBox 中的所选项目赋值

发布于 2024-10-16 12:06:08 字数 209 浏览 2 评论 0原文

这是我的问题:

我们学期有两门 Java 课程。我们一直在学习 GUI 元素。在我们最新的作业中,我们必须注册与会者姓名。姓名将从文本框和组合框中选择,其中他们可以选择以商务人士 ($895)、学生 ($495) 或免费 ($0) 身份参加。

我的问题是:

我们被指示使用 switch 语句来确定注册费用。我该怎么做?

Here is my problem:

In our term there are two java courses. we have been learning about GUI elements. In our latest assignment, we have to register an attendees name. the name will be taken from text box, and a combo box in which they have option to attend as a business person ($895), student ($495), or complimentary ($0).

My question is this:

we are directed to use a switch statement to determine the the registration fee. How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

躲猫猫 2024-10-23 12:06:08

我假设您使用 JButton 及其 ActionListener 代码来“接受”输入。在 actionPerformed() 方法中,您检查 JComboBox 的所选索引(您可以检查所选项目,但这不适用于您的 switch 语句)并测试返回的 < code>int 在你的 switch 块中。您可以通过在 JComboBox 上调用 getSelectedIndex()(当然!)来获取选定的索引。我想您很清楚如何使用 switch 语句,对吗?

在伪代码中

begin actionPerformed method
  get selected index from combobox
  do switch on selected index
     case 0: set registration fee to first value, break
     case 1: set registration fee to second value, break
     case 2: set registration fee to third value, break
     default -- something's wrong. ;)
  end of switch
end of action performed

I assume that you use a JButton and its ActionListener code to "accept" the input. In the actionPerformed() method you check the JComboBox's selected index (you could check the selected item, but that won't work for your switch statement) and test the returned int in your switch block. You can get the selected index by calling getSelectedIndex() (but of course!) on the JComboBox. I assume that you're clear on how to use a switch statement, correct?

In pseudocode

begin actionPerformed method
  get selected index from combobox
  do switch on selected index
     case 0: set registration fee to first value, break
     case 1: set registration fee to second value, break
     case 2: set registration fee to third value, break
     default -- something's wrong. ;)
  end of switch
end of action performed
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文