有人可以解释这段代码中的错误吗? 我正在尝试将数据从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?的方式,但没有解释......
我只编译了另一个代码using getSelectedValuesList()
,并且没有工作......为什么?
答案 0 :(得分:2)
似乎适合我。当我移动/选择所有项目并单击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()]);
}
});
}
您可以看到我添加模型的<--
,并在模型中添加元素。
编辑:
“我使用getSelectedValuesList()编译另一个代码,但是没有用...为什么?”
getSelectedValuesList()
返回List
,setListData
需要传递数组参数。此外,当您setListData
时,您无法一次只更改添加一项。为此,您需要使用该模型。