如何使用开关为 JComboBox 中的所选项目赋值
这是我的问题:
我们学期有两门 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设您使用
JButton
及其ActionListener
代码来“接受”输入。在actionPerformed()
方法中,您检查 JComboBox 的所选索引(您可以检查所选项目,但这不适用于您的 switch 语句)并测试返回的 < code>int 在你的 switch 块中。您可以通过在JComboBox
上调用getSelectedIndex()
(当然!)来获取选定的索引。我想您很清楚如何使用 switch 语句,对吗?在伪代码中
I assume that you use a
JButton
and itsActionListener
code to "accept" the input. In theactionPerformed()
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 returnedint
in your switch block. You can get the selected index by callinggetSelectedIndex()
(but of course!) on theJComboBox
. I assume that you're clear on how to use a switch statement, correct?In pseudocode