开发者

How to use a switch to assign value to a selected item from a JComboBox

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 t开发者_JAVA技巧o use a switch statement to determine the the registration fee. How can I do this?


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜