Jak cię złapią, to znaczy, że oszukiwałeś. Jak nie, to znaczy, że posłużyłeś się odpowiednią taktyką.
sleep(pause);
} catch(InterruptedException e) { throw new RuntimeException(e); } } } } public class ColorBoxes extends JApplet { private boolean isApplet = true; private int grid = 12; private int pause = 50; public void init() { // Get parameters from Web page: if(isApplet) { String gsize = getParameter("grid"); if(gsize != null) grid = Integer.parseInt(gsize); String pse = getParameter("pause"); if(pse != null) pause = Integer.parseInt(pse); } Container cp = getContentPane(); cp.setLayout(new GridLayout(grid, grid)); for(int i = 0; i < grid * grid; i++) cp.add(new CBox(pause)); } public static void main(String[] args) { ColorBoxes applet = new ColorBoxes(); applet.isApplet = false; if(args.length > 0) applet.grid = Integer.parseInt(args[0]); if(args.length > 1) Chapter 14: Creating Windows & Applets 895 applet.pause = Integer.parseInt(args[1]); Console.run(applet, 500, 400); } } ///:~ ColorBoxes is the usual applet/application with an init( ) that sets up the GUI. This configures a GridLayout so that it has grid cells in each dimension. Then it adds the appropriate number of CBox objects to fill the grid, passing the pause value to each one. In main( ) you can see how pause and grid have default values that can be changed if you pass in command-line arguments, or by using applet parameters. Feedback CBox is where all the work takes place. This is inherited from JPanel and it implements the Runnable interface so that each JPanel can also be a Thread. Remember that when you implement Runnable, you don’t make a Thread object, just a class that has a run( ) method. Thus, you must explicitly create a Thread object and hand the Runnable object to the constructor, then call start( ) (this happens in the constructor). In CBox this thread is called t. Feedback Notice the array colors, which is an enumeration of all the colors in class Color. This is used in newColor( ) to produce a randomly selected color. The current cell color is cColor. Feedback paintComponent( ) is quite simple—it just sets the color to cColor and fills the entire JPanel with that color. Feedback In run( ), you see the infinite loop that sets the cColor to a new random color and then calls repaint( ) to show it. Then the thread goes to sleep( ) for the amount of time specified on the command line. Feedback Precisely because this design is flexible and threading is tied to each JPanel element, you can experiment by making as many threads as you want. (In reality, there is a restriction imposed by the number of threads your JVM can comfortably handle.) Feedback This program also makes an interesting benchmark, since it can and has shown dramatic performance and behavioral differences between one JVM threading implementation and another. Feedback 896 Thinking in Java www.BruceEckel.com Managing concurrency 12When you make changes to any Swing component properties from the main method of your class or in a separate thread, be aware that the event dispatching thread might be vying for the same resources. Feedback The following program shows how you can get an unexpected result by not paying attention to the event dispatching thread: //: c14:EventThreadFrame.java // Race Conditions using Swing Components. import javax.swing.*; import java.awt.*; import java.awt.event.*; import com.bruceeckel.swing.Console; public class EventThreadFrame extends JFrame { private JTextField statusField = new JTextField("Initial Value"); public EventThreadFrame() { Container cp = getContentPane(); cp.add(statusField, BorderLayout.NORTH); addWindowListener(new WindowAdapter() { public void windowOpened(WindowEvent e) { try { // Simulate initialization overhead Thread.sleep(2000); } catch (InterruptedException ex) { throw new RuntimeException(ex); } statusField.setText("Initialization complete"); } }); } public static void main (String[] args) { EventThreadFrame etf = new EventThreadFrame(); Console.run(etf, 150, 60); etf.statusField.setText("Application ready"); System.out.println("Done"); } } ///:~ 12 This section was created by Jeremy Meyer. Chapter 14: Creating Windows & Applets 897 It is easy to see what is supposed to happen. In the main method, a new EventThreadFrame class is created and run using the Console.run( ) method. After the frame has been created and run, the value of the text field is set to “Application ready,” and then, just before exiting main( ), “Done” is sent to the console. Feedback When the frame is created, the text field is constructed with the value “Initial Value” in the constructor of the frame, and an event listener is added which listens for the opening of the window. This event will be received by the JFrame as soon as the setVisible(true) method has been called (by Console.run( )) and is the right place to do any initialization that affects the view of the window. In this example, a call to sleep( ) simulates some initialization code that might take a couple of
|
Wątki
|