Java by API/javax.swing/JProgressBar

Материал из Java эксперт
Перейти к: навигация, поиск

JProgressBar: addChangeListener(ChangeListener l)

  
import javax.swing.JProgressBar;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Main {
  public static void main(String[] argv) throws Exception {
    int minimum = 0;
    int maximum = 100;
    JProgressBar progress = new JProgressBar(minimum, maximum);
    progress.addChangeListener(new ChangeListener() {
      public void stateChanged(ChangeEvent evt) {
        JProgressBar comp = (JProgressBar) evt.getSource();
        int value = comp.getValue();
        int min = comp.getMinimum();
        int max = comp.getMaximum();
      }
    });
  }
}





JProgressBar: getMaximum()

  

import javax.swing.JProgressBar;
public class Main {
  public static void main(String[] argv) throws Exception {
    int minimum = 0;
    int maximum = 100;
    JProgressBar progress = new JProgressBar(minimum, maximum);
    // Get the current value
    int value = progress.getValue();
    // Get the minimum value
    int min = progress.getMinimum();
    // Get the maximum value
    int max = progress.getMaximum();
    // Change the minimum value
    int newMin = 0;
    progress.setMinimum(newMin);
    // Change the maximum value
    int newMax = 256;
    progress.setMaximum(newMax);
    // Set the value; the new value will be forced into the bar"s range
    int newValue = 33;
    progress.setValue(newValue);
  }
}





JProgressBar: getMinimum()

  

import javax.swing.JProgressBar;
public class Main {
  public static void main(String[] argv) throws Exception {
    int minimum = 0;
    int maximum = 100;
    JProgressBar progress = new JProgressBar(minimum, maximum);
    // Get the current value
    int value = progress.getValue();
    // Get the minimum value
    int min = progress.getMinimum();
    // Get the maximum value
    int max = progress.getMaximum();
    // Change the minimum value
    int newMin = 0;
    progress.setMinimum(newMin);
    // Change the maximum value
    int newMax = 256;
    progress.setMaximum(newMax);
    // Set the value; the new value will be forced into the bar"s range
    int newValue = 33;
    progress.setValue(newValue);
  }
}





JProgressBar: getValue()

  

import javax.swing.JProgressBar;
public class Main {
  public static void main(String[] argv) throws Exception {
    int minimum = 0;
    int maximum = 100;
    JProgressBar progress = new JProgressBar(minimum, maximum);
    // Get the current value
    int value = progress.getValue();
    // Get the minimum value
    int min = progress.getMinimum();
    // Get the maximum value
    int max = progress.getMaximum();
    // Change the minimum value
    int newMin = 0;
    progress.setMinimum(newMin);
    // Change the maximum value
    int newMax = 256;
    progress.setMaximum(newMax);
    // Set the value; the new value will be forced into the bar"s range
    int newValue = 33;
    progress.setValue(newValue);
  }
}





JProgressBar: setBorder(Border b)

 
import java.awt.BorderLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.border.Border;
public class MainClass {
  public static void main(String args[]) {
    JFrame f = new JFrame("JProgressBar Sample");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JProgressBar progressBar = new JProgressBar();
    progressBar.setValue(25);
    progressBar.setStringPainted(true);
    Border border = BorderFactory.createTitledBorder("Reading...");
    progressBar.setBorder(border);
    f.add(progressBar, BorderLayout.NORTH);
    f.setSize(300, 100);
    f.setVisible(true);
  }
}





JProgressBar: setBorderPainted(boolean b)

 
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
public class MainClass {
  public static void main(String args[]) throws Exception {
    JFrame frame = new JFrame("ProgressBars");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JProgressBar dJProgressBar = new JProgressBar(JProgressBar.VERTICAL);
    dJProgressBar.setValue(100);
    dJProgressBar.setBorderPainted(false);
    dJProgressBar.setString("Ack");
    dJProgressBar.setStringPainted(true);
    frame.add(dJProgressBar, BorderLayout.WEST);
    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}





JProgressBar: setIndeterminate(boolean newValue)

 
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
public class MainClass {
  public static void main(String args[]) throws Exception {
    JFrame frame = new JFrame("Indeterminate");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JProgressBar aJProgressBar = new JProgressBar();
    aJProgressBar.setIndeterminate(true);
    frame.add(aJProgressBar, BorderLayout.NORTH);
    frame.setSize(300, 100);
    frame.setVisible(true);
  }
}





JProgressBar: setMaximum(int n)

 
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
public class MainClass extends JPanel {
  JProgressBar pbar = new JProgressBar();
  static final int MY_MINIMUM = 0;
  static final int MY_MAXIMUM = 100;
  public MainClass() {
    super(true);
    pbar.setMinimum(MY_MINIMUM);
    pbar.setMaximum(MY_MAXIMUM);
    add(pbar);
  }
  public void updateBar(int newValue) {
    pbar.setValue(newValue);
  }
  public static void main(String args[]) {
    final MainClass it = new MainClass();
    JFrame frame = new JFrame("Progress Bar Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(it);
    frame.pack();
    frame.setVisible(true);
    for (int i = MY_MINIMUM; i <= MY_MAXIMUM; i++) {
      final int percent = i;
      try {
        SwingUtilities.invokeAndWait(new Runnable() {
          public void run() {
            it.updateBar(percent);
          }
        });
        java.lang.Thread.sleep(100);
      } catch (Exception e) {
        ;
      }
    }
  }
}





JProgressBar: setMinimum(int n)

 
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
public class MainClass extends JPanel {
  JProgressBar pbar = new JProgressBar();
  static final int MY_MINIMUM = 0;
  static final int MY_MAXIMUM = 100;
  public MainClass() {
    super(true);
    pbar.setMinimum(MY_MINIMUM);
    pbar.setMaximum(MY_MAXIMUM);
    add(pbar);
  }
  public void updateBar(int newValue) {
    pbar.setValue(newValue);
  }
  public static void main(String args[]) {
    final MainClass it = new MainClass();
    JFrame frame = new JFrame("Progress Bar Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(it);
    frame.pack();
    frame.setVisible(true);
    for (int i = MY_MINIMUM; i <= MY_MAXIMUM; i++) {
      final int percent = i;
      try {
        SwingUtilities.invokeAndWait(new Runnable() {
          public void run() {
            it.updateBar(percent);
          }
        });
        java.lang.Thread.sleep(100);
      } catch (Exception e) {
        ;
      }
    }
  }
}





JProgressBar: setStringPainted(boolean b)

 
import java.awt.BorderLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.border.Border;
public class MainClass {
  public static void main(String args[]) {
    JFrame f = new JFrame("JProgressBar Sample");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JProgressBar progressBar = new JProgressBar();
    progressBar.setValue(25);
    progressBar.setStringPainted(true);
    Border border = BorderFactory.createTitledBorder("Reading...");
    progressBar.setBorder(border);
    f.add(progressBar, BorderLayout.NORTH);
    f.setSize(300, 100);
    f.setVisible(true);
  }
}





JProgressBar: setValue(int v)

 
import java.awt.BorderLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.border.Border;
public class MainClass {
  public static void main(String args[]) {
    JFrame f = new JFrame("JProgressBar Sample");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JProgressBar progressBar = new JProgressBar();
    progressBar.setValue(25);
    progressBar.setStringPainted(true);
    Border border = BorderFactory.createTitledBorder("Reading...");
    progressBar.setBorder(border);
    f.add(progressBar, BorderLayout.NORTH);
    f.setSize(300, 100);
    f.setVisible(true);
  }
}





JProgressBar.VERTICAL

 
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
public class MainClass {
  public static void main(String args[]) throws Exception {
    JFrame frame = new JFrame("ProgressBars");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JProgressBar dJProgressBar = new JProgressBar(JProgressBar.VERTICAL);
    dJProgressBar.setValue(100);
    dJProgressBar.setBorderPainted(false);
    dJProgressBar.setString("Ack");
    dJProgressBar.setStringPainted(true);
    frame.add(dJProgressBar, BorderLayout.WEST);
    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}





new JProgressBar(int min, int max)

  
import javax.swing.JProgressBar;
public class Main {
  public static void main(String[] argv) throws Exception {
    int minimum = 0;
    int maximum = 100;
    JProgressBar progress = new JProgressBar(minimum, maximum);
  }
}





new JProgressBar(int orient, int min, int max)

  

import javax.swing.JProgressBar;
public class Main {
  public static void main(String[] argv) throws Exception {
    int minimum = 0;
    int maximum = 100;
    JProgressBar progress = new JProgressBar(JProgressBar.VERTICAL, minimum, maximum);
  }
}