260 likes | 438 Vues
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.
E N D
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 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
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.*;
Swing components Top-level Container • JFrame(Form Application) • JApplet (Applet) • JDialog Intermediate-level Container • JPanel • JTabbedPane Atomic components • JLabel, JTextField, JTextArea, JButton, JComboBox, JCheckBox
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); }
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.
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();
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);
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;
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.
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); } }
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.
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.
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(); } }
Event Handling public void actionPerformed(ActionEventevt) { if (evt.getActionCommand().equals("Nhap")) stat.setText(txtNhap.getText()); else if (evt.getActionCommand().equals("Thoat")) dispose(); }
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); } }
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);
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);
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).
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.
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);
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.