Swing Swing Differences between Swing and AWT Naming
Swing
Swing Differences between Swing and AWT Naming Conventions All Swing components begin with a capital J -- JPanel, JButton, JScroll. Bar, JApplet, etc. . Most Swing Components are “Lightweight” • Use Java code rather than native code to draw in the underlying window. • The Exceptions are JFrame, JApplet, JWindow, and JDialog Use Content Pane for adding Components to “Heavyweight” Containers To add Components to a JApplet (in method init( )) use Container cp = new get. Content. Pane( ); cp. add(new JButton(“Start”);
Swing Differences between Swing and AWT Components Use of paint. Component for Drawing public void paint. Component(Graphics g) { super. paint. Component(g); //always call super. paint. Component( ) before performing custom drawing // draw Shape on the Swing Component } Double Buffering In Swing a JPanel is used instead of a Canvas to be the target for drawing graphics objects. When drawing to a JPanel using paint. Component double buffering is automatically employed. There is an option to perform drawing directly to the screen using the paint method by using panel. get. Graphics( );
Swing Additional features of Swing Components All of the “lightweight” Swing classes inherit from JComponent. The four “heavyweight” components – JFrame, JApplet, JWindow, and JDialog – inherit from their AWT counterparts. Unlike their AWT counterparts, Swing uses set. XXX( ) and get. XXX( ) to access private attributes. This designation is a bit easier for users to remember, and lends itself to the documentation conventions used by Java Beans and class Introspector( ) to display information about a bean. The default layout manager for a JApplet is Border. Layout.
Swing In many ways Swing is an improvement upon the original Java design and the AWT… BUT Many browsers (none before Netscape 6. 0) cannot run the most current versions of the JRE (Java Runtime Environment) and will not run applets using swing components. If you want your applet to be viewable by the widest audience use AWT components.
Swing The Java Plug-In A plug-in is available from Sun that, once installed, allows browsers to run the most current version of the JRE. This allows you to deliver applets that use the more recent JRE classes (like Swing and 2 Dgraphics) that can run over Netscape and IE browsers This capability comes at a price! The plug-in is 5 MB, and downloading the plug-in is not a viable solution for an internet client. The plug-in may be obtained at: http: //java. sun. com/products/plugin/
Swing Example 1 – an applet with 3 buttons and 2 text fields Step 1 – Design the interface Applet Enter text Correct Entry? Read Write Clear The JApplet will consist of 2 JPanels – The top one containing labels and text fields The bottom one containing three JButtons The browser will add a title bar when the applet is running
Swing Applet Enter text Correct Entry? Read Write Clear The top Panel will use a Grid. Layout with 2 rows and 2 columns (with a separation of 10 pixels)
Swing Construct the top panel – write a method make. JText. Panel( ) //(global) attributes of the class private My. JText. Field source = new My. JText. Field(25); private My. JText. Field target = new My. JText. Field(25); private JLabel edit = new JLabel("Enter text"); private JLabel verify = new JLabel("Correct Entry? "); private String in. Str = new String(""); //method to construct the top panel private JPanel make. JText. Panel( ) { JPanel the. Text = new JPanel( ); the. Text. set. Layout(new Grid. Layout(2, 2, 10)); the. Text. add(edit); Add components to the grid – left to the. Text. add(source); right, top to bottom the. Text. add(verify); the. Text. add(target); return the. Text; }
Swing Applet Enter text Top JPanel Correct Entry? Read Write Clear Construct the bottom Panel – Use Flow. Layout (center adjusted) Bottom JPanel
Swing Construct the bottom panel – with a method private My. JButton read; private My. JButton write; private My. JButton clear; //build the bottom panel with method make. JButton. Panel( ) private JPanel make. JButton. Panel() { read = new My. JButton ("Read", source); write = new My. JButton("Write", target); clear = new My. JButton("Clear", target); Create the bottom panel JPanel the. Buttons = new JPanel( ); the. Buttons. set. Layout(new Flow. Layout(Flow. Layout. CENTER)); the. Buttons. add(read); Set its layout and color, and add the. Buttons. add(write); the buttons to it (left to right) the. Buttons. add(clear); the. Buttons. set. Background(Color. blue); return the. Buttons; }
Swing Place the panels into the applet in method init( ) public void init( ) { Container cp = get. Content. Pane( ); the. Buttons = make. JButton. Panel( ); For heavyweight component like JApplet components must be added to a Content. Pane //add the. Buttons (a JPanel) to the Content. Pane cp. add(Border. Layout. SOUTH, the. Buttons); //the default Layout. Manager of a JApplet is Border. Layout the. Text = make. JText. Panel( ); cp. add(Border. Layout. CENTER, the. Text); }
Swing Design of the System A button event will cause a read, write, or clear of a text field. My. JButton objects with of My. JText. Field The applet is aninteract aggregate 2 JPanels objects JApplet 2 JPanels contain JLabels and My. JText. Field objects OR My. JButtons JLabel Action. Listener My. JText. Field My. JButton String response My. JText. Field(int size) describes do. Button. Act(My. JButton) My. JText. Field target 2 3 activates My. JButton(String, My. JText. Field) action. Performed(Action. Event)
Swing class My. JButton extends JButton implements Action. Listener { private My. JText. Field target; Constructor receives handle to text field public My. JButton(String name, My. JText. Field targ) { Font button. Font = new Font ("Times Roman", Font. BOLD, 12); set. Font(button. Font); set. Text(name); set. Background(Color. cyan); target = targ; add. Action. Listener(this); Hold handle to target text field } public void action. Performed(Action. Event e) { target. do. Button. Act(this); Event causes message to be sent to the target to perform a read or write } }
Swing class My. JText. Field extends JText. Field { private String response = new String( ); public My. JText. Field(int size) { super(size); } public void do. Button. Act(My. JButton source) { response = source. get. Text( ); if (response. equals("Read") ) in. Str = super. get. Text( ); else if (response. equals("Write") ) set. Text(in. Str); else if (response. equals("Clear") ) set. Text(""); } } A My. JButton object notifies a My. JText. Field of a button event and elicits an action
Swing The Applet JButtons -- implementation of this design JButtons. html
Swing Adding scroll bars to a component such as a JText. Area Just wrap a JText. Area object inside a JScroll. Pane. JText. Area ta = new JText. Area(10, 25); JScroll. Pane sp = new JScroll. Pane(ta); The programmer can control which scrollbars are allowed – vertical, horizontal, both, or neither JScroll. Pane sp = new JScroll. Pane(ta, JScroll. Pane. VERTICAL_SCROLLBAR_ALWAYS, JScroll. Pane. HORIZONTAL_SCROLLBAR_NEVER);
Swing Example 2. Drawing Shapes on a JPanel
Swing JApplet Main. Panel Line Arc Ellipse Rectangle Rounded Rectangle Circle RGB Colors R G B A Finally JScroll. Bar add a to Panel JPanel to the Center (whatever is left no West Add JButton. Panel to the North Remember! You get a Content. Pane from thewith JApplet on Add Contains aa. JPanel a JLabel themust Panel East that…. and… and South) to serve a canvas which you will add as these panels. on which you will draw the shapes.
Swing The applet will contain the following classes: My. Canvas – a JPanel that will be the object where the shapes will be placed. This class will need to hold the shape objects in a container, implement the method paint. Component( ) for painting the contents of this container, and discover when and which JButton and JScroll. Bar events occur and handle the repainting commands that they require. My. JScroll. Bar – Three scroll bars with adjustment listeners handle adjustment events and notify the My. Canvas object to change its background color. My. JButton – Butttons with action listeners respond to an event by notifying the My. Canvas object to add a new shape to its collection and indicate the specific shape that has been requested. A class Shape that is extended by such classes as Line, Ellipse, Rectangle, etc. – These classes hold color and size information and provide a draw method for locating and painting the shape on the canvas.
Swing Class Shape uses a Bounds. Box object, that is located and adjusted by pressing and dragging a mouse on the My. Canvas object, to determine the location and size parameters of each shape that is drawn. The class My. Canvas object must be notified of and respond to button and scroll bar events. It must also implement the Mouse. Listener and Mouse. Motion. Listener interfaces to respond to mouse. Pressed( ) and mouse. Dragged( ) events that determine the location and extent of the Bounds. Box object. Classes My. JButton and My. JScroll. Bar must maintain a permanent handle to the My. Canvas object that they must notify when one of their events occur.
Swing Here is the applet that implements the previous design Draw Shapes
- Slides: 22