gpt4 book ai didi

java - int数组无法调用出来

转载 作者:行者123 更新时间:2023-12-01 13:26:11 26 4
gpt4 key购买 nike

我对我的程序感到困惑。如果我单击某个列表,如何在串联中获取整数数组?这是我正在尝试的代码。

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;

public class JListDemo extends JFrame {
JList list;
String[] listColorNames = {"tres", "dos", "piso", "kwatro", "sais"};
int[] listColorValues = {3, 2, 1, 4, 6};
Container con;

public JListDemo() {
super("List Source Demo");
con = getContentPane();
con.setLayout(new FlowLayout());
list = new JList(listColorNames);
list.setSelectedIndex(0);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
con.add(new JScrollPane(list));
list.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {

con. (listColorValues[list.getSelectedIndex()]);
}
}
);
setSize(200, 200);
setVisible(true);
//setSizable(false);
}

public static void main(String[] args) {
JListDemo test = new JListDemo();
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

最佳答案

如果我明白你想做什么:

list.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {

JLabel price = new JLabel(listColorValues[list
.getSelectedIndex()] + "");

con.add(price);
con.revalidate();
}
});

但是,如果您希望文本显示在列表下方,那么 flowLayout 不适合您的需求。

关于java - int数组无法调用出来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21798819/

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