Swing Component

Download Report

Transcript Swing Component

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()
1. import java.awt.*;
2. import javax.swing.*;
3. class TestButton extends JFrame{
4.
FlowLayout layout; JButton ok,clear;
5.
TestButton(){
6.
super ("Create FlowLayout");
7.
layout = new FlowLayout();
8.
Container c = getContentPane();
9.
c.setLayout(layout);
10.
ok = new JButton(" OK ");
11.
clear = new JButton();
12.
c.add(ok); c.add(clear);
13.
layout.setAlignment(FlowLayout.RIGHT); }
14.
public static void main(String s[]){
15.
TestButton f = new TestButton();
16.
f.setSize(300,300);
17.
f.setVisible(true);
}
18.}
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()
1. import javax.swing.*;
2. class TestLabel extends JFrame{
3.
FlowLayout layout; JLabel label;
4.
TestLabel(){
5.
super ("Create FlowLayout");
6.
layout = new FlowLayout();
7.
Container c = getContentPane();
8.
c.setLayout(layout);
9.
label = new JLabel("hello"); c.add(label) ;
10.
layout.setAlignment(FlowLayout.RIGHT); }
11. public static void main(String s[]) {
12.
TestLabel f = new TestLabel();
13.
f.setSize(300,300);
14.
f.setVisible(true);
}
15.}
CheckBox
ทาหน้าที่คล้ายกับสวิตช์ปิดเปิ ดหรื อ on – off, yes no ในทุก
ครั้งที่คลิ๊ก ลักษณะของ CheckBox

Check me
Constructor
JCheckBox(String text)
JCheckBox(String text, Icon icon)
RadioButton
คือ กลุ่มของปุ่ มเรดิโอ ซึ่งมีรูปร่ างดังนี้
Button 1
Button 2
Button 3
Button 4
ผูใ้ ช้สามารถเลือกได้เพียงตัวเลือกเดียวเท่านั้น (ภายในเรดิโอกลุ่ม
เดียวกัน)
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)
1. import java.awt.*; import javax.swing.*;
2. class RadioTest1 extends JFrame {
3.
JRadioButton rb1,rb2; JLabel lb1;
4.
FlowLayout layout;
5.
RadioTest1 (){
6.
super("TestRadio") ;
7.
layout = new FlowLayout();
8.
Container c = getContentPane();
9.
c.setLayout(layout);
10.
lb1 = new JLabel("Your Gender is : ");
11.
rb1 = new JRadioButton("Male");
12.
rb2 = new JRadioButton("Female");
13.
c.add(lb1); c.add(rb1); c.add(rb2); }
14.
public static void main(String s[]){
15.
RadioTest1 radio = new RadioTest1();
16.
radio.setSize(250,100);
17.
radio.setVisible(true); }
18.}
RadioTest1.java
ปั ญหาที่พบ
สามารถเลือก Radio ได้หลายตัวเลือก เช่น เพศสามารถเลือกได้ท้ งั
เพศชายและหญิงในเวลาเดียวกันได้ จึงต้องทาการสร้างกลุ่มของตัวเลือก
ขึ้นเพื่อให้สามารถเลือกได้เพียง ตัวเลือกเดียวด้วยคาสัง่
ButtonGroup()
1. import java.awt.*; import javax.swing.*;
2. class RadioTest2 extends JFrame{
3.
JRadioButton rb1,rb2; ButtonGroup myGroup;
4.
JLabel lb1; FlowLayout layout;
5.
RadioTest2 () {
6.
super("TestRadio") ;
7.
layout = new FlowLayout();
8.
Container c = getContentPane();
9.
c.setLayout(layout);
10.
lb1 = new JLabel("Your Gender is : ");
11.
rb1 = new JRadioButton("Male");
12.
rb2 = new JRadioButton("Female");
13.
myGroup = new ButtonGroup();
14.
myGroup.add(rb1); myGroup.add(rb2);
15.
c.add(lb1); c.add(rb1); c.add(rb2);
}
16.
public static void main(String s[]) {
17.
RadioTest2
radio = new RadioTest2();
18.
radio.setSize(250,100); radio.setVisible(true); }
19.}
JTextField & JPasswordField
จะใช้ในการสร้างและรับข้อมูลจากผูใ้ ช้งานโดยตรง โดยที่ผใู ้ ช้งาน
สามารถแก้ไขข้อมูลที่ป้อนเข้าไปได้ดว้ ย แต่มมีขอ้ จากัดว่าสามารถ
ป้ อนเข้าไปได้เพียง 1 บรรทัดเท่านั้น โดยที่ JPasswordField
จะรับข้อมูลเข้ามาแล้วจะแสดงข้อมูลเหล่านั้นด้วย * แทน
Constructor




JTextField()
JTextField(int column)
JTextField(String text)
JTextField(String text,
int column)




JPasswordField()
JPasswordField(int column)
JPasswordField(String text)
JPasswordField(String text,
int column)
1. import java.awt.*; import javax.swing.*;
2. class TestJtextField extends JFrame{
3.
JTextField text; JLabel lb1;
4.
FlowLayout layout;
5.
TestJtextField (){
6.
super("TestRadio") ;
7.
layout = new FlowLayout();
8.
Container c = getContentPane();
9.
c.setLayout(layout);
10.
lb1 = new JLabel("Your name is : ");
11.
text = new JTextField(10);
12.
c.add(lb1); c.add(text); }
13.
public static void main(String s[]) {
14.
TestJtextField radio = new TestJtextField();
15.
radio.setSize(250,100);
16.
radio.setVisible(true); }
17.}
TextArea
จะทาหน้าที่คล้าย JTextField แต่แสดงข้อความได้มากกว่า 1
บรรทัด และมี scroll bar ทั้งในแนวตั้งและแนวนอน
Constructor
JTextArea()
JTextArea(int row,int column)
JTextArea(String text)
JTextArea(String text, int row,int column)