240 likes | 670 Views
Swing. Swing. 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.
E N D
Swing 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, andJDialog 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”);
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( );
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.
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 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/
Applet Read Write Clear Swing Example 1 – an applet with 3 buttons and 2 text fields Step1 – Design the interface Enter text Correct Entry? 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
Enter text Correct Entry? Applet Read Write Clear Swing The top Panel will use a GridLayout with 2 rows and 2 columns (with a separation of 10 pixels)
Add components to the grid – left to right, top to bottom Swing Construct the top panel – write a method makeJTextPanel( ) //(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(newGridLayout(2,2, 10, 10)); theText.add(edit); theText.add(source); theText.add(verify); theText.add(target); return theText; }
Enter text Correct Entry? Applet Read Write Clear Swing Top JPanel Bottom JPanel Construct the bottom Panel – Use FlowLayout (center adjusted)
Create the bottom panel 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; } Set its layout and color, and add the buttons to it (left to right)
For heavyweight component like JApplet components must be added to a ContentPane 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); } //add theButtons (a JPanel) to the ContentPane //the default LayoutManager of a JApplet is BorderLayout
JApplet JPanel JLabel MyJTextField MyJButton Swing Design of the System A button event will cause a read, write, or clear of a text field. MyJButton objects interact with MyJTextField objects The applet is an aggregate of 2 JPanels 2 ActionListener JPanels contain JLabels and MyJTextField objects OR MyJButtons String response MyJTextField target 2 3 MyJTextField(int size) doButtonAct(MyJButton) MyJButton(String, MyJTextField) actionPerformed(ActionEvent) activates describes
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 Swing class MyJButton class MyJButton extends JButton implements ActionListener { privateMyJTextField 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); } }
Swing class MyJTextField extends JTextField { private String response = new String( ); public MyJTextField(int size) { super(size); } publicvoiddoButtonAct(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
Swing The Applet JButtons -- implementation of this design JButtons.html
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);
Swing Example 2. Drawing Shapes on a JPanel
Line Arc Ellipse Rectangle Rounded Rectangle Circle RGB Colors R G B Swing JApplet MainPanel Add a JButtonPanel to the North A JScrollBar Panel Finally 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. Add a JPanel to the East that…. Contains a JLabel Panel and…
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.
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.
Swing Here is the applet that implements the previous design Draw Shapes