我正在尝试创建一个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();
}
}
这就是我所拥有的。
答案 0 :(得分:1)
对书籍商品
使用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);
}
在addButton
方法
buildButtonPanel()
添加监听器
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);
}
最后,ActionListener
的{{1}}
removeButton