1 / 16

Java Swing One of the most important features of Java is its ability to draw graphics.

Java Swing One of the most important features of Java is its ability to draw graphics. AWT to Swing. AWT: Abstract Window Toolkit import java.awt.* Swing: new with Java2 import javax.swing.* extends AWT Depends less on underlying platform Many more predefined GUI components

Download Presentation

Java Swing One of the most important features of Java is its ability to draw graphics.

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java SwingOne of the most important features of Java is its ability to draw graphics.

  2. AWT to Swing • AWT: Abstract Window Toolkit • import java.awt.* • Swing: new with Java2 • import javax.swing.* extends AWT • Depends less on underlying platform • Many more predefined GUI components • Event listeners • API: • http://docs.oracle.com/javase/7/docs/api/

  3. JButton GUI Component API • Java: GUI component = class • Properties • Methods • Events

  4. Using a GUI Component • Create it • Instantiate object: b = new JButton(“press me”); • Configure it • Properties: b.text = “press me”; //avoid • Methods: b.setText(“press me”); //better • Add it • panel.add(b); • Listen to it • Events: Listeners JButton

  5. JFrame JFrame JPanel containers JPanel JButton JButton JLabel JLabel Anatomy of an Application GUI GUI Internal structure

  6. Using a GUI Component 2 • Create it • Configure it • Add children (if container) • Add to parent (if not JFrame) • Listen to it orderimportant

  7. Listener JButton JLabel JPanel JFrame Add from bottom up • Create: • Frame • Panel • Components • Listeners • Add: (bottom up) • listeners into components • components into panel • panel into frame

  8. Frames • Swing version JFrame (extends AWT’s frame) • Import javax.swing (‘x’ – extension package) • Default size is 0x0 pixels DEMO FrameTest.java

  9. Frames • Need to extend class • Override constructor for size desired. • Select behavior on Close (for example, exit) • Start invisible, must show DEMO SimpleFrameTest.java

  10. Frames • Only a few methods to change look • Inherits methods from superclasses • dispose– closes window and reclaims resources • setIconImage - used when window is minimized • setTitle – for menu bar of window • setResizable – takes boolean to enable/disable user’s ability to resize window • setLocation – position component • setBounds – size and position component DEMO SimpleFrameTest2.java

  11. Inheritance Chain Object Window Frame JFrame Component Container JComponent JPanel

  12. Panels • Draw on a panel, which is added to frame • Specifically to the content pane of the frame • Displays information • Draw strings • Draw graphical elements • Add components into content pane (they are containers) SomeComponent c = new … Container contentPane = frame.getContentPane(); contentPane.add (c);

  13. press me Application Code import javax.swing.*; class hello { public static void main(String[ ] args) { JFrame f = new JFrame(“title”); JPanel p = new JPanel(); JButton b = new JButton(“press me”); p.add(b); // add button to panel f.setContentPane(p); // add panel to frame f.show(); } }

  14. Subclasses • Extend JPanel • Override paintComponent method in that class to do graphics class MyPanel extends Jpanel { public void paintComponent (Graphics g) { //code for drawing goes here } DEMO NotHelloWorld.java

  15. Painting… • Never call paintComponent • Event handler notifies component to paint itself whenever needed • Resizing, minimizing, overlapping windows, etc. • If you need to force repainting, call repaint instead

  16. Subclasses cont. class MyPanel extends Jpanel { public void paintComponent (Graphics g) { super.paintComponent (g); //superclass does job //code for drawing goes here g.drawString (“Java is COOL!”, 75, 100); } DEMO NotHelloWorld.java

More Related