04 - GUI Programming With JAVA

download 04 - GUI Programming With JAVA

of 49

description

GUI Programming with JAVA

Transcript of 04 - GUI Programming With JAVA

JAVA SWING

GUI Programming with JAVAThS. Hong Mnh [email protected]://sites.google.com/site/hoangha84

Lp trnh Java

Ni dungSGU - CNTT - Lp Trnh JAVA2AWT PackageEvent-handlersLayout ManagersSwing Package

AWT Package

SGU - CNTT - Lp Trnh JAVA3

GUI ProgrammingSGU - CNTT - Lp Trnh JAVA4Graphical User Interface GUI: Giao din ngi dng.GUI cho php chng trnh tng tc vi ngi s dng mt cch d dng, thn thin hn.JAVA cung cp 2 b th vin h tr lp trnh GUI:AWT (Abstract Window Toolkit): xut hin t JDK 1.0. Hin nay hu ht c thay th bi Swing.Swing: mt phn ca Java Foundation Classes (JFC), l mt add-on ca JDK 1.1 sau c thm vo t JDK1.2.Mt s th vin GUI khc: Eclipse's Standard Widget Toolkit (SWT), Google Web Toolkit (GWT)

AWTSGU - CNTT - Lp Trnh JAVA5Bao gm 12 gi, trong s dng ph bin nht l 2 gi: java.awt java.awt.eventPlatform-independentDevice-independent

Gi java.awtSGU - CNTT - Lp Trnh JAVA6

GUI Component classes (such as Button, TextField, and Label),GUI Container classes (such as Frame, Panel, Dialog and ScrollPane),Layout managers (such as FlowLayout, BorderLayout and GridLayout),Custom graphics classes (such as Graphics, Color and Font).

Gi java.awt.eventSGU - CNTT - Lp Trnh JAVA7Event classes (such as ActionEvent, MouseEvent, KeyEvent and WindowEvent),Event Listener Interfaces (such as ActionListener, MouseListener, KeyListener and WindowListener),Event Listener Adapter classes (such as MouseAdapter, KeyAdapter, and WindowAdapter).

Containers v ComponentsSGU - CNTT - Lp Trnh JAVA8Component: cc i tng giao din c bn (Button, label, textfield)Container: cc i tng dng cha cc component (Frame, Panel, Applet)

FrameSGU - CNTT - Lp Trnh JAVA9Frame: container mc cao nht trong AWT.Thanh tiu Thanh menu (ty chn)Phn hin th Thanh tiu ca frame:IconTiu B nt iu khin ca s

PanelSGU - CNTT - Lp Trnh JAVA10L mt vng hnh ch nht dng nhm cc thnh phn giao din khc theo mt layout nht nh.Mi component phi c a vo container.Tt c container u c phng thc: add(Component c)

AWT Container ClassesSGU - CNTT - Lp Trnh JAVA11Top-level: Frame, Dialog, AppletFrame: cung cp giao din ca s chnh.Dialog: ca s pop-up tng tc vi ngi dng.

Applet: top-level container cho applet (chng trnh java nhng trong trnh duyt)

AWT Container ClassesSGU - CNTT - Lp Trnh JAVA12Container cp 2: Panel v ScrollPane

AWT ComponentSGU - CNTT - Lp Trnh JAVA13

Mt s phng thc thng dngSGU - CNTT - Lp Trnh JAVA14 setSize(int width, int height) setVisible(true) setTitle(String title);

Event-Handlers

SGU - CNTT - Lp Trnh JAVA15

AWT Event-HandlingSGU - CNTT - Lp Trnh JAVA16Source, listener, event objectSource object:Button, TextField,Tng tc vi ngi dngKhi c s tng tc t ngi dng, pht sinh event object n tt c cc listener.Listener object: bt cc event, phi ng k i tng listener cho i tng source tng ng.Mi loi event s c x l bi phng thc event-handler ca cc listener tng ng.

AWT Event-HandlingSGU - CNTT - Lp Trnh JAVA17

AWT Event-HandlingSGU - CNTT - Lp Trnh JAVA18Tt c cc XXXListener l nhng interface c xy dng sn trong c cc phng thc x l s kin tng ng vi tng loi s kin (event-handler).

public interface MouseListener extends EventListener { public void mouseClicked(MouseEvent e); public void mousePressed(MouseEvent e); public void mouseReleased(MouseEvent e); public void mouseEntered(MouseEvent e); public void mouseExited(MouseEvent e);}

AWT Event-HandlingSGU - CNTT - Lp Trnh JAVA19Khi mun thc hin mt hnh ng ng vi mt s kin no , ta cn x l trong phng thc event-handler tng ng. lm c iu ny ta cn override event-handler tng ng, thng thng bng cch xy dng lp thc thi interface ca Listener cn thit.class MyMouseListener implements MouseListener { @Override public void mouseClicked(MouseEvent e) { } @Override public void mousePressed(MouseEvent e) { } }Ch : Khi implements cn phi override tt c cc phng thc c sn trong interface.

AWT Event-HandlingSGU - CNTT - Lp Trnh JAVA20Sau thng qua phng thc: srcObj.addXXXListener(XXXListener l) srcObj.removeXXXListener(XXXListener l) ng k / hy ng k Listener tng ng cho i tng source pht sinh s kin.Khi khi ngi dng tng tc vi i tng source s sinh ra event object tng ng.Cc event object ny c tt c cc listener ghi nhn v gi cc phng thc x l event-handler tng ng.

WindowListenerSGU - CNTT - Lp Trnh JAVA21windowOpened: ln u tin ca s c hin thwindowClosed: khi thot bi hm disposewindowActivatedwindowDeactivated

AWT Event-HandlingSGU - CNTT - Lp Trnh JAVA22Vi cch s dng class thc thi interface gy phin phc khi ta ch cn x l 1 s kin m phi override tt c cc event-handler. khc phc, s dng cc lp event listener adapter.tf.addKeyListener(new KeyAdapter() { @Override public void keyReleased(KeyEvent ke) { } });

Layout Managers

SGU - CNTT - Lp Trnh JAVA23

Layout ManagerSGU - CNTT - Lp Trnh JAVA24Layout manager dng sp xp cc component trong container.Cc loi layout trong AWT:FlowLayoutGridLayoutBorderLayoutGridBagLayoutBoxLayoutCardLayoutPhng thc thit lp layout ca container:public void setLayout(LayoutManager mgr)

Thit lp layout cho containerSGU - CNTT - Lp Trnh JAVA25To i tng ca layout cn thit.FlowLayout flowLayout = new FlowLayout();Gi phng thc setLayout() ca container mun thay i layout: f.setLayout(flowLayout);a cc component vo container theo ng th t hoc ng v tr (ty theo loi layout).Ly thng tin layout hin ti ca container:getLayout()

FlowLayoutSGU - CNTT - Lp Trnh JAVA26Cc component hin th theo th t t tri sang phi, t trn xung di bn trong container theo ng th t c add ca chng.Khi ht mt dng s xung dng mi tip theo.Hin th ph thuc vo kch thc hin th ca container.

FlowLayout ConstructorsSGU - CNTT - Lp Trnh JAVA27public FlowLayout();public FlowLayout(int align);public FlowLayout(int align, int hgap, int vgap); // align: FlowLayout.LEFT (or LEADING), FlowLayout.RIGHT (or TRAILING), or FlowLayout.CENTER // hgap, vgap: horizontal/vertical gap between the components // By default: hgap=5, vgap=5, align=CENTER

GridLayoutSGU - CNTT - Lp Trnh JAVA28Cc component hin th theo tng c chia ra bi cc dng v ct.Cc component c thm vo theo th t t tri qua phi, t trn xung di theo th t a vo ca chng.

GridLayout ConstructorsSGU - CNTT - Lp Trnh JAVA29public GridLayout(int rows, int columns);public GridLayout(int rows, int columns, int hgap, int vgap); // By default: rows=1, cols=0, hgap=0, vgap=0

BorderLayoutSGU - CNTT - Lp Trnh JAVA30Container c chia lm 5 vng.Component c thm vo bi phng thcaContainer.add(acomponent, aZone)aZone c th l:BorderLayout.NORTH BorderLayout.SOUTH BorderLayout.WEST BorderLayout.EAST BorderLayout.CENTERPhng thc add(acomponent) s thm vo CENTER

BorderLayout ConstructorsSGU - CNTT - Lp Trnh JAVA31public BorderLayout();public BorderLayout(int hgap, int vgap); // By default hgap=0, vgap=0

Panel trong containerSGU - CNTT - Lp Trnh JAVA32Ta c th dng panel nh mt container ph nhm cc component theo mt layout no .Panel c th a vo panel khc hoc a vo top-level container (Frame).VD: Frame BorderLayoutNORTH: panel FlowLayoutCENTER: panel GridLayout

Swing Package

SGU - CNTT - Lp Trnh JAVA33

Gii thiuSGU - CNTT - Lp Trnh JAVA34Swing l mt phn ca Java Foundation Classes (JFC)JFC gm:Swing API (Application Programming Interface)Accessibility API: cng ngh h tr ngi khim th.Java 2D API: for high quality 2D graphics and images.Pluggable look and feel supports.Drag-and-drop support between Java and native applications.

Tnh nngSGU - CNTT - Lp Trnh JAVA35 phin bn JDK 1.7, Swing gm 18 gi APICh yu dng javax.swing v javax.swing.event.H tr pluggable look-and-feel

Tnh nngSGU - CNTT - Lp Trnh JAVA36Dng cc lp s kin ca AWT trong java.awt.event v thm 1 s lp mi trong javax.swing.event nhng t khi dng n.Dng layout ca AWT ng thi thm mt s mi trong javax.swing.

Swing componentsSGU - CNTT - Lp Trnh JAVA37

Swing componentsSGU - CNTT - Lp Trnh JAVA38Cc lp component ca Swing bt u vi k t J. VD: JLabel, JTextField, JPanel, JFrame

Swing containersSGU - CNTT - Lp Trnh JAVA39Khng dng chung AWT vi Swing. AWT component lun hin th trn Swing component.Tng t trong AWT, 3 top-level container trong Swing l:JFrameJDialogJAppletSecondary container: JPanel

Content-Pane ca SwingSGU - CNTT - Lp Trnh JAVA40Khc vi AWT, cc JComponent khng a vo container trc tip.JComponent phi c a vo thng qua content-pane ca top-level container.Content-pane thc cht l mt java.awt.container.

Cch 1SGU - CNTT - Lp Trnh JAVA41Ly i tng content-pane thng qua phng thc getContentPane() ca top-level container, sau thm cc component vo .Container cp = this.getContentPane();cp.setLayout(new FlowLayout());cp.add(new JLabel("Hello, world!"));cp.add(new JButton("Button"));

Cch 2SGU - CNTT - Lp Trnh JAVA42Ch nh content-pane n mt JPanel thng qua phng thc setContentPane() ca JFrame.JPanel mainPanel = new JPanel(new FlowLayout());mainPanel.add(new JLabel("Hello, world!"));mainPanel.add(new JButton("Button"));

// Set the content-pane of this JFrame to the main JPanelthis.setContentPane(mainPanel);

Mt s phng thc thng dngSGU - CNTT - Lp Trnh JAVA43JFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setToolTipText(String s)

Lp trnh Swing vi NetBeans IDESGU - CNTT - Lp Trnh JAVA44http://docs.oracle.com/javase/tutorial/uiswing/learn/settingup.html

Lp trnh giao din vi Swing

SGU - CNTT - Lp Trnh JAVA45

Thay i Look-and-FeelSGU - CNTT - Lp Trnh JAVA46Thng qua phng thc ca lp UIManager v SwingUtilities.UIManager.setLookAndFeel(UIManager.getInstalledLookAndFeels()[S nguyn].getClassName());SwingUtilities.updateComponentTreeUI(JComponent c);S nguyn: 0: Metal Look and Feel1: Nimbus Look and Feel2: CDE/Motif Look and Feel3: Windows Look and Feel4: Windows Classic Look and Feel

Nhp xut n gin vi JOptionPaneSGU - CNTT - Lp Trnh JAVA47JOptionPane.showInputDialog()JOptionPane.showConfirmDialog()JOptionPane.showMessageDialog()JOptionPane.showOptionDialog()

Nhp xut n gin vi JOptionPaneSGU - CNTT - Lp Trnh JAVA48messageType:ERROR_MESSAGEINFORMATION_MESSAGEWARNING_MESSAGEQUESTION_MESSAGEPLAIN_MESSAGE optionType:DEFAULT_OPTION YES_NO_OPTION YES_NO_CANCEL_OPTION OK_CANCEL_OPTION

Cc component ph binSGU - CNTT - Lp Trnh JAVA49JTextFieldJButtonJCheckBoxJRadioButtonJComboBoxJListJTextArea