我有一个队列实现为链表。我想从另一个类中排序这个队列。两个类都在同一个包中。我的排序列表是在公共类上,我想把它放在公共场所,所以我可以制作一个GUI。我想我错过了什么,我不知道它是什么。有人可以帮助我。
这是我的完整代码:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.LinkedList;
import java.util.*;
public class Search extends JFrame {
private JLabel lblSearch;
private JTextField txtSearch;
private JButton btnSort, btnborrow, btnreturn, btnsearch;
public JList listbooks, returnbooks;
public Object[] books;
private ListModel[] listmodel;
private LinkedList ll = new LinkedList(); {
ll.add("The Fault In Our Stars");
ll.add("Solid Mensuration");
ll.add("Analytic Geometry");
ll.add("Engineering Mechanics");
ll.add("Perks Of Being A Wallflower");
ll.add("Reckless");
ll.add("Paper Towns");
ll.add("Stewart's Algebra And Trigonometry");
ll.add("The Lost Hero");
ll.add("The Son Of Neptune");
ll.add("Harry Potter And The Sorcerer's Stone");
ll.add("Paper Towns");
ll.add("Looking For Alaska");
ll.add("Principles Of Taxation");
ll.add("Contemporary Physics");
ll.add("The House Of Hades");
ll.add("The Last Olympian");
ll.add("An Imperial Affliction");
ll.add("The Daily Bread");
ll.add("Looking For Alaska");
ll.add("The Lightning Thief");
ll.add("Sociology Focus On The Philippines");
ll.add("The Red Pyramid");
ll.add("Demigod Diaries");
ll.add("The Battle Of The Labyrinth");
ll.add("Webster's Dictionary");
ll.add("Strength Of Materials");
ll.add("Discrete Mathematics");
ll.add("The Serpent's Shadow");
ll.add("Twilight");
ll.add("Breaking Dawn");
ll.add("Eclipse");
ll.add("Diary Of A Wimpy Kid");
ll.add("Electronic Devices And Circuit Theory");
ll.add("The Princess And The Frog");
ll.add("Fifty Shades Of Grey");
ll.add("The Mark Of Athena");
ll.add("The Throne Of Fire");
ll.add("Fifty Shades Of Grey");
ll.add("Divergent");
ll.add("Data Structures And Algorithms");
ll.add("Introduction To Operating System");
ll.add("Integral Calculus");
ll.add("Life And Works Of Rizal");
ll.add("The Sea Of Monsters");
ll.add("Insurgent");
ll.add("Holy Bible");
}
public Search() {
super("TIPQC-Library: SEARCH");
setLayout(null);
getContentPane().setBackground(Color.BLACK);
txtSearch = new JTextField(30);
txtSearch.setBounds(80, 100, 240, 30);
add(txtSearch);
btnsearch = new JButton("Search");
btnsearch.setBounds(330, 100, 90, 28);
add(btnsearch);
DefaultListModel<Object> model = new DefaultListModel<Object>();
for (Object element : ll)
model.addElement(element);
listbooks = new JList(model);
listbooks.setBounds(80, 180, 350, 100);
listbooks.setBackground(Color.YELLOW);
listbooks.setForeground(Color.BLACK);
listbooks.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
add(listbooks);
JScrollPane tscroll = new JScrollPane(listbooks);
tscroll.setBorder(null);
tscroll.setBounds(80, 180, 350, 100);
tscroll.setBackground(Color.YELLOW);
tscroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
add(tscroll);
btnSort = new JButton("Sort");
btnSort.setBounds(90, 300, 80, 25);
add(btnSort);
btnreturn = new JButton("Return");
btnreturn.setBounds(220, 300, 80, 25);
add(btnreturn);
btnborrow = new JButton("Borrow");
btnborrow.setBounds(340, 300, 80, 25);
add(btnborrow);
returnbooks = new JList();
returnbooks.setBounds(80, 380, 350, 100);
returnbooks.setBackground(Color.YELLOW);
returnbooks.setForeground(Color.BLACK);
returnbooks.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
add(returnbooks);
ActionListener listener = new ButtonListener();
btnborrow.addActionListener(listener);
btnSort.addActionListener(listener);
}
public class ButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == btnborrow)
{
Object removed;
DefaultListModel model = (DefaultListModel) listbooks.getModel();
int selectedIndex = listbooks.getSelectedIndex();
if (selectedIndex != -1) {
removed = model.remove(selectedIndex);
}
}
if (source == btnSort) {
Collections.sort(ll);
ListIterator liter = ll.listIterator();
Collections.sort(ll);
liter = ll.listIterator();
while (liter.hasNext())
System.out.println(liter.next());
}
}
}
public void run() {
setSize(500, 500);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
}
public static void main(String[] args) {
Search log = new Search();
log.run();
}
}
答案 0 :(得分:0)
你的意思是,就像在GUI中显示排序列表一样?
Collections.sort(ll);
ListIterator liter = ll.listIterator();
Collections.sort(ll);
liter = ll.listIterator();
DefaultListModel model = (DefaultListModel) listbooks.getModel();
model.clear();
for (Object element : ll)
model.addElement(element);
//while (liter.hasNext())
// System.out.println(liter.next());