1

这是我大学的作业之一。我有一个使用 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);
    }
}
4

0 回答 0