Java by API/javax.swing/JProgressBar

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

JProgressBar: addChangeListener(ChangeListener l)

   <source lang="java">
 

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();
     }
   });
 }

}


 </source>
   
  
 
  



JProgressBar: getMaximum()

   <source lang="java">
 

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);
 }

}


 </source>
   
  
 
  



JProgressBar: getMinimum()

   <source lang="java">
 

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);
 }

}


 </source>
   
  
 
  



JProgressBar: getValue()

   <source lang="java">
 

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);
 }

}


 </source>
   
  
 
  



JProgressBar: setBorder(Border b)

   <source lang="java">

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);
 }

}


 </source>
   
  
 
  



JProgressBar: setBorderPainted(boolean b)

   <source lang="java">

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);
 }

}


 </source>
   
  
 
  



JProgressBar: setIndeterminate(boolean newValue)

   <source lang="java">

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);
 }

}


 </source>
   
  
 
  



JProgressBar: setMaximum(int n)

   <source lang="java">

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) {
       ;
     }
   }
 }

}


 </source>
   
  
 
  



JProgressBar: setMinimum(int n)

   <source lang="java">

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) {
       ;
     }
   }
 }

}


 </source>
   
  
 
  



JProgressBar: setStringPainted(boolean b)

   <source lang="java">

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);
 }

}


 </source>
   
  
 
  



JProgressBar: setValue(int v)

   <source lang="java">

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);
 }

}


 </source>
   
  
 
  



JProgressBar.VERTICAL

   <source lang="java">

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);
 }

}


 </source>
   
  
 
  



new JProgressBar(int min, int max)

   <source lang="java">
 

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);
 }

}


 </source>
   
  
 
  



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

   <source lang="java">
 

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);
 }

}


 </source>