Swing Set

ทำหน้าที่สร้างส่วนติดต่อกับผู้ใช้(User Interface)
พัฒนาเพื่อใช้แทน AWT(Abstract Window Toolkit) ให้มีความหลากหลายและใช้งานง่ายขึ้นกว่าเดิม เรียกว่าชุด JFC(Java Foundation Classes) ประกอบด้วย
- AWT API
- Swing API
- Java 2D API
- Accessibility API
- Drag and Drop API
เป็นมาตรฐานในการใช้งานในรุ่น JDK1.2 หรือ Java2
Frame
การทำงานกับ Application ส่วนใหญ่เราจะติดต่อผ่าน Frame
การสร้าง
JFrame f = new JFrame();
Constructor
JFrame()
JFrame(String text)
Maximized and Resizable
เป็นการกำหนดขนาดของปุ่ม Maximize บน Frame
Maximize เป็นการกำหนดให้ขยายหน้าจอได้
Resizable เป็นการกำหนดให้สามารถขยายหน้าจอไม่ได้
วิธีการใช้
Obj.setMaximizedBounds(Rectangle bounds);
Obj.setResizable(boolean resizable);
Creating Frames
import javax.swing.*;
public class MyFrame {
  public static void main(String[] args) {
    JFrame frame = new JFrame("Test Frame");
    frame.setSize(400, 300);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(
      JFrame.EXIT_ON_CLOSE);
  }
   }
Panel
เป็นการจัดวาง component ต่างๆไว้ใน Panel อย่างเป็นสัดส่วนโดยใช้ Layout Manager ใดๆ จากนั้นจึงนำ Panel ไปวางในจอภาพใหญ่อีกทีหนึ่ง
การสร้าง
JPanel p = new JPanel();
Creating a JPanel
You can use new JPanel() to create a panel with a default FlowLayout manager or new JPanel(LayoutManager) to create a panel with the specified layout manager. Use the add(Component) method to add a component to the panel. For example,
JPanel p = new JPanel();
p.add(new JButton("OK"));
Component
Component คือส่วนที่ถูกแสดงบนจอภาพ(Container) ตัวอย่างเช่น Button, Label, List,TextArea ฯลฯ ซึ่งเป็นลักษณะ Lightweight
Container เป็นคอมโพเน็นต์หนึ่งที่สามารถบรรจุ Component ได้ เช่น JPanel,JFrame
ปกติจะใช้ Method
add(Component com); //วางคอมโพแนนต์
getContentPane(); //Create Obj. ก่อนใช้ add() ได้
 JTextField Constructors
JTextField(int columns)
Creates an empty text field with the specified number of columns.
FJTextField(String text)
Creates a text field initialized with the specified text.
JTextField(String text, int columns)
Creates a text field initialized with the
specified text and the column size.
JTextField Methods
getText()
Returns the string from the text field.
setText(String text)
Puts the given string in the text field.
setEditable(boolean editable)
Enables or disables the text field to be edited. By default, editable is true.
setColumns(int)
Sets the number of columns in this text field.
The length of the text field is changeable. 

กลับไปหน้าจาวา

 

หน้าหลัก บทเรียน ประวัติ เพื่อน