You are on page 1of 34

CHUYN JAVA

SWING GUI FORMS

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn

Ni dung
JFrame

JDialog
JDesktopPane & JInternalFrame JPanel

JFrame Hnh nh

JFrame
JFrame c s dng lm giao din chnh trong ng dng Swing (top-level windows) Hu ht cc ng dng Swing c xy dng t JFrame

JFrame M rng JFrame


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 package packageName;
import javax.swing.*; public class FrameName extends JFrame {

public FrameName() { this.initComponents(); } /** * Khi to cc thnh phn */ private void initComponents() { ... pack(); }
}

JFrame Hin th
1 2 3 4 5 6 7 8 9 package packageName;
public class Main { public static void main(String[] args) { FrameName frm = new FrameName(); frm.setVisible(true); } }

JFrame Mt s phng thc thng dng


public void setDefaultCloseOperation(int operation);

Gn phng thc mc nh khi ngi dng ng Frame


C 4 la chn sau:
WindowContants.DO_NOTHING_ON_CLOSE WindowContants.HIDE_ON_CLOSE

WindowContants.DISPOSE_ON_CLOSE
WindowContants.EXIT_ON_CLOSE

Jframe Mt s phng thc thng dng


public void setExtendedState(int state);

Gn trng thi ca JFrame


C 5 la chn sau JFrame.NORMAL

JFrame.ICONIFIED
JFrame.MAXIMIZED_HORIZ JFrame.MAXIMIZED_VERT

JFrame.MAXIMIZED_BOTH

JFrame Mt s phng thc thng dng


public void setResizable(boolean resizable);
true: Cho php thay i kch thc false: khng cho php

public void setTitle(String title);


Gn ta cho JFrame

public void setBackground(Color c);


Gn mu nn cho JFrame public void setForeground(Color c);

Gn mu ch cho JFrame
9

JFrame Mt s phng thc thng dng


public void setIconImage(Image image); Gn hnh nh Icon cho JFrame public void setSize(Dimension d); public void setSize(int width, int height); Gn kch thc cho JFrame public void setLocation(Point p); public void setLocation(int x, int y); Gn v tr cho JFrame public void setVisible(boolean visible); true : hin JFrame false : n Jframe Tng t cho cc phng thc get/is

10

JFrame Mt s phng thc thng dng


public void dispose(); Hy public Containter getCotentPane() getContentPane.add

11

Jframe V d 1

12

JFrame V d 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
13

package swingcontainersample; import javax.swing.*; public class ViDu1Frame extends JFrame{ public ViDu1Frame() { super(); this.initComponents(); } private void initComponents() { this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); ImageIcon icon=new ImageIcon( this.getClass().getResource("images/java-icon.jpg") ); this.setIconImage(icon.getImage()); this.setTitle("V d 1"); this.setLocation(0, 0); this.setSize(300, 400); this.setExtendedState(JFrame.NORMAL); } }

JFrame V d 1
1 2 3 4 5 6 7 8 9 package swingcontainersample;
public class Main { public static void main(String[] args) { ViDu1Frame frm = new ViDu1Frame(); frm.setVisible(true); } }

14

JFrame V d 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
15

package swingcontainersample; import javax.swing.*; public class ViDu2Frame extends JFrame { private JButton jbt1; public ViDu2Frame() { super(); this.initComponents(); } private void initComponents() { //JFrame this.setTitle("V d 2"); this.setSize(200, 100); this.setExtendedState(JFrame.NORMAL); //Buton this.jbt1=new JButton(); this.jbt1.setText("Button 1"); this.add(this.jbt1); } }

JDialog Hnh nh

16

JDialog
JFrame hay JDialog thng s dng JDialog nhp liu hoc xut liu JDialog c 2 trng thi Modal: Khi Jdialog thc hin xong mi c php thao tc ln form cha . Modeless: Sau khi hin th dialog, ngi dng c th thao tc ln form cha JDialog thng c s dng vi trng thi Modal

17

JDialog M rng JDialog


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 package packageName; import javax.swing.*;
public class NameDialog extends JDialog { D liu + Cc phng thc get/set (nu c) public NameDialog(JFrame parent, boolean modal){ super(parent, modal); this.intComponents(); } public NameDialog(JDialog parent, boolean modal){ super(parent, modal); this.intComponents(); } private void intComponents(){ } }
18

JDialog Hin th JDialog


JFrame hay JDialog thng s dng JDialog nhp liu hoc xut liu Jdialog thng c thit lp trng thi Modal, ngha l khi Jdialog thc hin xong mi c php thao tc ln form cha .

19

JDialog Hin th JDialog


1 2 3 4 5 6 7 8 //JFrame hoc JDialog khc gi private void jbuttonActionPerformed(ActionEvent evt){ DialogName dlg=new DialogName (this, true); Gn d liu cho dlg thng qua set (nu c) dlg.setVisible(true); Ly d liu cho dlg thng qua get (nu c) }

20

JDialog Mt s phng thc thng dng


public void setDefaultCloseOperation(int operation);

Gn phng thc mc nh khi ngi dng ng Frame


C 3 la chn sau:
WindowContants.DO_NOTHING_ON_CLOSE WindowContants.HIDE_ON_CLOSE

WindowContants.DISPOSE_ON_CLOSE

21

JDialog Mt s phng thc thng dng


public void setResizable(boolean resizable);
true: Cho php thay i kch thc false: khng cho php

public void setTitle(String title);


Gn ta cho JDialog

public void setBackground(Color c);


Gn mu nn cho JFrame public void setForeground(Color c);

Gn mu ch cho JFrame
22

JDialog Mt s phng thc thng dng


public void setIconImage(Image image); Gn hnh nh Icon cho JFrame public void setSize(Dimension d); public void setSize(int width, int height); Gn kch thc cho JFrame public void setLocation(Point p); public void setLocation(int x, int y); Gn v tr cho JFrame public void setVisible(boolean visible); true : hin JFrame false : n Jframe

23

JDialog Cc phng thc thng dng


public true: false public Hy void setModal(boolean modal); Modal : Modeless void dispose();

public Containter getCotentPane() getContentPane.add Tng t cho cc phng thc get/is

24

JDesktopPane Hnh nh

MDI

JDesktopPane

25

JDesktopPane
JDesktopPane c s dng xy dng ng dng MDI JDesktopPane thng l mt thnh phn bn trong JFrame JDesktopPane thng c s dng cha cc JInternalFrame bn trong

26

JDesktopPane Mt s phng thc thng dng


public void add(Component component); component : thng l JInternalFrame public JInternalFrame getSelectedFrame(); Ly JInternalFrame ang c chn public JInternalFrame [] getAllFrames();

Ly tt c cc JInternalFrame bn trong

27

JInternalFrame Hnh nh

JDesktopPane

MDI

28

JInternalFrame
JInternalFrame c s dng a vo bn trong JDesktopPane ca JFrame xy dng ng dng MDI. Cc bc xy dng ng dng MDI: Bc 1: Xy dng mt hay nhiu JInternalFrame Bc 2:To mt JFrame c mt JDesktopPane bn trong Bc 3: Gn JInternalFrame vo bn trong JDesktopPane

29

JInternalFrame Cc phng thc thng dng


Tng t nh JFrame

30

JPanel Hnh nh

31

JPanel
JPanel c s dng gom nhm cc control bn trong, c th c s dng nh mt user control JPanel c s dng nh mt thnh phn bn trong JFrame, JDialog, JInternalFrame, hoc trong mt Jpanel khc.

32

Ti liu tham kho


http://download.oracle.com/javase/tutorial/uiswing/co mponents/index.html

33

HI V P

34

You might also like