这是我大学的作业之一..我有一个使用JApplet的链接列表,并且我面临的第3个问题是我想要在点击Insert_Front按钮后,新按钮应该放在任何存在的按钮之前在面板上而不是最后一个(例如,已经存在的值为5的按钮,在单击Insert_Front按钮后,创建的新按钮应放在按钮5之前)第二个问题是在单击Del_Front按钮后第一个按钮将被删除而不是最后一个按钮,最后搜索按钮无法找到面板中的按钮。欢迎使用解决方案..代码也是如此..
public interface BasicLinkedList1<itemtype>
{
public void insert_front(itemtype item);
public void delete_front();
public void insert_rear(itemtype item);
}
public interface listsearch<itemtype>
{
public void search(itemtype v);
}
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
/* <applet code="LinkedListApplet.class" height=300 width=500></applet>*/
public class LinkedListApplet extends JApplet implements listsearch<JButton>,BasicLinkedList1<JButton>
{
JButton Insert_Front,Insert_Rear,Del_Front,search;
JLabel label;JPanel npanel,spanel;JTextField field;
LinkedList<JButton> linklist;
public void init()
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
makegui();
}
});
}
void makegui()
{
linklist = new LinkedList<JButton>();
npanel = new JPanel();
spanel = new JPanel();
label = new JLabel();
field = new JTextField(40);
spanel.setLayout(new FlowLayout());
npanel.setLayout(new GridLayout(2,5));
listener list = new listener();
Insert_Front = new JButton("Insert_Front");
Insert_Front.addActionListener(list);
npanel.add(Insert_Front);
Insert_Rear = new JButton("Insert_Rear");
Insert_Rear.addActionListener(list);//adding listener for buttons
npanel.add(Insert_Rear);
Del_Front=new JButton("Del_Front");
Del_Front.addActionListener(list);
npanel.add(Del_Front);
search=new JButton("search");
search.addActionListener(list);
npanel.add(search);
npanel.add(field);
npanel.add(label,SwingUtilities.CENTER);
add(npanel,BorderLayout.NORTH);
add(spanel,BorderLayout.CENTER);
}
private class listener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String cmd=e.getActionCommand(),text="";
System.out.println(cmd);
if(cmd.equals("Insert_Front"))
{
text=field.getText();
if(text.length()==0)
label.setText("enter some value!!!");
else
{
label.setText("node successfully created");
insert_front(new JButton(text));
}
}
else if(cmd.equals("Insert_Rear"))
{
text=field.getText();
if(text.length()==0)
label.setText("enter some value!!!");
else
{
label.setText("node successfully created");
insert_rear(new JButton(text));
}
}
else if (cmd.equals("Del_Front"))
{
label.setText("node successfully destroyed");
delete_front();
}
else if(cmd.equals("search"))
search(new JButton(text));
}
}
public void search(JButton b)
{
if(linklist.contains(b))
{
label.setText("element found:"+b.getText());
}
else
label.setText("no such elements");
}
public void insert_front(JButton item)
{
linklist.addFirst(item);
ListIterator<JButton> itr=linklist.listIterator();
JButton b=itr.next();
spanel.add(b);
}
public void delete_front()
{
JButton b=null;
if(linklist.size()==0)
label.setText("cannot delete node,list is empty!");
else
{
b=linklist.removeFirst();
spanel.remove(b);
}
}
public void insert_rear(JButton item)
{
linklist.addLast(item);JButton s=null;
ListIterator<JButton> itr=linklist.listIterator();
while(itr.hasNext())
s=itr.next();
JButton b=s;
spanel.add(b);
}
}