DOC PREVIEW
USC CSCI 571 - 04Basic-Swing

This preview shows page 1-2-3-22-23-24-45-46-47 out of 47 pages.

Save
View full document
Premium Document
Do you want full access? Go Premium and unlock all 47 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

core Web programming Basic Swing GUI Controls in Java 2 1 2001 2002 Marty Hall Larry Brown http www corewebprogramming com Agenda New features Basic approach Summary of Swing components Starting points JApplet JFrame Swing equivalent of AWT components JLabel JButton JPanel JSlider New Swing components JColorChooser JInternalFrame JOptionPane JToolBar JEditorPane Other simple components 2 JCheckBox JRadioButton JTextField JTextArea JFileChooser www corewebprogramming com Basic Swing New Features Many more built in controls Image buttons tabbed panes sliders toolbars color choosers HTML text areas lists trees and tables Increased customization of components Border styles text alignments and basic drawing features Images can be added to almost any control A pluggable look and feel Not limited to native look Many miscellaneous small features Built in double buffering tool tips dockable toolbars keyboard accelerators custom cursors etc Model view controller architecture 3 Can change internal representation ofwww corewebprogramming com trees lists tables Basic Swing Swing vs AWT Programming Naming convention All Swing component names begin with a capital J and follow the format JXxx E g JFrame JPanel JApplet JDialog JButton Many are just AWT names with a J Lightweight components Most Swing components are lightweight formed by drawing in the underlying window Use of paintComponent for drawing Custom drawing code is in paintComponent not paint Double buffering turned on by default New Look and Feel as default With Swing you have to explicitly set the native look 4 Don t mix Swing and AWT in same window www corewebprogramming com Basic Swing Windows Look and Feel 5 Basic Swing www corewebprogramming com Motif Look and Feel 6 Basic Swing www corewebprogramming com Java Look and Feel 7 Basic Swing www corewebprogramming com Setting Native Look and Feel Most applications should use native look not default Java look Changing is tedious so use static method 8 public class WindowUtilities public static void setNativeLookAndFeel try UIManager setLookAndFeel UIManager getSystemLookAndFeelClassName catch Exception e System out println Error setting native LAF e Basic Swing www corewebprogramming com Whirlwind Tour of Basic Components Starting points JApplet JFrame Swing equivalent of AWT components JLabel JButton JPanel JSlider New Swing components JColorChooser JInternalFrame JOptionPane JToolBar JEditorPane Other simple components JCheckBox JRadioButton JTextField JTextArea JFileChooser 9 Basic Swing www corewebprogramming com Starting Point 1 JApplet Content pane A JApplet contains a content pane in which to add components Changing other properties like the layout manager background color etc also applies to the content pane Access the content pane through getContentPane Layout manager The default layout manager is BorderLayout as with Frame and JFrame not FlowLayout as with Applet BorderLayout is really layout manager of content pane Look and feel The default look and feel is Java Metal so you have to explicitly switch the look and feel if you want the native look 10 Basic Swing www corewebprogramming com JApplet Example Code import java awt import javax swing public class JAppletExample extends JApplet public void init WindowUtilities setNativeLookAndFeel Container content getContentPane content setBackground Color white content setLayout new FlowLayout content add new JButton Button 1 content add new JButton Button 2 content add new JButton Button 3 11 Basic Swing www corewebprogramming com JApplet Example Output 12 Basic Swing www corewebprogramming com Starting Point 2 JFrame Content pane JFrame uses content pane in same way as does JApplet Auto close behavior JFrames close automatically when you click on the Close button unlike AWT Frames However closing the last JFrame does not result in your program exiting the Java application So your main JFrame still needs a WindowListener to call System exit Or alternatively if using JDK 1 3 or later you can call setDefaultCloseOperation EXIT ON CLOSE This permits the JFrame to close however you won t be able to complete any house cleaning as you might in the WindowListener Look and feel The default look and feel is Java Metal 13 Basic Swing www corewebprogramming com JFrame Example Code import java awt import javax swing 14 public class JFrameExample public static void main String args WindowUtilities setNativeLookAndFeel JFrame f new JFrame This is a test f setSize 400 150 Container content f getContentPane content setBackground Color white content setLayout new FlowLayout content add new JButton Button 1 content add new JButton Button 2 content add new JButton Button 3 f addWindowListener new ExitListener f setVisible true Basic Swing www corewebprogramming com JFrame Helper ExitListener import java awt import java awt event public class ExitListener extends WindowAdapter public void windowClosing WindowEvent event System exit 0 15 Basic Swing www corewebprogramming com JFrame Example Output 16 Basic Swing www corewebprogramming com Swing Equivalents of AWT Components JLabel New features HTML content images borders JButton New features icons alignment mnemonics JPanel New feature borders JSlider New features tick marks and labels 17 Basic Swing www corewebprogramming com JLabel Main new feature HTML content If text is html html it gets rendered as HTML HTML labels only work in JDK 1 2 2 or later or in Swing 1 1 1 or later In JDK 1 2 the label string must begin with html not HTML It is case insensitive in JDK 1 3 and 1 4 JLabel fonts are ignored if HTML is used If you use HTML all font control must be performed by HTML You must use P not BR to force a line break Other HTML support is spotty Be sure to test each HTML construct you use Permitting the user to enter HTML text at runtime is asking for trouble 18 Other new features images borders www corewebprogramming com Basic Swing JLabel Example Code String labelText html FONT COLOR WHITE WHITE FONT and FONT COLOR GRAY GRAY FONT Text html JLabel coloredLabel new JLabel labelText JLabel CENTER labelText html B Bold B and I Italic I Text html JLabel boldLabel new JLabel labelText JLabel CENTER labelText html The Applied Physics Laboratory is of the Johns Hopkins University P html 19 Basic Swing www corewebprogramming com JLabel Example Output 20 Basic Swing www corewebprogramming com JButton Main new feature icons 1 Create an ImageIcon by passing the ImageIcon constructor a String representing a GIF


View Full Document

USC CSCI 571 - 04Basic-Swing

Download 04Basic-Swing
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view 04Basic-Swing and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view 04Basic-Swing and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?