Bai Giang 04 - Swing - GUI-1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 36

Swing Package

Ni dung
Giao din ngi dng vi Java (Java GUI) Cc container: JFrame, JPanel, JDialog Cc component: (JLabel, JTextField, JButton, JComboBox, JCheckBox, JRadioButton Layout manager: FlowLayout, GridLayout, BorderLayout, To menu
Overview Of Java Language / Chapter 1/

Giao din ngi dng vi Java


Cung cp cc cng c cho php to giao tip trc quan v hp dn vi ngi dng, c bit n l swing Giao din vi ngi dng bao gm 1 ca s chnh, v cc control c t ln trn Cc thnh phn to giao din nm trong gi javax.swing Tn ca cc lp ny bt u bng ch J
Overview Of Java Language / Chapter 1/

Giao din ngi dng vi Java (2)

Overview Of Java Language / Chapter 1/

JFrame
y l ca s chnh, dng cha cc thnh phn giao din khc. ng vai tr l 1 container JFrame thng dng to ra ca s trong chng trnh swing Hm dng JFrame() JFrame(String title) Cc thnh phn ha c a vo content pane, khng a trc tip vo i tng JFrame V d: frame.getContentPane().add(b);

Overview Of Java Language / Chapter 1/

V d
import javax.swing.*; public class DemoFrm extends JFrame { public DemoFrm() { super("Demo Frame"); setSize(300, 300); setVisible(true); } public static void main(String arg[]) { new DemoFrm(); } }
Overview Of Java Language / Chapter 1/

Kt qu

JPanel
JPanel l container trung gian dng cha cc component khc Thng dng phn chia cc component trong ng dng Layout mc nh l FlowLayout Cc hm dng:
JPanel() JPanel(LayoutManager lm)
Overview Of Java Language / Chapter 1/

Cc thnh phn giao tip ngi dng c bn


Form c dng thu thp thng tin t pha ngi dng Trong khi to giao tip, component c dng cho php nhp liu l textfield hoc textbox khi to cc phn t, cc bc cn phi thc hin l:
To phn t Thit lp cc thuc tnh (size, color, font, ) Xc nh v tr cho n a n vo mn hnh
Overview Of Java Language / Chapter 1/

Cc loi component

Overview Of Java Language / Chapter 1/

JLabel
c dng hin th vn bn (text) v hnh nh (image) To hiu ng trc quan cho mn hnh giao din Cc hm dng ca JLabel:
JLabel(Icon img) JLabel(String st) JLabel(String st, Icon img, int align)
Overview Of Java Language / Chapter 1/

V d
import javax.swing.*; import java.awt.*; public class DemoFrm extends JFrame { public DemoFrm() { super("Demo Frame"); Container c = getContentPane(); JLabel lblHello = new JLabel("Hello, world"); c.add(lblHello); setSize(300, 300); setVisible(true); } public static void main(String arg[]) { new DemoFrm(); } }
Overview Of Java Language / Chapter 1/

JTextComponent
y l lp cha ca tt c cc lp hin th vn bn trong Swing

Overview Of Java Language / Chapter 1/

JTextComponent (2)
JTextField cho php son tho ch 1 dng vn bn Cc hm dng ca JTextField:
JTextField() JTextField(int columns) JTextField(String text) JTextField(String text, int column)

Chui hin th c xc nh trong trng text, v s ct hin th c xc nh trong trng column


Overview Of Java Language / Chapter 1/

V d
import javax.swing.*; import java.awt.*; public class DemoFrm extends JFrame { public DemoFrm() { super("Demo Frame"); Container c = getContentPane(); c.setLayout(new FlowLayout()); JLabel lblHello = new JLabel("Ho va ten"); JTextField tfHoten = new JTextField(20); c.add(lblHello); c.add(tfHoten); pack(); // setsize vua setVisible(true); } public static void main(String arg[]) { new DemoFrm(); } }

Overview Of Java Language / Chapter 1/

JTextArea
y l component cho php nhp vo nhiu dng vn bn C h tr cc thanh cun Cc hm dng:
JTextArea() JTextArea(int rows, int cols) JTextArea(String text) JTextArea(String text, int rows, int cols)
Overview Of Java Language / Chapter 1/

V d
import javax.swing.*; import java.awt.*; public class DemoFrm extends JFrame { public DemoFrm() { super("Demo Frame"); Container c = getContentPane(); c.setLayout(new FlowLayout()); JLabel lblHello = new JLabel("Mo ta"); JTextArea tfHoten = new JTextArea(5, 15); c.add(lblHello); c.add(tfHoten); pack(); setVisible(true); } public static void main(String arg[]) { new DemoFrm(); } }

Overview Of Java Language / Chapter 1/

JPasswordField
import javax.swing.*; import java.awt.*; public class DemoFrm extends JFrame { public DemoFrm() { super("Demo Frame"); Container c = getContentPane(); c.setLayout(new FlowLayout()); JLabel lblHello = new JLabel("Mat khau"); JPasswordField tfHoten = new JPasswordField(12); tfHoten.setEchoChar('*'); c.add(lblHello); c.add(tfHoten); pack(); setVisible(true); } public static void main(String arg[]) { new DemoFrm(); } }
Overview Of Java Language / Chapter 1/

JButton
Th hin chc nng nt bm JButton l lp con ca lp AbstractButton i tng JButton bao gm chui vn bn, hnh nh v cc ng vin Cc hm dng:
JButton() JButton(Icon icon) JButton(String text) JButton(String text, Icon icon) JButton(Action a)
Overview Of Java Language / Chapter 1/

V d
import javax.swing.*; import java.awt.*; public class DemoFrm extends JFrame { public DemoFrm() { super("Demo Frame"); Container c = getContentPane(); c.setLayout(new FlowLayout()); JLabel lblHello = new JLabel("Mat khau"); JPasswordField tfHoten = new JPasswordField(12); tfHoten.setEchoChar('*'); JButton btnOK = new Button("Ok"); c.add(lblHello); c.add(tfHoten); c.add(btnOK); pack(); setVisible(true); } public static void main(String arg[]) { new DemoFrm(); } }
Overview Of Java Language / Chapter 1/

JCheckBox
Checkbox c dng cung cp cho ngi dng kh nng la chn Cc hm dng ca lp JCheckBox:
JCheckBox() JCheckBox(Icon icon) JCheckBox(Icon icon, boolean selected) JCheckBox(String text) JCheckBox(String text, boolean selected) JCheckBox(String text, Icon icon) JCheckBox(String text, Icon icon, boolean selected) JCheckBox(Action a) Overview Of Java Language / Chapter 1/

V d
import javax.swing.*; import java.awt.*; public class DemoFrm extends JFrame { public DemoFrm() { super("Demo Frame"); Container c = getContentPane(); c.setLayout(new FlowLayout()); JLabel lblHello = new JLabel("So thich cua ban "); JCheckBox cbPhim = new JCheckBox("Xem phim"); JCheckBox cbNhac = new JCheckBox("Nghe nhac"); JCheckBox cbSach = new JCheckBox("Doc sach"); JButton btnOK = new JButton("Ok"); c.add(lblHello); c.add(cbPhim); c.add(cbNhac); c.add(cbSach); c.add(btnOK); pack(); setVisible(true); } public static void main(String arg[]) Overview Of Java Language / Chapter 1/ { new DemoFrm(); }

JRadioButton
1 tp cc nt i cho php ch la chn c 1 ti 1 thi im Dng lp ButtonGroup to ra nhm

Cc hm dng ca lp JRadioButton:
JRadioButton() JRadioButton(Icon icon) JRadioButton(Icon icon, boolean selected) JRadioButton(String text) JRadioButton(String text, boolean selected) JRadioButton(String text, Icon icon) JRadioButton(String text, Icon icon, boolean selected) JRadioButton(Action a)
Overview Of Java Language / Chapter 1/

V d
import javax.swing.*; import java.awt.*; public class DemoFrm extends JFrame { public DemoFrm() { super("Demo Frame"); Container c = getContentPane(); c.setLayout(new FlowLayout()); JLabel lblHello = new JLabel("Phai "); JRadioButton rbNam = new JRadioButton("Nam"); JRadioButton rbNu = new JRadioButton("Nu"); ButtonGroup bg = new ButtonGroup(); bg.add(rbNam); bg.add(rbNu); JButton btnOK = new JButton("Ok"); c.add(lblHello); c.add(rbNam); c.add(rbNu); c.add(btnOK); pack(); Overview Of Java Language / Chapter 1/ setVisible(true); }

JList
Khi cc thng tin dng chn la nhiu, chng ta c th dng 1 danh sch cho php vic chn Component JList cho php sp xp d liu hin th, c th phn nhm JList c th hin th chui v icon JList khng h tr double click chut
Overview Of Java Language / Chapter 1/

JList (2) Cc hm dng


JList() JList(ListModel dataModel) JList(Object []listData)

Overview Of Java Language / Chapter 1/

V d
import javax.swing.*; import java.awt.*; public class DemoFrm extends JFrame { public DemoFrm() { super("Demo Frame"); Container c = getContentPane(); c.setLayout(new FlowLayout()); JLabel lblHello = new JLabel("Phai "); String []listData = {"Nam", "Nu"}; JList list = new JList(listData); JButton btnOK = new JButton("Ok"); c.add(lblHello); c.add(list); c.add(btnOK); pack(); setVisible(true); } public static void main(String arg[]) { new DemoFrm(); } }
Overview Of Java Language / Chapter 1/

JComboBox
L s kt hp gia TextField v Listbox Cu trc gn ging nh JList, cc hm dng:
JComboBox() JComboBox(ComboBoxModel asModel) JComboBox(Object []item)

Overview Of Java Language / Chapter 1/

B tr cac thnh phn bn trong cc i tng cha


S dung Layout Managers
FlowLayout BorderLayout GridLayout

Lm vic khng c Layout Manager(Absolute Positioning)

Overview Of Java Language / Chapter 1/

FlowLayout
L mt class cung cp cch qun l vic xp t n gin cc component. Lp FlowLayout c ba cu trc: public FlowLayout() public FlowLayout(int alignment) public FlowLayout(int alignment, int horizontalGap, int verticalGap)
alignment c cc gia tri FlowLayout.LEFT, FlowLayout.CENTER, hoc FlowLayout.RIGHT. Thng s horizontalGap va verticalGap xc nh s Pixel t gia cc thnh phn. Gia tri 5 cho mi thng s.
Overview Of Java Language / Chapter 1/

FlowLayout
import javax.swing.*; import java.awt.*; public class FlowLayoutDemo extends JFrame{ public FlowLayoutDemo() { FlowLayout layout = new FlowLayout(); Container c = getContentPane(); c.setLayout(layout); c.add(new JButton("Button 1")); c.add(new JButton("Button 2")); c.add(new JButton("Button 3")); c.add(new JButton("Long-Named Button 4")); c.add(new JButton("5")); } public static void main (String[] args) { FlowLayoutDemo fr = new FlowLayoutDemo(); fr.setDefaultCloseOperation(EXIT_ON_CLOSE); fr.pack(); fr.setVisible(true); } }

Overview Of Java Language / Chapter 1/

BorderLayout
add(Component c, BorderLayout layout) - c: component thm vo container. - layout: c 5 gi tr
PAGE_START PAGE_END LINE_START LINE_END CENTER

Nu add() c mt thng s thi thanh phn o se khng hin thi. Mc nh, BorderLayout khng t khong trng gia cc thnh. public BorderLayout(int horizontalGap, int verticalGap)
Overview Of Java Language / Chapter 1/

BorderLayout
import java.awt.*; import javax.swing.*; public class BorderLayoutTest { public static void main(String args[]) { JFrame frame = new JFrame("Border Layout"); Container contentPane = frame.getContentPane(); contentPane.setLayout(new BorderLayout()); contentPane.add(new JButton("PAGE_START"), BorderLayout.PAGE_START); contentPane.add(new JButton("PAGE_END"), BorderLayout.PAGE_END); contentPane.add(new JButton("LINE_END"), BorderLayout.LINE_END); contentPane.add(new JButton("LINE_START"), BorderLayout.LINE_START); contentPane.add(new JButton("Center"), BorderLayout.CENTER); frame.setSize(300, 200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.show(); } }

Overview Of Java Language / Chapter 1/

GridLayout
Lp GridLayout to mt i tng c nhiu ct va nhiu hng.
public GridLayout(int rows, int columns) public GridLayout(int rows, int columns, int horizontalGap, int verticalGap)

Overview Of Java Language / Chapter 1/

GridLayoutDemo.java

Lm vic khng c Layout Manager(Absolute Positioning)


Bn c th t v tr cho cc component m khng cn dng layout manager. Gii php ny c dng xc nh hon ton kch thc v v tr ca component. Mc d c th lam vic m khng cn Layout Manager, bn nn dng Layout Manager nu c th. Layout managers d thay i kch thc ca Container va iu chnh hnh dng ca cc thnh phn phu thuc vo Platform.

Overview Of Java Language / Chapter 1/

V d
import java.awt.*; import javax.swing.*; public class AbsoluteLayoutDemo { public static void addComponentsToPane(Container pane) { pane.setLayout(null); JButton b1 = new JButton("one"); JButton b2 = new JButton("two"); JButton b3 = new JButton("three"); pane.add(b1); pane.add(b2); pane.add(b3); Insets insets = pane.getInsets(); Dimension size = b1.getPreferredSize(); b1.setBounds(25 + insets.left, 5 + insets.top, size.width, size.height); size = b2.getPreferredSize(); b2.setBounds(55 + insets.left, 40 + insets.top, size.width, size.height); size = b3.getPreferredSize(); b3.setBounds(150 + insets.left, 15 + insets.top, size.width + 50, size.height + 20); }

Overview Of Java Language / Chapter 1/

V d
private static void createAndShowGUI() { JFrame frame = new JFrame("AbsoluteLayoutDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); addComponentsToPane(frame.getContentPane()); //Size and display the window. Insets insets = frame.getInsets(); frame.setSize(300 + insets.left + insets.right, 125 + insets.top + insets.bottom); frame.setVisible(true); } public static void main(String[] args) { createAndShowGUI(); } }

Overview Of Java Language / Chapter 1/

You might also like