import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.plaf.basic.*;
import sun.swing.*;
public class NewJApplet extends JApplet implements ActionListener {     
    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(); 
            if (model.isEnabled()) { 
                g.setColor(b.getForeground()); 
                SwingUtilities2.drawStringUnderlineCharAt(c, g, text, mnemonicIndex, textRect.x 
                    + getTextShiftOffset(), textRect.y + fm.getAscent() + getTextShiftOffset()); 
            }
            else { 
                 g.setColor(b.getForeground().brighter()); 
                 SwingUtilities2.drawStringUnderlineCharAt( 
                    c, 
                    g, 
                    text, 
                    mnemonicIndex, 
                    textRect.x, 
                    textRect.y + fm.getAscent()); 
                g.setColor(b.getForeground().WHITE); 
                SwingUtilities2.drawStringUnderlineCharAt( 
                    c, 
                    g, 
                    text, 
                    mnemonicIndex, 
                    textRect.x - 1, 
                    textRect.y + fm.getAscent() - 1); 
            } 
        } 
   }    
    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("New");
       btn1.setBackground(Color.WHITE);
       btn2 = new JButton("Fill");
       btn2.setBackground(Color.WHITE);
       btn3 = new JButton("Ins");
       btn3.setBackground(Color.WHITE);
       btn4 = new JButton("Del");
       btn4.setBackground(Color.WHITE);
       btn5 = new JButton("Find");
       btn5.setBackground(Color.WHITE);
       btn1.addActionListener(this);
       btn2.addActionListener(this);
       btn3.addActionListener(this);
       btn4.addActionListener(this);
       btn5.addActionListener(this);
       l1 = new JLabel("Enter number"); 
       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);
       l2.setFont(new Font("Dialog",Font.BOLD,14)); 
       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.setUI(new BtnUI()); 
          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);
       }

解决方案 »

  1.   

    for(int i=12;i<24;i++)
           {  
              JLabel labe = new JLabel(""+i);
              la[i]=labe;
              p4.add(labe);
              JButton button = new JButton();
              button.setUI(new BtnUI()); 
              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.setUI(new BtnUI()); 
              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.setUI(new BtnUI()); 
              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.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 ra=new Random();
             int t=ra.nextInt(999);
             hash(t);     
           } 
        }
        public void hash(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].getText().equals(null))
        { 
          btn[h].setText(Integer.toString(key));    
        }
            else{
        int a=1;
        h=(h+a)%n;
        while(!(btn[h].getText().equals("")))
        h=(h+a)%n;
        btn[h].setText(String.valueOf(key));
        btn[h].setBackground(new Color(R,G,B));
            }    
        }
        public void  New()
        {  
           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 Fill()
        {    
           c2++;
           if(c2==0)
               l2.setText("Press any button");  
        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");  
           
        }
      

  2.   

    if(c2==3)
        {
        l2.setText("Fill completed; total items= "+tf.getText());
         for(int i=0;i<Integer.parseInt(tf.getText());i++)
           {
             Random ra=new Random();
             int t=ra.nextInt(999);
             hash(t);     
           } 
        }       
        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 Ins()
        {
        Random f=new Random();
        int R=f.nextInt(255);
        int G=f.nextInt(255);
        int B=f.nextInt(255);
           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("CANNOT INSERT: need key between 0 and 999");
                c3=0;
            }
             else 
                 l2.setText("Will insert item with key  "+tf.getText());              
        }
    if(c3==3){ 
        i++;
    int key=Integer.parseInt(tf.getText());
    if(i==1)
         h=key % n;
    if(btn[h].getLabel().equals("")||btn[h].getLabel().equals("*Del*")){
       btn[h].setLabel(String.valueOf(key));
       btn[h].setBackground(new Color(R,G,B));
       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)%n;  
    }
    }
    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 Del()
        {   
           c4++;int q=0;
        if(c4==1){
            l2.setText("Enter key of item to delete");i=0;
        }
        if(c4==2){
            if(tf.getText().equals("")||(Integer.parseInt(tf.getText())>999)||(Integer.parseInt(tf.getText())<0)){
                l2.setText("ERROR: use key between 0 and 999");
                c4=0;
            }
             else {
                 int key=Integer.parseInt(tf.getText());
                 h=(key+1)%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("Cannot 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("Cannot 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;
     }
    }
     public void Find()
        {
           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{}
           }
     if(c5==3){
      int key=Integer.parseInt(tf.getText());
            int s=1;
            h=(key+s)%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)
            {   New();  }     
             if(e.getSource()==btn2)
            {   Fill();  }
            if(e.getSource()==btn3)
            {   Ins();  }
            if(e.getSource()==btn4)
            {   Del();  }
             if(e.getSource()==btn5)
            {   Find();  }       
        }
     }