gpt4 book ai didi

Java,如何制作双列表

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

这是我的代码。确实,我正在尝试学习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();
}
}
}

左按钮正在运行,正如我正在尝试创建双重列表的主题中一样。我不知道如何处理单个或多个选择。如何添加单个或多个元素。如何在没有有趣的 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;
}

}

如何排序?有没有现成的功能,还是我必须自己制作?什么是带有 Object//String 声明的 Jlist?DefaultListModel、AbstractListModel、ListModel 之间有什么区别以及何时使用什么?如何使用子类型数据 ABC 制作 Jlist - 名称:xxx,ForVesrion:yyy,类型:zzz;并按 xxx 或 yyy 或 zzz 或任何其他 yyy/zzz/xxx 排序..??

最佳答案

How to switch betwen list without funny 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");

关于Java,如何制作双列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27365377/

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