gpt4 book ai didi

java - JComboBox 数组的事件并在 JLabel 数组中显示信息

转载 作者:行者123 更新时间:2023-12-02 06:29:02 26 4
gpt4 key购买 nike

我有像这样的 JComboBox 数组和 JLabel 数组 当我选择每个 JComboBo 中的值时,我将在这里做什么以使 JLabel 返回相应的值例如,comboBox[0].setSelectItem(4); label[0] 将得到文本为 4当comboBox[4].setSelectedItem(2); label[4] 将得到文本为 2

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import javax.swing.JLabel;

public class Example extends javax.swing.JFrame implements ItemListener {
JComboBox[] comboBox = new JComboBox[5];
JLabel[] label = new JLabel[5];

public void test() {
for (int i = 0; i < label.length; i++) {
comboBox[i] = new JComboBox();
label[i] = new JLabel();
add(comboBox[i]);
add(label[i]);
for (int j = 0; j <= 10; j++) {
comboBox[i].addItem(j);
}
comboBox[i].addItemListener(this);

}
}

@Override
public void itemStateChanged(ItemEvent e) {
// What i will do here to make the JLabel return corresponding when I
// select the values in each JComboBox

/*
* Example when comboBox[0].setSelectItem(4); the label[0] will get the
* text is 4 when the comboBox[4].setSelectedItem(2); the lable[4] will
* get the text is 2
*/
}
}

请任何人帮助我:(

最佳答案

JComboBox 有方法

public int getSelectedIndex()

用它来获取索引,将索引转换为字符串并设置到JLabel

关于java - JComboBox 数组的事件并在 JLabel 数组中显示信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20235379/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com