180 likes | 294 Vues
This guide provides an overview of basic Swing components in Java, including JButton, JLabel, JCheckBox, and JRadioButton. Each component's functionality and constructors are explained. You'll learn how to create buttons, labels, checkboxes, and radio buttons, along with examples of their usage. This resource is essential for anyone looking to build user interfaces with Java Swing, offering practical insights into component creation and layout management in graphical applications.
E N D
Swing Component Basic Component
Swing Component พื้นฐาน • JButton • JLabel • JCheckBox • JRadioButton • JTextField & JPasswordField • JTextArea
JButton • ใช้ในการสร้างปุ่มขึ้นมาใช้งานโดยปุ่มที่สร้างนั้นมีอยู่ 3 ประเภท คือ • JButton ได้แก่ปุ่มทั่ว ๆ ไป • JToggkeButton ใช้ในการสร้างปุ่มที่ให้ผู้ใช้เลือกแบบ on หรือ off • JmenuItem ใช้ในการสร้างปุ่มที่ให้ผู้ใช้เลือกแบบ on หรือ off เฉพาะภายในเมนู
Constructor • JButton()Aใช้ในการสร้างปุ่มว่าง ๆ ไม่มีข้อความอะไร • JButton(String text) ใช้ในการสร้างปุ่มมีข้อความ text • JButton(Icon icon) ใช้ในการสร้างปุ่มที่มีไอคอน • JButton(String text, Icon icon) ใช้ในการสร้างปุ่มที่มีข้อความ text และ ไอคอน • การสร้าง JButton ชื่อวัตถุ = new JButton()
import java.awt.*; • import javax.swing.*; • class TestButton extends JFrame{ • FlowLayout layout; JButton ok,clear; • TestButton(){ • super ("Create FlowLayout"); • layout = new FlowLayout(); • Container c = getContentPane(); • c.setLayout(layout); • ok = new JButton(" OK "); • clear = new JButton(); • c.add(ok); c.add(clear); • layout.setAlignment(FlowLayout.RIGHT);} • public static void main(String s[]){ • TestButton f = new TestButton(); • f.setSize(300,300); • f.setVisible(true); } • }
JLabel • ใช้ในการแสดงข้อความหรือรูปภาพ Icon ใด ๆ บนจอภาพ โดยที่ข้อความนั้นจะไม่สามารถแก้ไขได้ (Read only Tex;) Constructor • JLabel() • JLabel(Icon icon) • JLabel(Icon icon, int HAlight) • JLabel(String text) • JLabel(String text, Icon icon, int HAlight) • JLabel(String text, int HAlight)
Method • setText(“Text1”) ใช้ในการเปลี่ยนข้อความของ Label ที่สร้างไว้แล้ว มีรูปแบบการใช้งานคือ • ชื่อ Label.setText(“ข้อความใหม่”) • getText()A • setTookTipText()
import javax.swing.*; • class TestLabel extends JFrame{ • FlowLayout layout;JLabel label; • TestLabel(){ • super ("Create FlowLayout"); • layout = new FlowLayout(); • Container c = getContentPane(); • c.setLayout(layout); • label = new JLabel("hello");c.add(label) ; • layout.setAlignment(FlowLayout.RIGHT);} • public static void main(String s[]) { • TestLabel f = new TestLabel(); • f.setSize(300,300); • f.setVisible(true); } • }
CheckBox • ทำหน้าที่คล้ายกับสวิตช์ปิดเปิดหรือ on – off, yes no ในทุกครั้งที่คลิ๊ก ลักษณะของ CheckBox Constructor • JCheckBox(String text) • JCheckBox(String text, Icon icon) Check me
Button 2 Button 1 Button 4 Button 3 RadioButton • คือ กลุ่มของปุ่มเรดิโอ ซึ่งมีรูปร่างดังนี้ • ผู้ใช้สามารถเลือกได้เพียงตัวเลือกเดียวเท่านั้น (ภายในเรดิโอกลุ่มเดียวกัน)
Constructor • 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)
import java.awt.*; import javax.swing.*; • class RadioTest1 extends JFrame { • JRadioButton rb1,rb2; JLabel lb1; • FlowLayout layout; • RadioTest1 (){ • super("TestRadio") ; • layout = new FlowLayout(); • Container c = getContentPane(); • c.setLayout(layout); • lb1 = new JLabel("Your Gender is : "); • rb1 = new JRadioButton("Male"); • rb2 = new JRadioButton("Female"); • c.add(lb1); c.add(rb1); c.add(rb2); } • public static void main(String s[]){ • RadioTest1 radio = new RadioTest1(); • radio.setSize(250,100); • radio.setVisible(true); } • } RadioTest1.java
ปัญหาที่พบ • สามารถเลือก Radio ได้หลายตัวเลือก เช่น เพศสามารถเลือกได้ทั้งเพศชายและหญิงในเวลาเดียวกันได้ จึงต้องทำการสร้างกลุ่มของตัวเลือกขึ้นเพื่อให้สามารถเลือกได้เพียง ตัวเลือกเดียวด้วยคำสั่ง ButtonGroup()
import java.awt.*; import javax.swing.*; • class RadioTest2 extends JFrame{ • JRadioButton rb1,rb2; ButtonGroup myGroup; • JLabel lb1; FlowLayout layout; • RadioTest2 () { • super("TestRadio") ; • layout = new FlowLayout(); • Container c = getContentPane(); • c.setLayout(layout); • lb1 = new JLabel("Your Gender is : "); • rb1 = new JRadioButton("Male"); • rb2 = new JRadioButton("Female"); • myGroup = new ButtonGroup(); • myGroup.add(rb1); myGroup.add(rb2); • c.add(lb1); c.add(rb1); c.add(rb2); } • public static void main(String s[]) { • RadioTest2 radio = new RadioTest2(); • radio.setSize(250,100); radio.setVisible(true); } • }
JTextField & JPasswordField • จะใช้ในการสร้างและรับข้อมูลจากผู้ใช้งานโดยตรง โดยที่ผู้ใช้งานสามารถแก้ไขข้อมูลที่ป้อนเข้าไปได้ด้วย แต่มมีข้อจำกัดว่าสามารถป้อนเข้าไปได้เพียง 1 บรรทัดเท่านั้น โดยที่ JPasswordField จะรับข้อมูลเข้ามาแล้วจะแสดงข้อมูลเหล่านั้นด้วย * แทน
JTextField() JTextField(int column) JTextField(String text) JTextField(String text, int column) JPasswordField() JPasswordField(int column) JPasswordField(String text) JPasswordField(String text, int column) Constructor
import java.awt.*; import javax.swing.*; • class TestJtextField extends JFrame{ • JTextField text; JLabel lb1; • FlowLayout layout; • TestJtextField (){ • super("TestRadio") ; • layout = new FlowLayout(); • Container c = getContentPane(); • c.setLayout(layout); • lb1 = new JLabel("Your name is : "); • text = new JTextField(10); • c.add(lb1); c.add(text); } • public static void main(String s[]) { • TestJtextField radio = new TestJtextField(); • radio.setSize(250,100); • radio.setVisible(true); } • }
TextArea • จะทำหน้าที่คล้าย JTextField แต่แสดงข้อความได้มากกว่า 1 บรรทัด และมี scroll bar ทั้งในแนวตั้งและแนวนอน Constructor • JTextArea() • JTextArea(int row,int column) • JTextArea(String text) • JTextArea(String text, int row,int column)