4. Java Foundation Class
ระบบปฏบ0ตการส?วนใหญ?จะม,ส?วนตดต?อก0บผM5ใช5เปRนแบบกราฟKก (Graphical
User Interface เร,ยกย?อว?า GUI) ท0งน,เน"องจากใช5งานง?ายกว?า
W W @
ภาษาจาวาจะสน0บสน6นการพ0ฒนาโปรแกรม GUI ท,สามารถใช5งานได5หลาย
@
แพลตฟอร<ม โดยจะใช5ช6ดค>าส0งเด,ยวก0น
@
Java Foundation Class (JFC) ประกอบด5วยแพคเกJจต?างๆด0งน,W
• Abstract Window Toolkit (AWT)
• Swing
• Java 2D
• Accessibility
• Drag and Drop
45. คลาส JLabel
ค1อสวนประกอบกราฟกทใชแสดงขอความยาวหน(งบรรทด ซc@งผM5ใช5ไม?สามารถ
ปwอนข5อความเข5าไปได5
Constructor ของคลาส JLabel ท,@ส>าค0ญม,ด0งน,W
• public JLabel(String text)
• public JLabel(String text, int align)
• public Jlabel(Icon icon)
• public Jlabel(Icon icon, int align)
• public JLabel(String text, Icon icon)
เราสามารถทจะก!าหนดหร1อเปลยนขอความของ Label ไดโดยเรยกใช
เมธอด setText(String text)
46. ตวอยางโปรแกรม JLabel
import java.awt.*;
import java.net.URL;
import javax.swing.*;
public class JLabelDemo {
private JFrame fr;
private JLabel label1, label2, label3;
public void init() {
ImageIcon icon = null;
fr = new JFrame("JLabel Sample");
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
URL imageURL = JLabelDemo.class.getResource("images/testImg.jpg");
if (imageURL != null) {
icon = new ImageIcon(imageURL);
}
fr.setLayout(new GridLayout(3, 1));
//Create the first label.
label1 = new JLabel("Image and Text", icon, JLabel.CENTER);
47. ตวอยางโปรแกรม JLabel
//Set the position of its text, relative to its icon:
label1.setVerticalTextPosition(JLabel.BOTTOM);
label1.setHorizontalTextPosition(JLabel.CENTER);
//Create the other labels.
label2 = new JLabel("Text-Only Label");
label3 = new JLabel(icon);
//Create tool tips, for the heck of it.
label1.setToolTipText("A label containing both image and text");
label2.setToolTipText("A label containing only text");
label3.setToolTipText("A label containing only an image");
//Add the labels.
fr.add(label1);
fr.add(label2);
fr.add(label3)
fr.pack();
fr.setVisible(true);
}
48. ตวอยางโปรแกรม JLabel
public static void main(String args[]) {
JLabelDemo obj = new JLabelDemo();
obj.init();
}
}
ผลล0พธ<ท,@ได5จากการร0นโปรแกรม
49. คลาส JTextField
ค1อสวนประกอบกราฟกเพ1อใหผใชป:อนขอความยาวหน(งบรรทดได
Constructor ของคลาส JTextField ท,@ส>าค0ญม,ด0งน,W
• public JTextField()
• public JTextField(int col)
• public JTextField(String text)
• public JTextField(String text, int col)
เราสามารถทจะก!าหนดหร1อเปลยนขอความโดยใช
เมธอดsetText(String text)
เมธอด setEditable(boolean b)เปRนเมธอดท,ใช5เพ"@อก!าหนดให
@
TextField สามารถอานไดอยางเดยว โดยจะต5องม,การส?ง argument เป"น
false
50. ตวอยางโปรแกรม JTextField
import java.awt.*;
import javax.swing.*;
public class JTextFieldDemo {
private JFrame fr;
private JLabel l;
private JTextField tf;
public void init() {
fr = new JFrame("JTextField Demo");
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
l = new JLabel("Name: ");
tf = new JTextField("Numnonda", 15);
fr.setLayout(new FlowLayout());
fr.add(l);
fr.add(tf);
fr.pack();
51. ตวอยางโปรแกรม JTextField
fr.setVisible(true);
}
public static void main(String args[]) {
JTextFieldDemo obj = new JTextFieldDemo();
obj.init();
}
}
ผลล0พธ<ท,@ได5จากการร0นโปรแกรม
52. คลาส JTextArea
ค1อสวนประกอบกราฟกเพ1อใหผใชป:อนขอความได และสามารถมจ!านวน
บรรทดไดหลายบรรทดตามทระบ2
Constructor ของคลาส TextArea ท,ส>าค0ญม,ด0งน,W
@
•
public JtextArea()
•
public JTextArea(String Text)
• public JTextArea(String Text, int row, int col)
•
public JTextArea(int row, int col))
53. ตวอยางโปรแกรมแสดง JTextArea
import java.awt.*;
import javax.swing.*;
ผลล0พธ<ท,@ได5จากการร0นโปรแกรม
public class JTextAreaDemo {
private JFrame fr;
private JTextArea ta;
public void init() {
fr = new JFrame("JTextArea Demo");
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ta = new JTextArea("Text Area", 5, 30);
JscrollPane jScrollPane = new JScrollPane(ta);
fr.setLayout(new FlowLayout());
fr.add(jScrollPane);
fr.pack();
fr.setVisible(true);
}
public static void main(String args[]) {
JTextAreaDemo obj = new JTextAreaDemo();
obj.init();
}
}
54. คลาส JCheckBox
ค1อสวนประกอบกราฟกทใหผใชเล1อกหร1อไมเล1อกชองตางๆได โดยสามารถ
เล"อกได5หลายช?องพร5อมๆก0น
มขอความ (Label) อยขางๆ
Constructor ของคลาส CJCheckbox ท,ส>าค0ญม,ด0งน,W
@
• public JCheckBox(String label)
• public JCheckBox(String label, boolean state)
• public JCheckBox(Icon icon)
• public JCheckBox(Icon icon, boolean state)
• public JCheckBox(String label, Icon icon)
• public JCheckBox(String label, Icon icon, boolean
state)
70. ตวอยางโปรแกรมแสดง Menu
import javax.swing.*;
public class JMenuDemo {
private JFrame fr;
private JMenuBar mb;
private JMenu m1,m2,m3,m4;
public void init() {
fr = new JFrame("JMenu Demo");
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mb = new JMenuBar();
m1 = new JMenu("File");
m2 = new JMenu("Edit");
m3 = new JMenu("View");
m4 = new JMenu("Help");
71. ตวอยางโปรแกรมแสดง Menu
fr.setJMenuBar(mb);
mb.add(m1);
mb.add(m2);
mb.add(m3); ผลล0พธ<ท,@ได5จากการร0นโปรแกรม
mb.add(m4);
fr.setSize(200,150);
fr.setVisible(true);
}
public static void main(String args[]) {
JMenuDemo mm = new JMenuDemo();
mm.init();
}
}
80. ตวอยางโปรแกรมแสดงเมนMยอย
?
import javax.swing.*;
public class SubmenuDemo {
private JFrame fr;
private JMenuBar mb;
private JMenu m1, m2, m3, ms1;
private JMenuItem mi2, mi3, mi4, msi1, msi2;
public void init() {
fr = new JFrame("SubMenuItem Demo");
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mb = new JMenuBar();
m1 = new JMenu("File");
m2 = new JMenu("Edit");
m3 = new JMenu("View");
fr.setJMenuBar(mb);
mb.add(m1);
mb.add(m2);
mb.add(m3);
81. ตวอยางโปรแกรมแสดงเมนMยอย
?
ms1 = new JMenu("New");
mi2 = new JMenuItem("Open");
mi3 = new JMenuItem("Save");
mi4 = new JMenuItem("Exit");
m1.add(ms1);
m1.add(mi2);
m1.addSeparator();
m1.add(mi3);
m1.addSeparator();
m1.add(mi4);
msi1 = new JMenuItem("Window");
msi2 = new JMenuItem("Message");
ms1.add(msi1);
ms1.addSeparator();
ms1.add(msi2);
fr.setSize(200, 200);
fr.setVisible(true);
}
82. ตวอยางโปรแกรมแสดงเมนMยอย
?
public static void main(String args[]) {
SubmenuDemo obj = new SubmenuDemo();
obj.init();
}
}
ผลล0พธ<ท,@ได5จากการร0นโปรแกรม
83. ค2ณลกษณะของคลาส Component
ส?วนประกอบกราฟKกต?างๆจะม,ค6ณล0กษณะอ"@นอาทเช?น รMปแบบของฟอนต< ส,
ของพ"นหล0งหร"อส,ของพ"นหน5า (Foreground) เราสามารถท,@จะก>าหนด
W W
ค6ณล0กษณะของส?วนประกอบกราฟKกได5 โดยปกตส?วนประกอบกราฟKกจะใช5
ค6ณล0กษณะแบบเด,ยวก0บออปเจJคประเภท Container ท,บรรจ6อยM?เว5นแต?จะม,
@
การก>าหนดค6ณล0กษณะเฉพาะของส?วนประกอบกราฟKกน0นๆ W
เมธอดท,@ใช5ในการก>าหนดค6ณล0กษณะของส?วนประกอบกราฟKก จะอยMใน
?
คลาส Component โดยม,เมธอดท,@ส>าค0ญค"อ
• setFont(Font f)
• setForeground(Color c)
• setBackground(Color c)
84. คลาส Font
เราสามารถสร5างออปเจJคของคลาส Font เพ"@อใช5ในการก>าหนดฟอนต<ได5
Constructor ของคลาส Font ท,ส>าค0ญม,ด0งน,W
@
• public Font(String name,int style,int size)
— name ค1อช1อฟอนต3
— style ค1อรปแบบของฟอนต3 เชน Font.PLAIN,Font.BOLD,
Font.ITALIC
— size ค1อขนาดของฟอนต3
เราสามารถก!าหนดฟอนต3ใหกบออปเจคของคลาส Component โดยใช
เมธอด setFont()
85. คลาส Color
เราสามารถสร5างออปเจJคของคลาส Color เพ"อใช5ในการก>าหนดส,ได5
@
Constructor ของคลาส Color ท,@ส>าค0ญม,ด0งน,W
• public Color(int r,int g,int b)
— โดยท,@ r,g,b ค"อค?าความเข5มของแสงส,แดง เข,ยว และนW>าเงน ตามล>าด0บ
ต0วอย?างเช?น
• yellow = new Color(255,255,0);
• black = new Color(0,0,0);
86. ตวอยางโปรแกรมแสดงค6ณล0กษณะ
import java.awt.*;
import javax.swing.*;
public class AttributeDemo {
private JFrame fr;
private JButton bn1, bn2, bn3;
public void init() {
fr = new Jframe("ShowAttribute");
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
bn1 = new JButton("OK");
bn2 = new JButton("Cancel");
bn3 = new JButton("Help");
fr.setLayout(new FlowLayout());
fr.add(bn1);
fr.add(bn2);
fr.add(bn3);
87. ตวอยางโปรแกรมแสดงค6ณล0กษณะ
bn2.setFont(new Font("TimesRoman", Font.BOLD, 16));
fr.getContentPane().setBackground(Color.blue);
bn2.setForeground(Color.red);
fr.setSize(200, 150);
fr.setVisible(true);
}
public static void main(String args[]) {
AttributeDemo ad = new AttributeDemo();
ad.init();
}
}
ผลล0พธ<ท,@ได5จากการร0นโปรแกรม