这是我的代码。我试图学习java但我不懂。
import java.awt.BorderLayout;
import java.awt.GridLayout;
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.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class Dual{
public static JFrame myframe;
public JPanel mypanel;
private JList sourceList;
private DefaultListModel sourceListModel;
private JList destList;
private DefaultListModel destListModel;
private JButton addButton;
private JButton removeButton;
public Dual(){
myframe = new JFrame("Dual List Box");
myframe.setDefaultCloseOperation(myframe.EXIT_ON_CLOSE);
myframe.setSize(400, 300);
myframe.setResizable(false);
mypanel = new JPanel();
myframe.getContentPane().add(mypanel);
initScreen();
data();
}
private void initScreen() {
mypanel.setLayout(new GridLayout(0, 2));
sourceListModel = new DefaultListModel();
sourceList = new JList(sourceListModel);
addButton = new JButton(">>");
addButton.addActionListener(new AddListener());
removeButton = new JButton("<<");
//removeButton.addActionListener(new RemoveListener());
destListModel = new DefaultListModel();
destList = new JList(destListModel);
JPanel leftPanel = new JPanel(new BorderLayout());
leftPanel.add(new JLabel("Available Elements:"), BorderLayout.NORTH);
leftPanel.add(new JScrollPane(sourceList), BorderLayout.CENTER);
leftPanel.add(addButton, BorderLayout.SOUTH);
JPanel rightPanel = new JPanel(new BorderLayout());
rightPanel.add(new JLabel("Selected Elements:"), BorderLayout.NORTH);
rightPanel.add(new JScrollPane(destList), BorderLayout.CENTER);
rightPanel.add(removeButton, BorderLayout.SOUTH);
mypanel.add(leftPanel);
mypanel.add(rightPanel);
}
private void data(){
//sourceListModel.firstElement(request.getParameter("t" + i));
sourceListModel.addElement("ABC");
sourceListModel.addElement(new String[] { "One", "Two", "Three" });
sourceListModel.addElement(new String[] { "Four", "Five", "Six" });
sourceListModel.addElement(new String[] { "Seven", "Eight", "Nine" });
sourceListModel.addElement(new String[] { "Ten", "Eleven", "Twelve" });
sourceListModel.addElement(new String[] { "Thirteen", "Fourteen", "Fifteen" });
sourceListModel.addElement(new String[] { "Sixteen", "Seventeen", "Eighteen" });
sourceListModel.addElement(new String[] { "Nineteen", "Twenty", "Thirty" });
sourceListModel.addElement("GREHDH");
destListModel.addElement("DER");
destListModel.addElement("GRTD");
destListModel.addElement("HRET");
}
private void showSourceSelected() {
Object selected[] = sourceList.getSelectedValues();
System.out.println(selected.length);
for (int i = selected.length - 1; i >= 0; --i) {
System.out.println(selected[i]);
}
}
private void change1Selected() {
Object selected[] = sourceList.getSelectedValues();
int descout = destList.getModel().getSize();
int c = 0;
//System.out.println(selected.length);
for (int i = descout + 1; i <=selected.length+descout; i++) {
destListModel.addElement(selected);
destListModel.addElement("belble"+c);
c++;
}
for (int i = selected.length - 1; i >= 0; --i) {
sourceListModel.removeElement(selected[i]);
//System.out.println(selected[i]);
}
//sourceList.getSelectionModel().clearSelection();
}
//myframe = new JFrame("Dual List Box ");
//myframe.;
private class AddListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Object selected[] = sourceList.getSelectedValues();
showSourceSelected();
change1Selected();
//sourceList.getSelectionModel().clearSelection();
//addDestinationElements(selected);
//clearSourceSelected();
}
}
}
左键正在运行,就像我正在尝试制作duallist的主题一样。 我不知道如何单独或多次选择。 如何添加单个或多重播放元素。 如何切换列表没有搞笑的jlang.object或字符串blabla。
基于:Dual List Box示例:双列表«Swing«Java教程
编辑 新代码
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class Dual{
public static JFrame myframe;
public JPanel mypanel;
private JList sourceList;
private DefaultListModel sourceListModel;
private JList destList;
private DefaultListModel destListModel;
private JButton addButton;
private JButton removeButton;
public Dual(){
myframe = new JFrame("Dual List Box");
myframe.setDefaultCloseOperation(myframe.EXIT_ON_CLOSE);
myframe.setSize(400, 300);
myframe.setResizable(false);
mypanel = new JPanel();
myframe.getContentPane().add(mypanel);
initScreen();
data();
}
private void initScreen() {
mypanel.setLayout(new GridLayout(0, 2));
sourceListModel = new DefaultListModel();
sourceList = new JList(sourceListModel);
addButton = new JButton(">>");
addButton.addActionListener(new AddListener());
removeButton = new JButton("<<");
removeButton.addActionListener(new RemoveListener());
destListModel = new DefaultListModel();
destList = new JList(destListModel);
ListSelectionListener listener1 = new ListSelectionListener(){
JList b = null;
@Override
public void valueChanged(ListSelectionEvent e) {
if (!e.getValueIsAdjusting()) {
// TODO Auto-generated method stub
//System.out.println(sourceselcted());
if(e.getSource().equals(sourceList)){
System.out.println("Source selcted");
if(b!=e.getSource()){
destList.clearSelection();
}
}
else{
System.out.println("Destination selcted");
if(b!=e.getSource()){
sourceList.clearSelection();
}
}
b=(JList) e.getSource();
//System.out.println(b);
}
}
};
destList.addListSelectionListener(listener1);
sourceList.addListSelectionListener(listener1);
JPanel leftPanel = new JPanel(new BorderLayout());
leftPanel.add(new JLabel("Available Elements:"), BorderLayout.NORTH);
leftPanel.add(new JScrollPane(sourceList), BorderLayout.CENTER);
leftPanel.add(addButton, BorderLayout.SOUTH);
JPanel rightPanel = new JPanel(new BorderLayout());
rightPanel.add(new JLabel("Selected Elements:"), BorderLayout.NORTH);
rightPanel.add(new JScrollPane(destList), BorderLayout.CENTER);
rightPanel.add(removeButton, BorderLayout.SOUTH);
mypanel.add(leftPanel);
mypanel.add(rightPanel);
/* sourceList.addMouseListener(new MouseAdapter() {
int lastSelectedIndex;
public void mouseClicked(MouseEvent e) {
int index = sourceList.locationToIndex(e.getPoint());
if (index != -1 && index == lastSelectedIndex) {
sourceList.clearSelection();
}
lastSelectedIndex = sourceList.getSelectedIndex();
}
});
*/
}
private void data(){
sourceListModel.addElement("ABC");
sourceListModel.addElement("One" );
sourceListModel.addElement("Four");
sourceListModel.addElement("Seven");
sourceListModel.addElement("Ten");
sourceListModel.addElement("Thirteen");
sourceListModel.addElement("Sixteen");
sourceListModel.addElement("Nineteen");
sourceListModel.addElement("GREHDH");
destListModel.addElement("DER");
destListModel.addElement("GRTD");
destListModel.addElement("HRET");
}
private void showSourceSelected() {
Object selectedcount[] = sourceList.getSelectedValues();
System.out.println(selectedcount.length);
for (int i = selectedcount.length - 1; i >= 0; --i) {
System.out.println(selectedcount[i]);
}
}
private void showSourceSelected2() {
Object selectedcount[] = destList.getSelectedValues();
System.out.println(selectedcount.length);
for (int i = selectedcount.length - 1; i >= 0; --i) {
System.out.println(selectedcount[i]);
}
}
private int countlist(JList list){
return list.getSelectedValues().length;
}
private void change1Selected() {
Object selectedcount[] = sourceList.getSelectedValues();
int count = selectedcount.length;
Object selected = sourceList.getSelectedValue();
//int descout = destList.getModel().getSize();
//int c = 0;
//System.out.println(selected.length);
// for (int i = descout + 1; i <=descout+1; i++) {
for(int i=0;i<=count-1;i++){
destListModel.addElement(selectedcount[i]);
sourceListModel.removeElement(selectedcount[i]);
// }
// for (int i = selected.length - 1; i >= 0; --i) {
}
//System.out.println(selected[i]);
//}
//sourceList.getSelectionModel().clearSelection();
}
private void change2Selected() {
Object selectedcount[] = destList.getSelectedValues();
int count = selectedcount.length;
Object selected = destList.getSelectedValue();
//int descout = destList.getModel().getSize();
//int c = 0;
//System.out.println(selected.length);
// for (int i = descout + 1; i <=descout+1; i++) {
for(int i=0;i<=count-1;i++){
sourceListModel.addElement(selectedcount[i]);
destListModel.removeElement(selectedcount[i]);
//destListModel.addElement("belble"+c);
//c++;
// }
// for (int i = selected.length - 1; i >= 0; --i) {
}
//System.out.println(selected[i]);
//}
//sourceList.getSelectionModel().clearSelection();
}
private class AddListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Object selected[] = sourceList.getSelectedValues();
//showSourceSelected();
System.out.println(countlist(sourceList));
change1Selected();
//sourceList.getSelectionModel().clearSelection();
//addDestinationElements(selected);
//clearSourceSelected();
}
}
private class RemoveListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Object selected[] = destList.getSelectedValues();
//showSourceSelected2();
System.out.println(countlist(destList));
change2Selected();
//sourceList.getSelectionModel().clearSelection();
//addDestinationElements(selected);
//clearSourceSelected();
}
}
private boolean sourceselcted() {
int sourcelength = sourceList.getSelectedValues().length;
if(sourcelength>=1)return true;
return false;
}
private boolean destselcted() {
int sourcelength = destList.getSelectedValues().length;
if(sourcelength>=1)return true;
return false;
}
}
如何排序?有没有现成的功能,或者我必须自己做? 什么是Jlist with Object // String声明? DefaultListModel,AbstractListModel,ListModel和什么时候使用什么? 如何使用子类型数据制作Jlist ABC - 名称:xxx,ForVesrion:yyy,类型:zzz;并按xxx或yyy或zzz或任何其他yyy / zzz / xxx排序.. ??
答案 0 :(得分:1)
如何在没有搞笑的jlang.object
的情况下切换列表
为什么要更改示例的代码?原始代码使用自定义ListModel。您的代码使用DefaultListModel。
默认情况下,JList只会在添加到ListModel的任何元素上调用toString()
方法。
sourceListModel.addElement(new String[] { "One", "Two", "Three" });
您的代码正在向ListModel添加一个数组。 Arry的toString()
方法将显示“jlang.object”等数据。
原始代码将Array的各个元素添加到ListModel。
因此,解决方案是不向ListModel添加数组。相反,您需要单独添加每个元素:
sourceListModel.addElement("One");
sourceListModel.addElement("Two");
sourceListModel.addElement("Three");