简体   繁体   English

使用JComboBox从同一对象内的数据更改JLabel:

[英]Using JComboBox to change JLabels from data within same object:

I am trying to design a GUI, and I am really stuck on a certain issue, which involves using JComboBox to change data in a JLabel, but the catch is the data in the JLabel comes from object data the combo box needs. 我正在尝试设计一个GUI,我确实陷入了一个特定的问题,该问题涉及使用JComboBox更改JLabel中的数据,但要注意的是JLabel中的数据来自组合框所需的对象数据。

Example: hypothetical arraylist with objects in each locale: 示例:在每个语言环境中都有对象的假设数组列表:

ArrayList<Animals> FarmAnimals = new ArrayList<Animals>();

FarmAnimals.add(new Animals("Cows", "Black"));    
FarmAnimals.add(new Animals("Pigs",  "Pink");    
FarmAnimals.add(new Animals("Sheep",  "White"));

Now, I want to populate the JComboBox with "Cows", "Pigs", and "Sheep", which is easily done with making a string array of the values. 现在,我想用“ Cows”,“ Pigs”和“ Sheep”填充JComboBox,这很容易通过创建值的字符串数组来完成。 The problem is that, when I, hypothetically, select "Cows", I want a color JLabel to display "Black", "Pink", and "White" respectively when chosen: ideally from the object data within the array (just in case I have more data for Cows or whatever like paw size or loudness of animal noise that I'd like to input later). 问题是,假设我选择“ Cows”,则希望颜色JLabel在选择时分别显示“ Black”,“ Pink”和“ White”:理想情况下是从数组中的对象数据中获取(以防万一)我有更多关于“奶牛”的数据或类似的爪子大小或动物噪音的响度,我想稍后再输入)。

Is there any way to get an ItemListener to do this, or at least is there a tutorial that shows how to directly influence JLabel data from objects associated with what's in the JComboBox? 有没有办法让ItemListener做到这一点,或者至少有一个教程显示如何直接从与JComboBox中的内容相关联的对象中影响JLabel数据? Or am I just being stupid and there's a much better way of doing it? 还是我只是愚蠢,还有更好的方法呢? Thanks. 谢谢。

This might be a better example: 这可能是一个更好的示例:

http://pokemonshowdown.com/damagecalc/ http://pokemonshowdown.com/damagecalc/

Notice that when you change the EV's or the IV's, or if you change the pokemon, the stats on the side directly change with respect to the Pokemon chosen and the change in the EV & IV inputs. 请注意,当您更改EV或IV的值时,或者如果您更改了Pokemon的值,则相对于所选的Pokemon以及EV&IV输入的更改,侧面的统计信息将直接更改。 I want this behavior in the GUI I'm making (stats change directly based on inputs, which I assume are directly connected to object data stored for each Pokey.) but I can't seem to mimic it. 我想要我正在做的GUI中的这种行为(统计数据直接基于输入而改变,我认为这些输入直接与为每个Pokey存储的对象数据相关联。)但是我似乎无法模仿它。

One technique here is: 这里的一种技术是:

  • Store all your Animal data in an array, as you are currently doing. 像当前一样,将所有Animal数据存储在一个数组中。
  • Give the Animal class a method to receive the relevant object data, in whatever form most naturally represents it (for example, if all your animal-specific data is a list of strings like colors, a String[] or a List<String> would be a good way to hold this data). Animal类提供一种方法,以最自然地表示它的任何形式接收相关的对象数据(例如,如果所有特定于动物的数据都是像颜色这样的字符串列表,则String[]List<String>是保存此数据的好方法)。 Let's call this method getData() . 我们将此方法称为getData()
  • Override Animal.toString() to return the name of the animal that you want to display in the combo box. 重写Animal.toString()以返回要在组合框中显示的动物的名称。
  • Now, populate the combo box with a list of animals. 现在,用动物列表填充组合框。 Use JComboBox.addItem to add each Animal directly to the combo box. 使用JComboBox.addItem将每个Animal直接添加到组合框中。
  • Add an action listener to the combo box. 将动作侦听器添加到组合框。 In the action listener, you may use (Animal)comboBox.getSelectedItem() to retrieve the selected Animal directly. 在动作侦听器中,您可以使用(Animal)comboBox.getSelectedItem()直接检索选定的Animal
  • Now that you have the selected Animal , you can call theAnimal.getData() to get the data you want to put in the label. 现在,您已经选择了Animal ,可以调用theAnimal.getData()来获取要放入标签中的数据。 You can call any other methods of Animal here too. 您也可以在此处调用Animal任何其他方法。

Basically, the idea above is to take advantage of JComboBox 's ability to directly store and return your actual objects. 基本上,上面的想法是利用JComboBox的功能直接存储和返回实际对象。 Then you can get the relevant data directly from the selected Animal , and update other GUI elements (eg your label) with that data accordingly. 然后,您可以直接从选定的Animal获取相关数据,并使用该数据相应地更新其他GUI元素(例如,您的标签)。

A couple of things to note about the above: 关于上述注意事项:

  • If overriding Animal.toString() to provide a display string is not appropriate for your situation for some reason (eg, as MadProgrammer points out in the comments, toString() is typically used for debugging output rather than information meaningful to a user), you have other options. 如果由于某种原因而覆盖Animal.toString()以提供显示字符串不适合您的情况(例如,正如MadProgrammer在注释中指出的那样, toString()通常用于调试输出,而不是对用户有意义的信息),您还有其他选择。 You could, eg, add String s to the combo box and, if the combo box is not sorted, take advantage of the fact that the combo box indices are the same as the indices into your Animal array. 例如,您可以将String添加到组合框,并且,如果组合框未排序,则可以利用组合框索引与Animal数组中的索引相同的事实。 Then in your action listener, you get the selected index, and look up the animal that way. 然后,在动作侦听器中,获取选定的索引,然后以这种方式查找动物。 Another option is to define a custom ListCellRenderer , which is beyond the scope of this answer (but information is readily available on the internet). 另一个选择是定义一个自定义ListCellRenderer ,它不在此答案的范围内(但可以在Internet上轻松获得信息)。
  • You also may notice that you don't actually need the separate animal array with the above approach. 您可能还会注意到,使用上述方法实际上并不需要单独的动物数组。 You could just use the combo box itself as the "array". 可以只使用组合框本身作为“数组”。 This may be sufficient for your needs, but bear in mind that it does tie your logic very closely to the GUI, and could cause problems in more complicated applications, especially when eg writing unit tests, or when writing methods that expect the collection of animals (and you end up having to pass the JComboBox / ComboBoxModel itself, or copy the items out of it into a temporary array), etc. 这可能足以满足您的需求,但请记住,它确实将您的逻辑与GUI紧密联系在一起,并可能在更复杂的应用程序中引起问题,尤其是在编写单元测试或编写希望收集动物的方法时。 (您最终不得不传递JComboBox / ComboBoxModel本身,或将其中的项目复制到一个临时数组中),等等。

Sorry if that was a little vague, hopefully it gets you started down the right path. 抱歉,如果有点含糊,希望它可以帮助您正确地开始工作。

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

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