简体   繁体   English

Java Eclipse复选框

[英]Java Eclipse checkboxes

I have two extra checkboxes which are set at .50 each when the checkbox is selected it adds the .50 to the total, so far no problems. 我有两个额外的复选框,当选中该复选框时,每个复选框都设置为.50 ,这会将.50添加到总数中,到目前为止没有问题。

How can I make it so that the user can change the checkbox text to the topping they like? 我该如何做,以便用户可以将复选框文本更改为他们喜欢的顶部?

Here is my code: 这是我的代码:

chckbxAddTopp1 = new JCheckBox("Add Topping1");
chckbxAddTopp1.setBackground(new Color(204, 255, 255));
chckbxAddTopp1.addActionListener(listener);


chckbxAddTopp2 = new JCheckBox("Add Topping2");
chckbxAddTopp2.setBackground(new Color(204, 255, 255));
chckbxAddTopp2.addActionListener(listener);

Associate the JCheckBox with JTextField or better yet a JComboBox , which will limit what the use can select. JCheckBoxJTextField或更好的JComboBox关联,这将限制用户可以选择的内容。 Place these in an array... 将它们放在一个数组中...

JCheckBox[] toppingsSelection = new JCheckBox[2];
JComboBox[] toppingTypes = new JComboBox[2];

For easier access (ie, element 0 of each array is associated together). 为了更容易访问(即,每个数组的元素0关联在一起)。 Layout the checkbox and the associated field on the same line within the UI. 在用户界面的同一行上布置复选框和关联的字段。

You can the use a loop to check which toppings are selected and use the same index value to get the type of topping. 您可以使用循环来检查选择了哪些浇头,并使用相同的索引值来获取浇头的类型。

Another idea would be to use a JComboBox , a JButton and a JList . 另一个想法是使用JComboBoxJButtonJList

The user would select the type of topping they want and press the "Add" button and it would be added to the JList . 用户将选择所需的浇头类型,然后按“添加”按钮,它将被添加到JList You would simply need to check the number of elements in the JList to determine the extra charge 您只需要检查JList的元素JList即可确定额外费用

Have a look at How to Use Buttons, Check Boxes, and Radio Buttons and How to Use Lists and How to Use Combo Boxes for more details 有关更多详细信息,请参阅如何使用按钮,复选框和单选按钮以及如何使用列表组合框

A JCheckBox isn't the appropriate type of component to use for what you are trying to achieve. JCheckBox不是要尝试实现的适当类型的组件。 A JComboBox seems more appropriate. JComboBox似乎更合适。 You could store all of the possible toppings (or if you want make the combo box editable so the user can enter what they want) in the model and have a JButton that adds the topping to the total cost. 您可以在模型中存储所有可能的浇头(或者如果要使组合框可编辑,以便用户可以输入所需的内容),并可以使用JButton将浇头添加到总成本中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM