gpt4 book ai didi

Java - JList , getSelectedValues 方法的困难

转载 作者:行者123 更新时间:2023-11-30 04:05:58 24 4
gpt4 key购买 nike

有人可以解释一下这段代码有什么问题吗?我正在尝试将数据从 JList 复制到另一个 JList。我想知道,如何以及为什么......

public class Ex3 extends JFrame{
private JList lista;
private JList listaCopia;
private static final String[] cores ={"Azul","Verde","Amarelo"};
private static final Color[] cor ={Color.blue,Color.green,Color.yellow};
private JButton copiar;

public Ex3(){
super("Utilizando JList");
copiar = new JButton(" Copiar >>>> ");
lista = new JList<String>(cores);
setLayout(new FlowLayout());
lista.setVisibleRowCount(3); // definida linhas visíveis
lista.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); // método de seleção única de opções
add(lista);
add(copiar);
copiar.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
listaCopia.setListData(lista.getSelectedValuesList().toArray());
}
});
lista.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
getContentPane().setBackground(cor[lista.getSelectedIndex()]);

}
});
listaCopia = new JList<String>();
listaCopia.setVisibleRowCount(3);
listaCopia.setFixedCellWidth(100);
listaCopia.setFixedCellHeight(15);
add(new JScrollPane(listaCopia));
}
}

我找到了这里的路Java - Deprecated method - What to do?但不解释...我仅使用 getSelectedValuesList() 编译了另一个代码,但不起作用...为什么?

最佳答案

似乎对我有用。当我移动/选择所有项目并单击 copiar按钮,左侧的所有项目都会复制到右侧的项目。这不是你想要的吗?

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class Ex3 extends JFrame {

private JList lista;
private JList listaCopia;
private static final String[] cores = {"Azul", "Verde", "Amarelo"};
private static final Color[] cor = {Color.blue, Color.green, Color.yellow};
private JButton copiar;
DefaultListModel model;

public Ex3() {
super("Utilizando JList");
copiar = new JButton(" Copiar >>>> ");

lista = new JList(cores);
setLayout(new FlowLayout());
lista.setVisibleRowCount(3); // definida linhas visíveis
lista.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); // método de seleção única de opções
add(lista);
add(copiar);
copiar.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
listaCopia.setListData(lista.getSelectedValuesList().toArray());
}
});
lista.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
getContentPane().setBackground(cor[lista.getSelectedIndex()]);

}
});
listaCopia = new JList<String>();
listaCopia.setVisibleRowCount(3);
listaCopia.setFixedCellWidth(100);
listaCopia.setFixedCellHeight(15);
add(new JScrollPane(listaCopia));
}

private static void createAndShowGui() {
Ex3 frame = new Ex3();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);

}


public Dimension getPreferredSize() {
return new Dimension(300, 300);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

如果您只想一次添加一个元素。您应该使用 ListModel。这是我为此所做的更改

public class Ex3 extends JFrame {

private JList lista;
private JList listaCopia;
private static final String[] cores = {"Azul", "Verde", "Amarelo"};
private static final Color[] cor = {Color.blue, Color.green, Color.yellow};
private JButton copiar;
DefaultListModel model; <-- DefaultListModel

public Ex3() {
super("Utilizando JList");
copiar = new JButton(" Copiar >>>> ");

lista = new JList(cores);
setLayout(new FlowLayout());
lista.setVisibleRowCount(3); // definida linhas visíveis
lista.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); // método de seleção única de opções

add(lista);
add(copiar);

listaCopia = new JList<String>();
listaCopia.setVisibleRowCount(3);
listaCopia.setFixedCellWidth(100);
listaCopia.setFixedCellHeight(15);
add(new JScrollPane(listaCopia));

model = new DefaultListModel(); <-- initialize model
listaCopia.setModel(model); <-- set model

copiar.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//listaCopia.setListData(lista.getSelectedValuesList().toArray());
model.addElement(lista.getSelectedValue()); <-- Add to model
}
});
lista.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
getContentPane().setBackground(cor[lista.getSelectedIndex()]);

}
});

}

您可以看到<--我在其中添加了模型并向模型添加了一个元素。

<小时/>

编辑:

"I compile another code just using getSelectedValuesList(), and don't worked... why ?"

getSelectedValuesList()返回 ListsetListData需要传递一个数组参数。另外当你 setListData您不能一次只更改添加一项。为此,您需要使用该模型。

参见setListData() javadoc | getSelectedValuesList() javadoc

关于Java - JList , getSelectedValues 方法的困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20714489/

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