?

Log in

Previous Entry | Next Entry

Fixed Code

Well, it's not pretty, but this is how it should look:

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

class Main
{
	public static Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
	public static int screenWidth = (int)screenSize.getWidth();
	public static int screenHeight = (int)screenSize.getHeight();
	public static int width = (int)screenWidth/5; 
	public static int height = (int)screenHeight/12;
	
	public static void main(String [] args)
	{
		//Creates a JFrame
		JFrame frameA = new JFrame("JFrame A");		
		frameA.setResizable(false);
		frameA.setSize(Main.width, Main.height);
		frameA.setLocation((Main.screenWidth - Main.width)/2, (Main.screenHeight - Main.height)/2);
		frameA.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
		JProgressBar bar;
		bar = new JProgressBar(0, 100);
		bar.setStringPainted(true);
		frameA.add(bar);
		frameA.setVisible(true);
		
		//Runs through a fake progress test
		for (int i = 0; i < 10; i++)
		{
			for (int j = 0; j < 10; j++)
			{
				for (int a = 0; a < 5000; a++)
					System.out.print(0);
	
				bar.setValue(i*10 + j + 1);
			}
		}
		
		//Get rid of JFrame A
		frameA.dispose();
		
		//Creates JFrame B
		JFrame frameB = new JFrame("JFrame B");		
		frameB.setResizable(false);
		frameB.setSize(Main.width, Main.height);
		frameB.setLocation((Main.screenWidth - Main.width)/2, (Main.screenHeight - Main.height)/2);
		frameB.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

		//Adds a button to the JFrame
		JButton button = new JButton("Go");

		//Lets us listen to the button being pressed
		button.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				//Creates JFrame C				
				final JFrame frameC = new JFrame("JFrame C");		
				frameC.setResizable(false);
				frameC.setSize(Main.width, Main.height);
				frameC.setLocation((Main.screenWidth - Main.width)/2, (Main.screenHeight - Main.height)/2);
				frameC.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
				
				//Adds a JProgressBar to the JFrame
				final JProgressBar bar;
				bar = new JProgressBar(0, 100);
				bar.setStringPainted(true);
				frameC.add(bar);
				frameC.setVisible(true);
				
				new Thread(new Runnable()
				{
					public void run()
					{
						//Runs through a fake progress test
						for (int i = 0; i < 10; i++)
						{
							for (int j = 0; j < 10; j++)
							{
								for (int a = 0; a < 5000; a++)
									System.out.print(0);
					
								final int iIndex = i;
								final int jIndex = j;
								
								SwingUtilities.invokeLater(new Runnable()
								{
									public void run()
									{
										bar.setValue(iIndex*10 + jIndex + 1);
									}
								});
							}
						}

						SwingUtilities.invokeLater(new Runnable()
						{
							public void run()
							{
								frameC.dispose();
							}
						});					
					}
				}).start();
			}
		});
		
		frameB.add(button);
		frameB.setVisible(true);
	}
}

Latest Month

December 2010
S M T W T F S
   1234
567891011
12131415161718
19202122232425
262728293031 

Tags

Powered by LiveJournal.com
Designed by Naoto Kishi