1 / 23

JAVA GUI Programming

JAVA GUI Programming. CONTENTS. Types of GUI Programs Swing components Layout Events and Listeners Convert Applet into Form Application. Types of GUI Programs. There are two basic types of GUI program in Java: Stand-alone A pplications and applets.

yeriel
Download Presentation

JAVA GUI Programming

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 GUI Programming

  2. CONTENTS • Types of GUI Programs • Swing components • Layout • Events and Listeners • Convert Applet into Form Application.

  3. Types of GUI Programs • There are two basic types of GUI program in Java: Stand-alone Applications and applets. • An applet is a program that runs in a rectangular area on a Web page. • A Stand-alone application is a program that runs on its own, without depending on a Web browser

  4. SWING COMPONENTS • GUI program uses objects (components) to make interaction possible with users. • To use components in java programs, import awt (Abstract Windows Toolkits) or swing componentlibraries. Swing is preferable over AWT as more lightweight and is newer. • Import java.awt.*; Or • Import javax.swing.*;

  5. Swing components Top-level Container • JFrame(Form Application) • JApplet (Applet) • JDialog Intermediate-level Container • JPanel • JTabbedPane Atomic components • JLabel, JTextField, JTextArea, JButton, JComboBox, JCheckBox

  6. Simple frame application import javax.swing.*; public class FrameAppextends JFrame { FrameApp() { super("My Frame"); setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); setVisible(true); } public static void main(String[] args) { new FrameApp(); } import javax.swing.*; public class FrameApp{ public static void main(String[] args) { Jframe w = new JFrame(); w.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); w.setVisible(true); }

  7. Content Pane • A basic JFramehas a blank content pane; • One can either: • Add things to that pane or • Replace the basic content pane entirely with another container. • JPanel is one of fundamental classes in Swing. The basic JPanelis just a blank rectangle. There are two ways to make a useful JPanel: • The first is to add other components to the panel; • The second is to draw somethingon the panel.

  8. Jpanel as a container • Another way of using a JPanel is as a container to hold other components. Java has many classes that define GUI components. Before these components can appear on the screen, they must be added to a container. JLabellabel = new JLabel("Nhap so:"); JTextFieldtxtNhap = new JTextField(15); JButtonbtnNhap = new JButton("Nhap"); JPanelcontent = new JPanel();

  9. Jpanel as a container JLabel label = new JLabel("Nhap so:"); JTextFieldtxtNhap = new JTextField(15); JButtonbtnNhap = new JButton("Nhap"); JPanel content = new JPanel(); content.add(label); content.add(txtNhap); content.add(btnNhap); setContentPane(content);

  10. Layout • The way components are added to a container, • In Java the technique for laying out components is to use a layout manager. • A layout manager is an object to arrange the components in a container;

  11. Setting up GUI • Create a container and assign a layout manager to it, • Create components and add them to the container, • Use the container as the content pane of a window or applet.

  12. Setting up GUI public class demoTwo { public static class displayPanel extends JPanel { public void paintComponent(Graphics g){ g.setColor(Color.RED); g.drawOval(50, 50, 150, 150); } } public static void main(String[] args) { JFramew = new JFrame("Demo two"); w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); w.setSize(400, 300); w.setVisible(true); displayPanelaPanel = new displayPanel(); w.setContentPane(aPanel); } }

  13. Events and Listeners • GUIs are largely event-driven; • The program waits for events that are generated by the user's actions • When an event occurs, the program responds by executing an event-handling method. • In order to program the behavior of a GUI, an event-handling methods must be implemented to respond to the events.

  14. Events and Listeners • A listener is an object that includes one or more event-handling methods. • The listener, has the responsibility of responding to the event. • The event itself is actually represented by a third object, which carries information about the type of event, when it occurred, and so on.

  15. import javax.swing.*; import java.awt.event.*; public class FrameAppextends JFrame implements ActionListener { FrameApp(String title){ super(title); JButtonbttn = new JButton("Click Me!"); bttn.addActionListener(this); getContentPane().add(bttn); } public void actionPerformed(ActionEventevt) { String title = "Greetings"; String message = "Hello from the Swing User Interface Library."; JOptionPane.showMessageDialog(null, message, title, JOptionPane.INFORMATION_MESSAGE); } public static void main(String[] arg) { FrameAppframe = new FrameApp("Frame Application"); frame.setSize(150,70); frame.show(); } }

  16. Event Handling public void actionPerformed(ActionEventevt) { if (evt.getActionCommand().equals("Nhap")) stat.setText(txtNhap.getText()); else if (evt.getActionCommand().equals("Thoat")) dispose(); }

  17. Setting up Drawing Panel on Frame public class demoTwo{ public static class displayPanel extends JPanel{ public displayPanel() { } } public static void main(String[] args) { JFrame win = new JFrame("Demo two"); win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); win.setSize(400, 300); win.setVisible(true); displayPanelaPanel = new displayPanel(); win.setContentPane(aPanel); } }

  18. MouseEvent & MouseListener MouseListeneris usedas a listener for mouse events, an object must implement this MouseListener interface. The MouseListener interface specifies five different instance methods: public void mousePressed(MouseEventevt); public void mouseReleased(MouseEventevt); public void mouseClicked(MouseEventevt); public void mouseEntered(MouseEventevt); public void mouseExited(MouseEventevt);

  19. MouseMotionListener MouseMotionListener is usedas a listener for mouse motion events, an object must implement this MouseMotionListener interface to handle motion event The interface specifies two different instance methods: public void mouseDragged(MouseEventevt); public void mouseMoved(MouseEventevt);

  20. Mouse coordinates • MouseEventevt parameter hold mouse coordinates information when mouse event occurred. • The coordinates of the mouse cursor can be retrieved by calling evt.getX() and evt.getY(). The coordinates are expressed in the coordinate system of the component that generated the event, where the top left corner of the component is (0,0).

  21. Modifiers keys • The user can hold down certain modifier keys while using the mouse. The possible modifier keys include: the Shift key, the Control key, the ALT key . You might want to respond to a mouse event differently when the user is holding down a modifier key. The boolean-valued instance methods evt.isShiftDown(), evt.isControlDown(), evt.isAltDown() and evt.isMetaDown() can be called to test whether the modifier keys are pressed.

  22. MouseMotionListeners& Dragging • The methods for responding to mouse motion events are defined in an interface named MouseMotionListener. This interface specifies two event-handling methods: public void mouseDragged(MouseEventevt); public void mouseMoved(MouseEventevt);

  23. MouseMotionListeners& Dragging • To respond to mouse motion events, you must create an object that implements the MouseMotionListener interface, then register that object to listen for events by calling a component's addMouseMotionListener method. The object will then listen for mouseDragged and mouseMoved events associated with that component.

More Related