Fการทำงานกับ Application ส่วนใหญ่เราจะติดต่อผ่าน Frame
Fการสร้าง
JFrame f = new JFrame();
FConstructor
JFrame()
JFrame(String text)
Fเป็นการกำหนดขนาดของปุ่ม Maximize บน Frame
FMaximize เป็นการกำหนดให้ขยายหน้าจอได้
FResizable เป็นการกำหนดให้สามารถขยายหน้าจอไม่ได้
Fวิธีการใช้
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
Fเป็นการจัดวาง component ต่างๆไว้ใน Panel อย่างเป็นสัดส่วนโดยใช้ Layout Manager ใดๆ จากนั้นจึงนำ Panel ไปวางในจอภาพใหญ่อีกทีหนึ่ง
Fการสร้าง
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
FComponent คือส่วนที่ถูกแสดงบนจอภาพ(Container) ตัวอย่างเช่น Button, Label, List,TextArea ฯลฯ ซึ่งเป็นลักษณะ Lightweight
FContainer เป็นคอมโพเน็นต์หนึ่งที่สามารถบรรจุ Component ได้ เช่น JPanel,JFrame
Fปกติจะใช้ Method
add(Component com); //วางคอมโพแนนต์
getContentPane(); //Create Obj. ก่อนใช้ add() ได้
JTextField Constructors
FJTextField(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.
FJTextField(String text, int columns)
Creates a text field initialized with the
specified text and the column size.
JTextField Methods
FgetText()
Returns the string from the text field.
FsetText(String text)
Puts the given string in the text field.
FsetEditable(boolean editable)
Enables or disables the text field to be edited. By default, editable is true.
FsetColumns(int)
Sets the number of columns in this text field.
The length of the text field is changeable.
|