Download presentation
Presentation is loading. Please wait.
Published byMonica Terry Modified over 9 years ago
2
Swing
3
Differences between Swing and AWT Naming Conventions All Swing components begin with a capital J -- JPanel, JButton, JScrollBar, 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 getContentPane( ); cp.add(new JButton(“Start”);
4
Swing Differences between Swing and AWT Components Use of paintComponent for Drawing public void paintComponent(Graphics g) { super.paintComponent(g); //always call super.paintComponent( ) 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 paintComponent double buffering is automatically employed. There is an option to perform drawing directly to the screen using the paint method by using panel.getGraphics( );
5
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 setXXX( ) and getXXX( ) 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 BorderLayout.
6
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.
7
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 2D- graphics) 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/
8
Swing Example 1 – an applet with 3 buttons and 2 text fields Step1 – Design the interface ReadWriteClear Enter text Correct Entry? Applet 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
9
Swing ReadWriteClear Enter text Correct Entry? Applet The top Panel will use a GridLayout with 2 rows and 2 columns (with a separation of 10 pixels)
10
Swing //(global) attributes of the class private MyJTextField source = new MyJTextField(25); private MyJTextField target = new MyJTextField(25); private JLabel edit = new JLabel("Enter text"); private JLabel verify = new JLabel("Correct Entry?"); private String inStr = new String(""); //method to construct the top panel private JPanel makeJTextPanel( ) { JPanel theText = new JPanel( ); theText.setLayout(new GridLayout(2,2, 10, 10)); theText.add(edit); theText.add(source); theText.add(verify); theText.add(target); return theText; } Construct the top panel – write a method makeJTextPanel( ) Add components to the grid – left to right, top to bottom
11
Swing ReadWriteClear Enter text Correct Entry? Applet Construct the bottom Panel – Use FlowLayout (center adjusted) Top JPanel Bottom JPanel
12
Swing Construct the bottom panel – with a method private MyJButton read; private MyJButton write; private MyJButton clear; //build the bottom panel with method makeJButtonPanel( ) private JPanel makeJButtonPanel() { read = new MyJButton ("Read", source); write = new MyJButton("Write", target); clear = new MyJButton("Clear", target); JPanel theButtons = new JPanel( ); theButtons.setLayout(new FlowLayout(FlowLayout.CENTER)); theButtons.add(read); theButtons.add(write); theButtons.add(clear); theButtons.setBackground(Color.blue); return theButtons; } Create the bottom panel Set its layout and color, and add the buttons to it (left to right)
13
Swing Place the panels into the applet in method init( ) public void init( ) { Container cp = getContentPane( ); theButtons = makeJButtonPanel( ); cp.add(BorderLayout.SOUTH, theButtons); theText = makeJTextPanel( ); cp.add(BorderLayout.CENTER, theText); } For heavyweight component like JApplet components must be added to a ContentPane //add theButtons (a JPanel) to the ContentPane //the default LayoutManager of a JApplet is BorderLayout
14
Swing Design of the System A button event will cause a read, write, or clear of a text field. JPanelJAppletMyJTextField MyJTextField(int size) doButtonAct(MyJButton) String response MyJButtonJLabel MyJButton(String, MyJTextField) actionPerformed(ActionEvent) MyJTextField target ActionListener activates The applet is an aggregate of 2 JPanels 2 32 describes JPanels contain JLabels and MyJTextField objects OR MyJButtons MyJButton objects interact with MyJTextField objects
15
Swing class MyJButton extends JButton implements ActionListener { private MyJTextField target; public MyJButton(String name, MyJTextField targ) { Font buttonFont = new Font ("Times Roman", Font.BOLD, 12); setFont(buttonFont); setText(name); setBackground(Color.cyan); target = targ; addActionListener(this); } public void actionPerformed(ActionEvent e) { target.doButtonAct(this); } class MyJButton Constructor receives handle to text field Hold handle to target text field Event causes message to be sent to the target to perform a read or write
16
Swing class MyJTextField extends JTextField { private String response = new String( ); public MyJTextField(int size) { super(size); } public void doButtonAct(MyJButton source) { response = source.getText( ); if (response.equals("Read") ) inStr = super.getText( ); else if (response.equals("Write") ) setText(inStr); else if (response.equals("Clear") ) setText(""); } A MyJButton object notifies a MyJTextField of a button event and elicits an action
17
Swing The Applet JButtons -- implementation of this design JButtons.html JButtons.html
18
Swing Adding scroll bars to a component such as a JTextArea Just wrap a JTextArea object inside a JScrollPane. JTextArea ta = new JTextArea(10, 25); JScrollPane sp = new JScrollPane(ta); The programmer can control which scrollbars are allowed – vertical, horizontal, both, or neither JScrollPane sp = new JScrollPane(ta, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
19
Swing Example 2. Drawing Shapes on a JPanel
20
Swing ArcLineEllipseRectangle Rounded Rectangle Circle RGB Colors R G B JApplet MainPanel Add a JButtonPanel to the North Add a JPanel to the East that….Contains a JLabel Panel and… A JScrollBar PanelFinally add a JPanel to the Center (whatever is left with no West and South) to serve as a canvas on which you will draw the shapes. Remember! You must get a ContentPane from the JApplet on which you will add these panels.
21
Swing The applet will contain the following classes: MyCanvas – 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 paintComponent( ) for painting the contents of this container, and discover when and which JButton and JScrollBar events occur and handle the repainting commands that they require. MyJScrollBar – Three scroll bars with adjustment listeners handle adjustment events and notify the MyCanvas object to change its background color. MyJButton – Butttons with action listeners respond to an event by notifying the MyCanvas 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.
22
Swing Class Shape uses a BoundsBox object, that is located and adjusted by pressing and dragging a mouse on the MyCanvas object, to determine the location and size parameters of each shape that is drawn. The class MyCanvas object must be notified of and respond to button and scroll bar events. It must also implement the MouseListener and MouseMotionListener interfaces to respond to mousePressed( ) and mouseDragged( ) events that determine the location and extent of the BoundsBox object. Classes MyJButton and MyJScrollBar must maintain a permanent handle to the MyCanvas object that they must notify when one of their events occur.
23
Swing Here is the applet that implements the previous design Draw Shapes
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.