exercise19_11.java

来自「java程序设计 机械工业出版社 书籍代码」· Java 代码 · 共 74 行

JAVA
74
字号
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;

public class Exercise19_11 extends JApplet implements Runnable {
  boolean isStandalone = false;
  JProgressBar jProgressBar1 = new JProgressBar();
  FlowLayout flowLayout1 = new FlowLayout();

  Thread thread;

  /**Construct the applet*/
  public Exercise19_11() {
  }

  /**Initialize the applet*/
  public void init() {
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }

  //Component initialization
  private void jbInit() throws Exception {
    this.setSize(new Dimension(400,300));
    this.getContentPane().setLayout(flowLayout1);
    this.getContentPane().add(jProgressBar1, null);

    thread = new Thread(this);
    thread.start();
  }

  public void run() {
    while (true) {
      try {
        Thread.sleep(500);
      }
      catch (InterruptedException ex) { }

      jProgressBar1.setValue((int)(jProgressBar1.getMaximum()*
        Math.random()));
    }
  }

  /**Main method*/
  public static void main(String[] args) {
    Exercise19_11 applet = new Exercise19_11();
    applet.isStandalone = true;
    JFrame frame = new JFrame();
    frame.setTitle("Exercise19_11");
    frame.getContentPane().add(applet, BorderLayout.CENTER);
    applet.init();
    applet.start();
    frame.setSize(400,320);
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
    frame.setVisible(true);
  }

  // static initializer for setting look & feel
  static {
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
      //UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
    }
    catch (Exception e) {}
  }
}

⌨️ 快捷键说明

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