gpt4 book ai didi

java - JList 使用对象删除列表

转载 作者:行者123 更新时间:2023-12-02 11:19:02 25 4
gpt4 key购买 nike

我正在尝试制作一个 jFrame 来删除用户选择的列表。但是,我在尝试让它工作时遇到了一些麻烦。

这是 GUI 代码。

public class Books extends JFrame
{
private JList bookList;
private JList selectedBookList;
private JButton addButton;
private JButton removeButton;
private JButton addUpButton;
private JPanel bookPanel;
private JPanel selectedBookPanel;
private JPanel buttonPanel;

private String[] books = { "I Did It Your Way",
"The History of Scotland"};

public Books()
{
super("Books");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setLayout(new BorderLayout());

// Build the panels.
buildBookPanel();
buildSelectedBookPanel();
buildButtonPanel();

// Add the panels to the content pane.
add(bookPanel, BorderLayout.WEST);
add(selectedBookPanel, BorderLayout.EAST);
add(buttonPanel, BorderLayout.CENTER);

// Pack and display the window.
pack();
setVisible(true);
}

private void buildBookPanel()
{
bookPanel = new JPanel();
bookList = new JList(books);

bookList.setSelectionMode(
ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

bookList.setVisibleRowCount(7);

// Add the list to a scroll pane.
JScrollPane monthListScrollPane = new JScrollPane(bookList);

// Add the scroll pane to the panel.
bookPanel.add(monthListScrollPane);
}

private void buildSelectedBookPanel()
{
selectedBookPanel = new JPanel();
selectedBookList = new JList();
selectedBookList.setVisibleRowCount(7);

JScrollPane selectedMonthScrollPane =
new JScrollPane(selectedBookList);

selectedBookPanel.add(selectedMonthScrollPane);
}

private void buildButtonPanel()
{
buttonPanel = new JPanel();
addButton = new JButton("Get Selections");
removeButton=new JButton("Remove Selections");
addUpButton=new JButton("Check Out");
addButton.addActionListener(new ButtonListener());
removeButton.addActionListener(new removeButton());
addUpButton.addActionListener(new ButtonListener());

buttonPanel.add(addButton);
buttonPanel.add(removeButton);
buttonPanel.add(addUpButton);
}

这是删除按钮的操作 ActionListener。一旦用户选择它,它应该从书籍列表中删除用户输入的内容。如何删除选择?

   private class removeButton implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Object[] selections = bookList.getSelectedValues();
}
}

这是我所拥有的。

enter image description here

最佳答案

对图书项目使用 DefaultListModel 而不是 array

在您的代码中,您使用了书籍数组

private String[] books = { "I Did It Your Way","The History of Scotland"};

将上面的语句替换为

private DefaultListModel<String> books = new DefaultListModel<>();
private DefaultListModel<String> selectedBooks = new DefaultListModel<>();

buildBookPanel() 方法中,添加这样的图书项目

   books.addElement("I Did It Your Way");
books.addElement("The History of Scotland");
books.addElement("Another book name");

您的 buildBookPanel() 方法应如下所示

private void buildBookPanel(){

bookPanel = new JPanel();

books.addElement("I Did It Your Way");
books.addElement("The History of Scotland");
books.addElement("Another book name");

bookList = new JList(books);

bookList.setSelectionMode(
ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

bookList.setVisibleRowCount(7);

// Add the list to a scroll pane.
JScrollPane monthListScrollPane = new JScrollPane(bookList);

// Add the scroll pane to the panel.
bookPanel.add(monthListScrollPane);
}

在方法 buildSelectedBookPanel() 中,将 selectedBookList = new JList(); 更改为 selectedBookList = new JList(selectedBooks);

private void buildSelectedBookPanel(){

selectedBookPanel = new JPanel();
selectedBookList = new JList(selectedBooks);
selectedBookList.setVisibleRowCount(7);

JScrollPane selectedMonthScrollPane = new JScrollPane(selectedBookList);

selectedBookPanel.add(selectedMonthScrollPane);
}

buildButtonPanel() 方法中向 addButton 添加监听器

private void buildButtonPanel()
{
buttonPanel = new JPanel();
addButton = new JButton("Get Selections");
removeButton=new JButton("Remove Selections");
addUpButton=new JButton("Check Out");

addButton.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent arg0)
{

for(Object book : bookList.getSelectedValues())
{
selectedBooks.addElement(book.toString());
books.removeElement(book);
}
}
});

removeButton.addActionListener(new removeButton());
addUpButton.addActionListener(new ButtonListener());

buttonPanel.add(addButton);
buttonPanel.add(removeButton);
buttonPanel.add(addUpButton);
}

最后,removeButtonActionListener

private class removeButton implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{

for(Object book : selectedBookList.getSelectedValues())
{
books.addElement(book.toString());
selectedBooks.removeElement(book);
}



}

}

关于java - JList 使用对象删除列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50066714/

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