import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.applet.*;
public class NewJApplet1 extends JApplet {   
    private JButton btn1,btn2,btn3,btn4,btn5; 
    private JTextField tf;
    private JLabel l1,l2;
    private JPanel pane,panel;
    private JPanel p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12,p13,p14,p15;
    private JButton[]btn = new JButton[60];
    private JLabel []la = new JLabel[60];
    private JLabel []l = new JLabel[60];
    Box q1=Box.createHorizontalBox();
    Box q2=Box.createHorizontalBox();
    Box w1=Box.createVerticalBox();
    Box q3=Box.createHorizontalBox();
    private int n=60;
    public void init(){
       pane = new JPanel(); 
       btn1 = new JButton("新建");
       btn2 = new JButton("添加");
       btn3 = new JButton("插入");
       btn4 = new JButton("删除");
       btn5 = new JButton("查找");
       l1 = new JLabel("输入整数"); 
       l1.setFont(new Font("Dialog",Font.BOLD,14)); 
       tf = new JTextField(6);
       tf.setBorder(BorderFactory.createLineBorder(Color.PINK));
       q1.add(btn1); 
       q1.add(Box.createHorizontalStrut(10));
       q1.add(btn2); 
       q1.add(Box.createHorizontalStrut(10));
       q1.add(btn3); 
       q1.add(Box.createHorizontalStrut(10));
       q1.add(btn4); 
       q1.add(Box.createHorizontalStrut(10));
       q1.add(btn5);
       q1.add(Box.createHorizontalStrut(10));
       q1.add(l1); 
       q1.add(Box.createHorizontalStrut(10));
       q1.add(tf);
       l2 = new JLabel("Press any button",JLabel.CENTER);
       q2.add(l2);
       w1.add(q1);
       w1.add(Box.createVerticalStrut(10));
       w1.add(q2);
       pane=new JPanel();
       pane.add(w1);  
        p1 = new JPanel();
        p2 = new JPanel();
        p3 = new JPanel();
        p4 = new JPanel();
        p5 = new JPanel(); 
        p6 = new JPanel(); 
        p7 = new JPanel();
        p8 = new JPanel();
        p9 = new JPanel();
        p10 = new JPanel();
        p11 = new JPanel(); 
        p12 = new JPanel(); 
        p13 = new JPanel();
        p14 = new JPanel(); 
        p15 = new JPanel();
       p1.setLayout(new GridLayout(12,0));
       p2.setLayout(new GridLayout(12,0));
       p3.setLayout(new GridLayout(12,0));
       p4.setLayout(new GridLayout(12,0));
       p5.setLayout(new GridLayout(12,0));
       p6.setLayout(new GridLayout(12,0));
       p7.setLayout(new GridLayout(12,0));
       p8.setLayout(new GridLayout(12,0));
       p9.setLayout(new GridLayout(12,0));
       p10.setLayout(new GridLayout(12,0));
       p11.setLayout(new GridLayout(12,0));
       p12.setLayout(new GridLayout(12,0));
       p13.setLayout(new GridLayout(12,0));
       p14.setLayout(new GridLayout(12,0));
       p15.setLayout(new GridLayout(12,0));
       for(int i=0;i<12;i++)
       {  
          JLabel labe = new JLabel(""+i);
          la[i]=labe;
          p1.add(labe);
          JButton button = new JButton();
          button.setPreferredSize(new Dimension(65,21));
          btn[i]=button;
          btn[i].setBorder(BorderFactory.createLineBorder(Color.PINK));
          p2.add(button);
          button.setEnabled(false);
          JLabel label = new JLabel("←");
          l[i]=label;
          label.setForeground(Color.RED);
          label.setVisible(false);
          p3.add(label);
       }
       for(int i=12;i<24;i++)
       {  
          JLabel labe = new JLabel(""+i);
          la[i]=labe;
          p4.add(labe);
          JButton button = new JButton();
          button.setPreferredSize(new Dimension(65,21));
          btn[i]=button;
          btn[i].setBorder(BorderFactory.createLineBorder(Color.PINK));
          p5.add(button);
          button.setEnabled(false);
          JLabel label = new JLabel("←");
          l[i]=label;
          label.setForeground(Color.RED);
          label.setVisible(false);
          p6.add(label);
       }
       for(int i=24;i<36;i++)
       {  
          JLabel labe = new JLabel(""+i);
          la[i]=labe;
          p7.add(labe);
          JButton button = new JButton();
          button.setPreferredSize(new Dimension(65,21));
          btn[i]=button;
          btn[i].setBorder(BorderFactory.createLineBorder(Color.PINK));
          p8.add(button);
          button.setEnabled(false);
          JLabel label = new JLabel("←");
          l[i]=label;
          label.setForeground(Color.RED);
          label.setVisible(false);
          p9.add(label);
       }
       for(int i=36;i<48;i++)
       {  
          JLabel labe = new JLabel(""+i);
          la[i]=labe;
          p10.add(labe);
          JButton button = new JButton();
          button.setPreferredSize(new Dimension(65,21));
          btn[i]=button;
          btn[i].setBorder(BorderFactory.createLineBorder(Color.PINK));
          p11.add(button);
          button.setEnabled(false);
          JLabel label = new JLabel("←");
          l[i]=label;
          label.setForeground(Color.RED);
          label.setVisible(false);
          p12.add(label);
       }
       for(int i=48;i<60;i++)
       {  
          JLabel labe = new JLabel(""+i);
          la[i]=labe;
          p13.add(labe);
          JButton button = new JButton();
          button.setPreferredSize(new Dimension(65,21));
          btn[i]=button;
          btn[i].setBorder(BorderFactory.createLineBorder(Color.PINK));
          p14.add(button);
          button.setEnabled(false);
          JLabel label = new JLabel("←");
          l[i]=label;
          label.setForeground(Color.RED);
          label.setVisible(false);
          p15.add(label);
       }
       q3.add(p1);q3.add(p2);q3.add(p3);q3.add(Box.createHorizontalStrut(10));
       q3.add(p4);q3.add(p5);q3.add(p6);q3.add(Box.createHorizontalStrut(10));
       q3.add(p7);q3.add(p8);q3.add(p9);q3.add(Box.createHorizontalStrut(10));
       q3.add(p10);q3.add(p11);q3.add(p12);q3.add(Box.createHorizontalStrut(10));
       q3.add(p13);q3.add(p14);q3.add(p15);
       panel=new JPanel(new FlowLayout(FlowLayout.LEFT));
       panel.add(q3);
       Container cp=getContentPane(); 
       cp.setBackground(Color.GRAY);
       cp.setLayout(new BorderLayout()); 
       cp.add(pane,BorderLayout.NORTH);  
       cp.add(panel,BorderLayout.CENTER);
       setSize(520,340);
       JSplitPane split;
       split=new JSplitPane(JSplitPane.VERTICAL_SPLIT, pane,panel);
       pane.setBorder(BorderFactory.createLineBorder(Color.PINK));
       panel.setBorder(BorderFactory.createLineBorder(Color.PINK));
       cp.add(split);
       split.setDividerLocation(0.8);
       split.setOneTouchExpandable(true);
       split.setDividerSize(8);
       l[0].setVisible(true);
        for(int i=0;i<30;i++)
       {
         Random r=new Random();
         int t=r.nextInt(999);
         add(t);     
        
       }     }
    public void add(int key) {   
    int  h=key%n;
    if(btn[h].getLabel().equals(null))
    {   
       btn[h].setLabel(Integer.toString(key));
    }
    else{
    int a=1;
    h=(h+a)%n;
    while(!(btn[h].getLabel().equals("")))
    h=(h+a)%n;
    btn[h].setLabel(String.valueOf(key));
      //btn[n].setForeground(Color.BLUE);
        }    
    }}

解决方案 »

  1.   

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.Font;
    import java.awt.FontMetrics;
    import java.awt.Graphics;
    import java.awt.GridLayout;
    import java.awt.Rectangle;
    import java.util.Random;import javax.swing.AbstractButton;
    import javax.swing.BorderFactory;
    import javax.swing.Box;
    import javax.swing.ButtonModel;
    import javax.swing.JApplet;
    import javax.swing.JButton;
    import javax.swing.JComponent;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JSplitPane;
    import javax.swing.JTextField;
    import javax.swing.plaf.basic.BasicButtonUI;import sun.swing.SwingUtilities2;public class NewJApplet1 extends JApplet {
        private JButton btn1, btn2, btn3, btn4, btn5;    private JTextField tf;    private JLabel l1, l2;    private JPanel pane, panel;    private JPanel p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15;    private JButton[] btn = new JButton[60];    private JLabel[] la = new JLabel[60];    private JLabel[] l = new JLabel[60];    Box q1 = Box.createHorizontalBox();    Box q2 = Box.createHorizontalBox();    Box w1 = Box.createVerticalBox();    Box q3 = Box.createHorizontalBox();    private int n = 60;    public void init() {
            pane = new JPanel();
            btn1 = new JButton("新建");
            btn2 = new JButton("添加");
            btn3 = new JButton("插入");
            btn4 = new JButton("删除");
            btn5 = new JButton("查找");
            l1 = new JLabel("输入整数");
            l1.setFont(new Font("Dialog", Font.BOLD, 14));
            tf = new JTextField(6);
            tf.setBorder(BorderFactory.createLineBorder(Color.PINK));
            q1.add(btn1);
            q1.add(Box.createHorizontalStrut(10));
            q1.add(btn2);
            q1.add(Box.createHorizontalStrut(10));
            q1.add(btn3);
            q1.add(Box.createHorizontalStrut(10));
            q1.add(btn4);
            q1.add(Box.createHorizontalStrut(10));
            q1.add(btn5);
            q1.add(Box.createHorizontalStrut(10));
            q1.add(l1);
            q1.add(Box.createHorizontalStrut(10));
            q1.add(tf);
            l2 = new JLabel("Press any button", JLabel.CENTER);
            q2.add(l2);
            w1.add(q1);
            w1.add(Box.createVerticalStrut(10));
            w1.add(q2);
            pane = new JPanel();
            pane.add(w1);
            p1 = new JPanel();
            p2 = new JPanel();
            p3 = new JPanel();
            p4 = new JPanel();
            p5 = new JPanel();
            p6 = new JPanel();
            p7 = new JPanel();
            p8 = new JPanel();
            p9 = new JPanel();
            p10 = new JPanel();
            p11 = new JPanel();
            p12 = new JPanel();
            p13 = new JPanel();
            p14 = new JPanel();
            p15 = new JPanel();
      

  2.   

    p1.setLayout(new GridLayout(12, 0));
            p2.setLayout(new GridLayout(12, 0));
            p3.setLayout(new GridLayout(12, 0));
            p4.setLayout(new GridLayout(12, 0));
            p5.setLayout(new GridLayout(12, 0));
            p6.setLayout(new GridLayout(12, 0));
            p7.setLayout(new GridLayout(12, 0));
            p8.setLayout(new GridLayout(12, 0));
            p9.setLayout(new GridLayout(12, 0));
            p10.setLayout(new GridLayout(12, 0));
            p11.setLayout(new GridLayout(12, 0));
            p12.setLayout(new GridLayout(12, 0));
            p13.setLayout(new GridLayout(12, 0));
            p14.setLayout(new GridLayout(12, 0));
            p15.setLayout(new GridLayout(12, 0));
            for (int i = 0; i < 12; i++) {
                JLabel labe = new JLabel("" + i);
                la[i] = labe;
                p1.add(labe);
                JButton button = new JButton();
                button.setPreferredSize(new Dimension(65, 21));
                btn[i] = button;
                btn[i].setBorder(BorderFactory.createLineBorder(Color.PINK));
                p2.add(button);
                button.setEnabled(false);
                JLabel label = new JLabel("←");
                l[i] = label;
                label.setForeground(Color.RED);
                label.setVisible(false);
                p3.add(label);
            }
            for (int i = 12; i < 24; i++) {
                JLabel labe = new JLabel("" + i);
                la[i] = labe;
                p4.add(labe);
                JButton button = new JButton();
                button.setPreferredSize(new Dimension(65, 21));
                btn[i] = button;
                btn[i].setBorder(BorderFactory.createLineBorder(Color.PINK));
                p5.add(button);
                button.setEnabled(false);
                JLabel label = new JLabel("←");
                l[i] = label;
                label.setForeground(Color.RED);
                label.setVisible(false);
                p6.add(label);
            }
            for (int i = 24; i < 36; i++) {
                JLabel labe = new JLabel("" + i);
                la[i] = labe;
                p7.add(labe);
                JButton button = new JButton();
                button.setPreferredSize(new Dimension(65, 21));
                btn[i] = button;
                btn[i].setBorder(BorderFactory.createLineBorder(Color.PINK));
                p8.add(button);
                button.setEnabled(false);
                JLabel label = new JLabel("←");
                l[i] = label;
                label.setForeground(Color.RED);
                label.setVisible(false);
                p9.add(label);
            }
            for (int i = 36; i < 48; i++) {
                JLabel labe = new JLabel("" + i);
                la[i] = labe;
                p10.add(labe);
                JButton button = new JButton();
                button.setPreferredSize(new Dimension(65, 21));
                btn[i] = button;
                btn[i].setBorder(BorderFactory.createLineBorder(Color.PINK));
                p11.add(button);
                button.setEnabled(false);
                JLabel label = new JLabel("←");
                l[i] = label;
                label.setForeground(Color.RED);
                label.setVisible(false);
                p12.add(label);
            }
            for (int i = 48; i < 60; i++) {
                JLabel labe = new JLabel("" + i);
                la[i] = labe;
                p13.add(labe);
                JButton button = new JButton();
                button.setForeground(Color.RED);
                button.setUI(new BtnUI());
                button.setPreferredSize(new Dimension(65, 21));
                btn[i] = button;
                btn[i].setBorder(BorderFactory.createLineBorder(Color.PINK));
                p14.add(button);
                button.setEnabled(false);
                JLabel label = new JLabel("←");
                l[i] = label;
                label.setForeground(Color.RED);
                label.setVisible(false);
                p15.add(label);
            }
            q3.add(p1);
            q3.add(p2);
            q3.add(p3);
            q3.add(Box.createHorizontalStrut(10));
            q3.add(p4);
            q3.add(p5);
            q3.add(p6);
            q3.add(Box.createHorizontalStrut(10));
            q3.add(p7);
            q3.add(p8);
            q3.add(p9);
            q3.add(Box.createHorizontalStrut(10));
            q3.add(p10);
            q3.add(p11);
            q3.add(p12);
            q3.add(Box.createHorizontalStrut(10));
            q3.add(p13);
            q3.add(p14);
            q3.add(p15);
            panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
            panel.add(q3);
            Container cp = getContentPane();
            cp.setBackground(Color.GRAY);
            cp.setLayout(new BorderLayout());
            cp.add(pane, BorderLayout.NORTH);
            cp.add(panel, BorderLayout.CENTER);
            setSize(520, 340);
            JSplitPane split;
            split = new JSplitPane(JSplitPane.VERTICAL_SPLIT, pane, panel);
            pane.setBorder(BorderFactory.createLineBorder(Color.PINK));
            panel.setBorder(BorderFactory.createLineBorder(Color.PINK));
            cp.add(split);
            split.setDividerLocation(0.8);
            split.setOneTouchExpandable(true);
            split.setDividerSize(8);
            l[0].setVisible(true);
            for (int i = 0; i < 30; i++) {
                Random r = new Random();
                int t = r.nextInt(999);
                add(t);        }    }    private class BtnUI extends BasicButtonUI {
            protected void paintText(Graphics g, JComponent c, Rectangle textRect, String text) {
                AbstractButton b = (AbstractButton) c;
                ButtonModel model = b.getModel();
                FontMetrics fm = SwingUtilities2.getFontMetrics(c, g);
                int mnemonicIndex = b.getDisplayedMnemonicIndex();            /* Draw the Text */
                if (model.isEnabled()) {
                    /** * paint the text normally */
                    g.setColor(b.getForeground());
                    SwingUtilities2.drawStringUnderlineCharAt(c, g, text, mnemonicIndex, textRect.x
                        + getTextShiftOffset(), textRect.y + fm.getAscent() + getTextShiftOffset());
                } else {
                    /** * paint the text disabled ** */
                    g.setColor(b.getForeground().brighter());
                    SwingUtilities2.drawStringUnderlineCharAt(
                        c,
                        g,
                        text,
                        mnemonicIndex,
                        textRect.x,
                        textRect.y + fm.getAscent());
                    g.setColor(b.getForeground().darker());
                    SwingUtilities2.drawStringUnderlineCharAt(
                        c,
                        g,
                        text,
                        mnemonicIndex,
                        textRect.x - 1,
                        textRect.y + fm.getAscent() - 1);
                }
            }
        }    public void add(int key) {
            int h = key % n;
            if (btn[h].getLabel().equals(null)) {
                btn[h].setLabel(Integer.toString(key));
            } else {
                int a = 1;
                h = (h + a) % n;
                while (!(btn[h].getLabel().equals("")))
                    h = (h + a) % n;
                btn[h].setLabel(String.valueOf(key));
                // btn[n].setForeground(Color.BLUE);
            }
        }
    }
      

  3.   

    import java.util.*;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.applet.*;
    public class NewJApplet extends JApplet implements ActionListener{   
        private JButton btn1,btn2,btn3,btn4,btn5; 
        private JTextField tf;
        private JLabel l1,l2;
        private JPanel pane,panel;
        private JPanel p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12,p13,p14,p15;
        private JButton[]btn = new JButton[60];
        private JLabel []la = new JLabel[60];
        private JLabel []l = new JLabel[60];
        Box q1=Box.createHorizontalBox();
        Box q2=Box.createHorizontalBox();
        Box w1=Box.createVerticalBox();
        Box q3=Box.createHorizontalBox();
        private static int c1=0,c2=0,c3=0,c4=0,c5=0;
        private int i=0,h=0,n=60;
        public void init(){
           pane = new JPanel(); 
           btn1 = new JButton("新建");
           btn2 = new JButton("添加");
           btn3 = new JButton("插入");
           btn4 = new JButton("删除");
           btn5 = new JButton("查找");
           btn1.addActionListener(this);
           btn2.addActionListener(this);
           btn3.addActionListener(this);
           btn4.addActionListener(this);
           btn5.addActionListener(this);
           l1 = new JLabel("输入整数"); 
           l1.setFont(new Font("Dialog",Font.BOLD,14)); 
           tf = new JTextField(6);
           tf.setBorder(BorderFactory.createLineBorder(Color.PINK));
           q1.add(btn1); 
           q1.add(Box.createHorizontalStrut(10));
           q1.add(btn2); 
           q1.add(Box.createHorizontalStrut(10));
           q1.add(btn3); 
           q1.add(Box.createHorizontalStrut(10));
           q1.add(btn4); 
           q1.add(Box.createHorizontalStrut(10));
           q1.add(btn5);
           q1.add(Box.createHorizontalStrut(10));
           q1.add(l1); 
           q1.add(Box.createHorizontalStrut(10));
           q1.add(tf);
           l2 = new JLabel("Press any button",JLabel.CENTER);
           q2.add(l2);
           w1.add(q1);
           w1.add(Box.createVerticalStrut(10));
           w1.add(q2);
           pane=new JPanel();
           pane.add(w1);  
            p1 = new JPanel();
            p2 = new JPanel();
            p3 = new JPanel();
            p4 = new JPanel();
            p5 = new JPanel(); 
            p6 = new JPanel(); 
            p7 = new JPanel();
            p8 = new JPanel();
            p9 = new JPanel();
            p10 = new JPanel();
            p11 = new JPanel(); 
            p12 = new JPanel(); 
            p13 = new JPanel();
            p14 = new JPanel(); 
            p15 = new JPanel();
           p1.setLayout(new GridLayout(12,0));
           p2.setLayout(new GridLayout(12,0));
           p3.setLayout(new GridLayout(12,0));
           p4.setLayout(new GridLayout(12,0));
           p5.setLayout(new GridLayout(12,0));
           p6.setLayout(new GridLayout(12,0));
           p7.setLayout(new GridLayout(12,0));
           p8.setLayout(new GridLayout(12,0));
           p9.setLayout(new GridLayout(12,0));
           p10.setLayout(new GridLayout(12,0));
           p11.setLayout(new GridLayout(12,0));
           p12.setLayout(new GridLayout(12,0));
           p13.setLayout(new GridLayout(12,0));
           p14.setLayout(new GridLayout(12,0));
           p15.setLayout(new GridLayout(12,0));
           for(int i=0;i<12;i++)
           {  
              JLabel labe = new JLabel(""+i);
              la[i]=labe;
              p1.add(labe);
              JButton button = new JButton();
              button.setPreferredSize(new Dimension(65,21));
              btn[i]=button;
              btn[i].setBorder(BorderFactory.createLineBorder(Color.PINK));
              p2.add(button);
              button.setEnabled(false);
              JLabel label = new JLabel("←");
              l[i]=label;
              label.setForeground(Color.RED);
              label.setVisible(false);
              p3.add(label);
           }
           for(int i=12;i<24;i++)
           {  
              JLabel labe = new JLabel(""+i);
              la[i]=labe;
              p4.add(labe);
              JButton button = new JButton();
              button.setPreferredSize(new Dimension(65,21));
              btn[i]=button;
              btn[i].setBorder(BorderFactory.createLineBorder(Color.PINK));
              p5.add(button);
              button.setEnabled(false);
              JLabel label = new JLabel("←");
              l[i]=label;
              label.setForeground(Color.RED);
              label.setVisible(false);
              p6.add(label);
           }
           for(int i=24;i<36;i++)
           {  
              JLabel labe = new JLabel(""+i);
              la[i]=labe;
              p7.add(labe);
              JButton button = new JButton();
              button.setPreferredSize(new Dimension(65,21));
              btn[i]=button;
              btn[i].setBorder(BorderFactory.createLineBorder(Color.PINK));
              p8.add(button);
              button.setEnabled(false);
              JLabel label = new JLabel("←");
              l[i]=label;
              label.setForeground(Color.RED);
              label.setVisible(false);
              p9.add(label);
           }
           for(int i=36;i<48;i++)
           {  
              JLabel labe = new JLabel(""+i);
              la[i]=labe;
              p10.add(labe);
              JButton button = new JButton();
              button.setPreferredSize(new Dimension(65,21));
              btn[i]=button;
              btn[i].setBorder(BorderFactory.createLineBorder(Color.PINK));
              p11.add(button);
              button.setEnabled(false);
              JLabel label = new JLabel("←");
              l[i]=label;
              label.setForeground(Color.RED);
              label.setVisible(false);
              p12.add(label);
           }
           for(int i=48;i<60;i++)
           {  
              JLabel labe = new JLabel(""+i);
              la[i]=labe;
              p13.add(labe);
              JButton button = new JButton();
              button.setPreferredSize(new Dimension(65,21));
              btn[i]=button;
              btn[i].setBorder(BorderFactory.createLineBorder(Color.PINK));
              p14.add(button);
              button.setEnabled(false);
              JLabel label = new JLabel("←");
              l[i]=label;
              label.setForeground(Color.RED);
              label.setVisible(false);
              p15.add(label);
           }
           q3.add(p1);q3.add(p2);q3.add(p3);q3.add(Box.createHorizontalStrut(10));
           q3.add(p4);q3.add(p5);q3.add(p6);q3.add(Box.createHorizontalStrut(10));
           q3.add(p7);q3.add(p8);q3.add(p9);q3.add(Box.createHorizontalStrut(10));
           q3.add(p10);q3.add(p11);q3.add(p12);q3.add(Box.createHorizontalStrut(10));
           q3.add(p13);q3.add(p14);q3.add(p15);
           panel=new JPanel(new FlowLayout(FlowLayout.LEFT));
           panel.add(q3);
           Container cp=getContentPane(); 
           cp.setBackground(Color.GRAY);
           cp.setLayout(new BorderLayout()); 
           cp.add(pane,BorderLayout.NORTH);  
           cp.add(panel,BorderLayout.CENTER);
           setSize(520,340);
           JSplitPane split;
           split=new JSplitPane(JSplitPane.VERTICAL_SPLIT, pane,panel);
           pane.setBorder(BorderFactory.createLineBorder(Color.PINK));
           panel.setBorder(BorderFactory.createLineBorder(Color.PINK));
           cp.add(split);
           split.setDividerLocation(0.8);
           split.setOneTouchExpandable(true);
           split.setDividerSize(8);
           l[0].setVisible(true);        for(int i=0;i<30;i++)
           {
             Random r=new Random();
             int t=r.nextInt(999);
             add(t);     
           } 
        }
        public void add(int key) {  
        Random f=new Random();
        int R=f.nextInt(255);
        int G=f.nextInt(255);
        int B=f.nextInt(255);
        int  h=key%n;
        if(btn[h].getLabel().equals(null))
        {   
           btn[h].setLabel(Integer.toString(key));
        }
        else{
        int a=1;
        h=(h+a)%n;
        while(!(btn[h].getLabel().equals("")))
        h=(h+a)%n;
        btn[h].setLabel(String.valueOf(key));
        btn[h].setBackground(new Color(R,G,B));
            }    
        }
      

  4.   

    public void  新建()
        {  
           c1++;
        if(c1==1)
            l2.setText("Enter size of array to create");
        if(c1==2){
                if(tf.getText().equals("")||(Integer.parseInt(tf.getText())>60)||(Integer.parseInt(tf.getText())<0))
                {
                l2.setText("ERROR:use size between 0 and 60");
                c1=0;
                }
               else 
                 l2.setText("Will create empty array with "+tf.getText()+" cells");
                 }
        if(c1==3){
            l2.setText("New array created; total item=0");
            n=Integer.parseInt(tf.getText());
            for(int k=0;k<n;k++)
            {
            l[0].setVisible(true);
            btn[k].setVisible(true);
            la[k].setVisible(true);
            btn[k].setLabel("");
            btn[k].setBackground(Color.WHITE);
            }
            for(int k=n;k<60;k++)
            {
            l[k].setVisible(false);
            btn[k].setVisible(false);
            la[k].setVisible(false);
            }      
         }
     if(c1==4)
       {
        l2.setText("Press any button");
        c1=0;
        }    
      }
        public void 添加()
        {    
           c2++;
        if(c2==1)
            l2.setText("Enter number of item to fill in");  
        if(c2==2)
        {    
            int y=0,b=0;        
             while(btn[y].getLabel()=="")
             {  
                 y++;   b++;}
            if(tf.getText().equals("")||(Integer.parseInt(tf.getText())>b)||(Integer.parseInt(tf.getText())<0))
            {
                l2.setText("ERROR: fill between 0 and "+ b +" items");
                c2=0;
            }
             else 
                 l2.setText("Will fill in "+tf.getText()+" items");  
           
        }
        if(c2==3)
        {
        l2.setText("Fill completed; total items= "+tf.getText());
        int t=Integer.parseInt(tf.getText());
        for(int i=0;i<t;i++){
        Random ran=new Random();
        int r=ran.nextInt(999);
        add(r);  
        }
        }       
        if(c2==4)
        {    
        l2.setText("Press any button");
        c2=0;
         for(int i=0;i<n;i++)
         {btn[i].setLabel(""); 
         btn[i].setBackground(Color.WHITE);}
        }
     }
        public void 插入()
        {
           c3++;
       int k=0,j=0,q=0;
      if(c3==1){
            l2.setText("Enter key of item to insert");i=0;
               }
        if(c3==2){
            for(j=0;j<n;j++)
            if((!(btn[j].getLabel().equals("")))&&(!(btn[j].getLabel().equals("*Del*"))))
                k++;
            if(k==n){
                l2.setText("CANNT INSERT: array is full");
                c3=0;
            }
            else  if((tf.getText().equals(""))||(Integer.parseInt(tf.getText())>999)||(Integer.parseInt(tf.getText())<0)){
                l2.setText("CANNNT INSERT: need key between 0 and 999");
                c3=0;
            }
             else 
                 l2.setText("Will insert item with key  "+tf.getText());              
        }
    if(c3==3){ 
        i++;
    int hashMax=n; int key=Integer.parseInt(tf.getText());
    if(i==1)
         h=key % hashMax;
    if(btn[h].getLabel().equals("")||btn[h].getLabel().equals("*Del*")){
       btn[h].setLabel(String.valueOf(key));
       for(q=0;q<n;q++)                                                                                                                                                
           l[q].setVisible(false);
       l[0].setVisible(true);
       l2.setText("Inserted item with key  "+key+"  at index "+h);
       l[0].setVisible(false);
       l[h].setVisible(true);
    }
    else{
        c3--;
        if(i==1){
             for(q=0;q<n;q++)
                l[q].setVisible(false);
           l2.setText("Cell  "+key+"  occupied; goning to next cell");
           l[h].setVisible(true);
        }
        else{
            for(q=0;q<n;q++)
                l[q].setVisible(false);
            l2.setText("Searching for unoccupied cell; step was  "+(i-1));
            l[h].setVisible(true);
        }
        int a=1;
        h=(h+a)%hashMax;  
    }
    }
    if(c3==4){
        int r=0;
    for(i=0;i<n;i++)
        if((!(btn[i].getLabel().equals("")))&&(!(btn[i].getLabel().equals("*Del*"))))
            r++;
        l2.setText("Insert completed; total item = "+r);
        for(q=0;q<n;q++)
                l[q].setVisible(false);
        l[0].setVisible(true);
        c3=0;
    }
    }
        public void 删除()
        {   
           c4++;int q=0;
        if(c4==1){
            l2.setText("Enter key of item to delete");i=0;
        }
        if(c4==2){
            if(tf.getText().equals("")){
                l2.setText("ERROR: use key between 0 and 999");
                c4=0;
            }
             else {
                 int key=Integer.parseInt(tf.getText());
                 h=key%n;
                  for(q=0;q<n;q++)
                l[q].setVisible(false);
                 l2.setText("Looking for item with key  "+tf.getText()+"  at index  "+h);
                 l[h].setVisible(true);
             }
        }
     if(c4==3){
         i++;
         if(btn[h].getLabel().equals(tf.getText())){
              for(q=0;q<n;q++)
                l[q].setVisible(false);
              l[h].setVisible(true);
         l2.setText("Have found item with key  "+tf.getText()+"  at index  "+h);
         }
         else if(btn[h].getLabel().equals("")){
              for(q=0;q<n;q++)
                l[q].setVisible(false);
              l[h].setVisible(true);
             l2.setText("Cannt locate item with key  "+tf.getText());
             for(q=0;q<n;q++)
                l[q].setVisible(false);
              l[0].setVisible(true);
             c4=0;
         }
         else{
             c4--;
           if(i==1)
               l2.setText("No match: will start probe");         
           else{
                for(q=0;q<n;q++)
                l[q].setVisible(false);
               l2.setText("Checking next cell; step was  "+(i-1));
            l[h].setVisible(true);
            if(i==n){           
             l2.setText("Cannt locate item with key  "+tf.getText());         
            }
            if(i==(n+1)){
             for(q=0;q<n;q++)
                l[q].setVisible(false);
                l[0].setVisible(true);
              l2.setText("Press any Button");
              c4=0;
            }
           }
             int a=1;
             h=(h+a)%n;
         } 
     }
     if(c4==4){
          btn[h].setLabel("*Del*");
         int r=0;
    for(i=0;i<n;i++)
        if((!(btn[i].getLabel().equals("")))&&(!(btn[i].getLabel().equals("*Del*"))))
            r++;      
        l2.setText("Deleted item with key  "+tf.getText()+" ; total item = "+r);   
     }
     if(c4==5){
     l2.setText("Press any button");
      for(q=0;q<n;q++)
            l[q].setVisible(false);
     l[0].setVisible(true);
     c4=0;
     }
    }
      

  5.   

      public void 查找()
        {
           c5++;
           if(c5==1)
            {
              l2.setText("Enter key of item to find");
            }
           if(c5==2)
           {
                if(tf.getText().equals("")||(Integer.parseInt(tf.getText())>999)||(Integer.parseInt(tf.getText())<0))
                {
                l2.setText("ERROR: use key between 0 and 999");
                c5=0;
                }
             else 
             { c5++; }
           }
     if(c5==3){
            int key=Integer.parseInt(tf.getText());
            h=(key+1)%n;
         if(btn[h].getLabel().equals(tf.getText()))
         {          
              l[0].setVisible(false);
              l[h].setVisible(true);
              l2.setText("Have found item with key  "+tf.getText()+"  at index  "+h); 
         }
         else 
         {    
              l[0].setVisible(false);
              l[h].setVisible(true);
              l2.setText("Cannot find item with key  "+tf.getText()+"  at index  "+h); 
         }
     }
     if(c5==4){    
        l2.setText("Press any button"); 
        l[h].setVisible(false);
        l[0].setVisible(true);
        c5=0;
     }
    }   
        public void actionPerformed(ActionEvent e) {
            if(e.getSource()==btn1)
            {   新建();  }     
             if(e.getSource()==btn2)
            {   添加();  }
            if(e.getSource()==btn3)
            {   插入();  }
            if(e.getSource()==btn4)
            {   删除();  }
            if(e.getSource()==btn5)
            {   查找();  }       
        }
    }我想让每次有数的按钮前景色显示成白色 不是明显好看嘛 直接用setForeground怎么不行啊