New to Java? We'll help you get started with our revised beginner's tutorial, or our free online textbook.
|
Get the latest Java books |
|
h t t p : / /w w w . j a v a c o f f e e b r e a k . c
o m /
|
Menu Articles Using Java Applets Looking for Java resources? Check out the Java Coffee Break directory! |
Writing the AWT versionThe code to generate an AWT frame, and a few buttons, is actually quite small. Figure 3 shows the application in action, and you should examine the source code. Next, we'll show you how to convert it into a Swing application. import java.awt.*; import java.awt.event.*; public class AWTFrame { public static void main (String args[]) throws Exception { Frame frame = new Frame("AWT Demo"); Label label = new Label ("AWT Version"); Button button = new Button ("Click to close"); button.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent evt) { System.exit(0); } }); // Set layout manager frame.setLayout( new FlowLayout() ); // Add to frame frame.add( label ); frame.add( button ); frame.pack(); // Center the frame Toolkit toolkit = Toolkit.getDefaultToolkit(); // Get the current screen size Dimension scrnsize = toolkit.getScreenSize(); // Get the frame size Dimension framesize= frame.getSize(); // Set X,Y location frame.setLocation ( (int) (scrnsize.getWidth() - frame.getWidth() ) / 2 , (int) (scrnsize.getHeight() - frame.getHeight()) / 2); frame.setVisible(true); } }
|
||||
|