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

📄 complete15_3.java

📁 北京大学出版社的
💻 JAVA
字号:
package questions.c15;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
public class Complete15_3 extends JPanel {
   private String lowerLimit = "0";
   private String upperLimit = "100";
   private String position = "0";
   private static final int LENGTH = 200;
   private static final int WIDTH = 20;
   private static final int VSPACE = 20;
   private static final int HSPACE = 10;
   private static final int PROG_INSET = 2;
   private PropertyChangeSupport listeners
      = new PropertyChangeSupport( this );
   public void paintComponent( Graphics g ) {
      super.paintComponent( g );
      // get the font metrics for the graphics context
      FontMetrics fm = g.getFontMetrics();      
      // get the height of the font in use
      int fontHeight = fm.getHeight();
      // put the start label on the progress bar
      g.drawString( lowerLimit, HSPACE, VSPACE );
      // put the end label on the progress bar so that
      // the end of the text lines up with the end of
      // the progress bar
      int upperLen = fm.stringWidth( upperLimit );
      g.drawString( upperLimit,
                    HSPACE + LENGTH - upperLen,
                    VSPACE );
      // draw the outline of the progress bar
      g.drawRect( HSPACE, VSPACE + fontHeight,
                  LENGTH, WIDTH );
      // fill in the appropriate amount of the bar
      // based upon the current position
      int ul = Integer.parseInt( upperLimit );
      int ll = Integer.parseInt( lowerLimit );
      int pos = Integer.parseInt( position );
      double progress = (double)(pos - ll) / (double)(ul - ll);
      int progressLength = (int) (progress * LENGTH);
      g.setColor( Color.red );
      g.fillRect( HSPACE + PROG_INSET,
                  VSPACE + fontHeight + PROG_INSET,
                  progressLength - PROG_INSET*2,
                  WIDTH - PROG_INSET*2 );
   }
   public Dimension getPreferredSize() {
      // determine the font height for the font in use
      int fontHeight = getFontMetrics(getFont()).getHeight();
      // set the size so that the progress bar, its
      // labels and the spacing between them and around
      // them is taken into account
      return new Dimension( LENGTH + 2*HSPACE,
                            HEIGHT + 3*VSPACE
                                   + fontHeight ); 
   }
   // add the method needed to register listeners
   public void addPropertyChangeListener(
               PropertyChangeListener l ) {
      listeners.addPropertyChangeListener( l );
   }
   // add the method needed to remove listeners
   public void removePropertyChangeListener(
               PropertyChangeListener l ) {
      listeners.removePropertyChangeListener( l );
   }
}

⌨️ 快捷键说明

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