gpt4 book ai didi

java - 需要使用 swing 在 Java 列表之间移动项目的帮助

转载 作者:行者123 更新时间:2023-12-02 03:28:32 24 4
gpt4 key购买 nike

我需要使用添加按钮将项目从左侧列表移动到右侧列表_1。删除按钮需要从右侧列表中删除项目。我在完成这一部分时遇到了困难。

package com.cooksys.assessment;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JMenu;
import javax.swing.JList;
import java.awt.GridLayout;
import javax.swing.JTextField;
import javax.swing.AbstractListModel;
import java.awt.Font;

public class Window {

private JFrame frame;

/**
* Launch the application. The main method is the entry point to a Java application.
* For this assessment, you shouldn't have to add anything to this.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Window window = new Window();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the application. This is the constructor for this Window class.
* All of the code here will be executed as soon as a Window object is made.
*/
public Window() {
initialize();
}

/**
* Initialize the contents of the frame. This is where Window Builder
* will generate its code.
*/
public void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 479);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);

JMenu mnFile = new JMenu("File");
menuBar.add(mnFile);

JMenuItem mntmLoad = new JMenuItem("Load");
mnFile.add(mntmLoad);

JMenuItem mntmSave = new JMenuItem("Save");
mnFile.add(mntmSave);

JMenuItem mntmExit = new JMenuItem("Exit");
mnFile.add(mntmExit);
frame.getContentPane().setLayout(null);

JButton remove = new JButton("<<");
remove.setBounds(173, 222, 79, 23);
frame.getContentPane().add(remove);

JButton Add = new JButton(">>");
Add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

}
});
Add.setBounds(173, 188, 79, 23);
frame.getContentPane().add(Add);

JList list = new JList();
list.setFont(new Font("Tahoma", Font.PLAIN, 16));
list.setModel(new AbstractListModel() {
String[] values = new String[] {"Case", "Motherboard", "CPU", "RAM", "GPU", "HDD", "PSU"};
public int getSize() {
return values.length;
}
public Object getElementAt(int index) {
return values[index];
}
});
list.setBounds(0, 0, 161, 419);
frame.getContentPane().add(list);

JList list_1 = new JList();
list_1.setBounds(262, 0, 172, 419);
frame.getContentPane().add(list_1);



}
}

最佳答案

为此,您应该使用以下代码设置列表选择模式:

list.setSelectionMode( mode );

哪种模式可以是以下模式之一:

  1. ListSelectionModel.SINGLE_SELECTION
  2. ListSelectionModel.SINGLE_INTERVAL_SELECTION
  3. ListSelectionModel.MULTIPLE_INTERVAL_SELECTION

还可以使用DefaultListModel代替AbstractListModel

通过将第三种模式设置为列表,您可以通过按住 ctrl 按钮并单击来从每个列表中选择多个项目。

在按钮 ActionListener 中使用 list.getSelectedValuesList() 来获取所选值的列表,现在您需要的只是使用循环并从 添加值>源列表目标列表同时将其从源列表中删除

我更改了代码的一些部分:

  • 已删除
    • AbstractListModel 用于列表
  • 已添加
    • DefaultListModel 列表
      • model_list 用于列表
      • model_list_1 对应 list_1
    • ActionListener 用于删除按钮。

我的所有更改都在代码中进行了注释,阅读它们以了解每个部分。

这是不重命名变量的代码:

为了正常工作,我必须在您的代码中移动一些代码。

package com.cooksys.assessment;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JMenu;
import javax.swing.JList;
import java.awt.GridLayout;
import javax.swing.JTextField;
import javax.swing.AbstractListModel;
import java.awt.Font;
import javax.swing.ListSelectionModel;
import javax.swing.DefaultListModel;

public class Window {

private JFrame frame;

/**
* Launch the application. The main method is the entry point to a Java
* application. For this assessment, you shouldn't have to add anything to
* this.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Window window = new Window();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the application. This is the constructor for this Window class.
* All of the code here will be executed as soon as a Window object is made.
*/
public Window() {
initialize();
}

/**
* Initialize the contents of the frame. This is where Window Builder will
* generate its code.
*/
public void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 479);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

String[] values = new String[]{"Case", "Motherboard", "CPU", "RAM", "GPU", "HDD", "PSU"};

// using default list model.
DefaultListModel model_list = new DefaultListModel();
DefaultListModel model_list_1 = new DefaultListModel();

JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);

JMenu mnFile = new JMenu("File");
menuBar.add(mnFile);

JMenuItem mntmLoad = new JMenuItem("Load");
mnFile.add(mntmLoad);

JMenuItem mntmSave = new JMenuItem("Save");
mnFile.add(mntmSave);

JMenuItem mntmExit = new JMenuItem("Exit");
mnFile.add(mntmExit);
frame.getContentPane().setLayout(null);

JList list = new JList();
JList list_1 = new JList();

JButton remove = new JButton("<<");
remove.setBounds(173, 222, 79, 23);
frame.getContentPane().add(remove);

JButton Add = new JButton(">>");
Add.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// get list of selected values and for each one of them do following
list.getSelectedValuesList().stream().forEach((data) -> {
// moving data
model_list_1.addElement(data);
// remove from other side
model_list.removeElement(data);
});
// refreshing the view after changes
list.revalidate();
list_1.revalidate();
}
});

Add.setBounds(173, 188, 79, 23);
frame.getContentPane().add(Add);

list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
list.setFont(new Font("Tahoma", Font.PLAIN, 16));
list.setBounds(0, 0, 161, 419);
list.setModel(model_list);
frame.getContentPane().add(list);
list_1.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
list_1.setBounds(262, 0, 172, 419);
list_1.setModel(model_list_1);
frame.getContentPane().add(list_1);

// Add values to list using list_model
for (String value : values) {
model_list.addElement(value);
}

remove.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
list_1.getSelectedValuesList().stream().forEach((data) -> {
model_list.addElement(data);
model_list_1.removeElement(data);
});
list.revalidate();
list_1.revalidate();
}
});

}
}

关于java - 需要使用 swing 在 Java 列表之间移动项目的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38426064/

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