繁体   English   中英

JList使用对象删除列表

[英]JList remove a list using a object

我正在尝试制作一个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();
        }
   }

这就是我所拥有的。

在此处输入图片说明

对书籍项目使用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()方法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);
           }



        }

   }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM