⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 randomcharacters.java

📁 java经典的源代码 我非常喜欢这个源代码 对于编程很有好处
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class RandomCharacters extends JApplet implements ActionListener {
  private String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  private final static int SIZE = 3;
  private JLabel outputs[];
  private JCheckBox checkboxes[];
  private Thread threads[];
  private boolean suspended[];
  public void init(){
    outputs = new JLabel[ SIZE ];
    checkboxes = new JCheckBox[ SIZE ];
    threads = new Thread[ SIZE ];
    suspended = new boolean[ SIZE ];
    Container container = getContentPane();
    container.setLayout( new GridLayout( SIZE, 2, 5, 5 ) );
    for ( int count = 0; count < SIZE; count++ ) {
      outputs[ count ] = new JLabel();
      outputs[ count ].setBackground( Color.green );
      outputs[ count ].setOpaque( true );
      container.add( outputs[ count ] );
      checkboxes[ count ] = new JCheckBox( "Suspended" );
      checkboxes[ count ].addActionListener( this );
      container.add( checkboxes[ count ] );
    }
  }
  public void start(){
    for ( int count = 0; count < threads.length; count++ ) {
      threads[ count ] = new Thread( new RunnableObject(),"Thread " + ( count + 1 ) );
      threads[ count ].start();
    }
 }
 private int getIndex( Thread current ) {
   for ( int count = 0; count < threads.length; count++ )
     if ( current == threads[ count ] )
       return count;
   return -1;
 }
 public synchronized void stop(){
   for ( int count = 0; count < threads.length; count++ )
     threads[ count ] = null;
   notifyAll();
 }
 public synchronized void actionPerformed( ActionEvent event ){
   for ( int count = 0; count < checkboxes.length; count++ ) {
     if ( event.getSource() == checkboxes[ count ] ) {
       suspended[ count ] = !suspended[ count ];
       outputs[ count ].setBackground( !suspended[ count ] ? Color.green : Color.red );
       if ( !suspended[ count ] )
         notifyAll();
       return;
     }
   }
 }
 private class RunnableObject implements Runnable {
   public void run(){
     final Thread currentThread = Thread.currentThread();
     final int index = getIndex( currentThread );
     while ( threads[ index ] == currentThread ) {
       try {
         Thread.sleep( ( int ) ( Math.random() * 1000 ) );
         synchronized( RandomCharacters.this ) {
           while ( suspended[ index ] &&threads[ index ] == currentThread ) {
             RandomCharacters.this.wait();
           }
         } // end synchronized block
       }
       catch ( InterruptedException interruptedException ) {
         System.err.println( "sleep interrupted" );
       }
       SwingUtilities.invokeLater(new Runnable() {
         public void run()
         { // pick random character
           char displayChar = alphabet.charAt(( int ) ( Math.random() * 26 ) );
           outputs[ index ].setText(currentThread.getName() + ": " + displayChar );
         }
       }) ; // end call to SwingUtilities.invokeLater
     } // end while
     System.err.println( currentThread.getName() + " terminating" );
   }
 }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -