Results 1 to 1 of 1
- 10-19-2009, 05:24 AM #1
Member
- Join Date
- Oct 2009
- Posts
- 5
- Rep Power
- 0
How prograssbarColor with differentThe color indicates
hi all
Suppose the progress takes 20% as a color, also is 100% will have 5 kinds of colors(is also be divided into 5 different colors), for example:
Segment 1(0~20%) red, segment 2(20%~40%) is orange, segment 3(40%~60%) green, segment 4(60~80) yellow, segment 5(80~100) blue
Java Code:import java.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; class ProgressBarExample extends JFrame implements ActionListener { private JProgressBar progress; private JProgressBar progress2; private JButton button; private JLabel label1; private JPanel topPanel; public ProgressBarExample() { setTitle( "Progress Bar Application" ); setSize( 310, 130 ); setBackground( Color.gray ); topPanel = new JPanel(); topPanel.setPreferredSize( new Dimension( 310, 130 ) ); getContentPane().add( topPanel ); // Create a label and progress bar label1 = new JLabel( "Waiting to start tasks..." ); label1.setPreferredSize( new Dimension( 280, 24 ) ); topPanel.add( label1 ); progress = new JProgressBar(); progress.setPreferredSize( new Dimension( 300, 20 ) ); progress.setMinimum( 0 ); progress.setMaximum( 100 ); progress.setValue( 0 ); progress.setBounds( 20, 35, 260, 20 ); progress2 = new JProgressBar(); progress2.setPreferredSize( new Dimension( 300, 20 ) ); progress2.setMinimum( 0 ); progress2.setMaximum( 100 ); progress2.setValue( 0 ); progress2.setBounds( 20, 35, 260, 20 ); topPanel.add( progress ); topPanel.add( progress2 ); button = new JButton( "Start" ); topPanel.add( button ); button.addActionListener( this ); } public void actionPerformed( ActionEvent event ) { if( event.getSource() == button ) { // Prevent more button presses button.setEnabled( false ); // Perform all of our bogus tasks for( int iCtr = 1; iCtr < 101; iCtr++ ) { // Do some sort of simulated task DoBogusTask( iCtr ); // Update the progress indicator and label label1.setText( "Performing task " + iCtr + " of 100" ); Rectangle labelRect = label1.getBounds(); labelRect.x = 0; labelRect.y = 0; label1.paintImmediately( labelRect ); progress.setValue( iCtr ); Rectangle progressRect = progress.getBounds(); progressRect.x = 0; progressRect.y = 0; progress.paintImmediately( progressRect ); } } } public void DoBogusTask( int iCtr ) { Random random = new Random( iCtr ); // Waste some time for( int iValue = 0; iValue < random.nextFloat() * 10000; iValue++ ) { System.out.println( "iValue=" + iValue ); } } public static void main( String args[] ) { // Create an instance of the test application ProgressBarExample mainFrame = new ProgressBarExample(); mainFrame.setVisible( true ); mainFrame.pack(); } }
Similar Threads
-
Another Color TabbedPane Example
By Java Tip in forum javax.swingReplies: 0Last Post: 06-26-2008, 07:37 PM -
Color TabbedPane Example
By Java Tip in forum javax.swingReplies: 0Last Post: 06-26-2008, 07:36 PM -
Color Composite
By Java Tip in forum java.awtReplies: 0Last Post: 06-21-2008, 08:47 PM -
Color objects
By CyberFrog in forum New To JavaReplies: 4Last Post: 04-01-2008, 12:41 AM -
A bit of color!
By tim in forum Java 2DReplies: 8Last Post: 02-11-2008, 11:57 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks