| 1. การแสดง Frame 4 แบบ ผ่าน AWT : Abstract Windows Toolkit :: แบบที่ 1 เรียกผ่าน main ที่ต้องมี static
 :: แบบที่ 2 เรียกผ่าน init ที่ไม่ต้องมี static
 :: แบบที่ 3 สืบทอด Frame เพื่อให้ paint ทำงาน
 
// โปรแกรมแบบใช้ Abstract Window Toolkit
// แบบที่ 1 เรียกผ่าน main ที่ต้องมี static
// ถ้าไม่มี setLayout จะได้ปุ่มเต็ม frame
// หากทำงานใน main จะใช้ this ไม่ได้ ต้องสร้าง instance ขึ้นมาใหม่
import java.awt.*;
import java.awt.event.*;
public class x implements ActionListener {
  static Button b1 = new Button("exit");
  // 01 - main
  public static void main(String[] args) {
    Frame s = new Frame("Screen");
    s.setSize(150,150);
    s.setLayout(null);
    s.setVisible(true);   
    s.add(b1);
    b1.setBounds(10,40,70,20);
    x myclass = new x();
    b1.addActionListener(myclass);
  }
  // 02 - actionPerformed
  public void actionPerformed(ActionEvent a) {
    if(a.getSource()==b1) { System.exit(0); }
  }
}
// แบบที่ 2 เรียกผ่าน init ที่ไม่ต้องมี static
// ไม่ต้องสร้าง instance ใน main เหมือนแบบแรก
// ถ้าสร้าง instance จะทำให้ b1 ใน actionPerformed เทียบไม่ตรง
import java.awt.*;
import java.awt.event.*;
public class x implements ActionListener {
  Button b1 = new Button("exit");
  // 01 - main
  public static void main(String[] args) {
    new x().init();
  }
  // 02 - init
  public void init() {
    Frame s = new Frame("Screen");
    s.setSize(150,150);
    s.setLayout(null);
    s.setVisible(true);   
    s.add(b1);
    b1.setBounds(10,40,70,20);
    b1.addActionListener(this);
  }
  // 03 - actionPerformed
  public void actionPerformed(ActionEvent a) {
    if(a.getSource()==b1) { System.exit(0); }
  }
}
 // แบบที่ 3 สืบทอด Frame เพื่อแสดง paint ไม่มี ActionListener
// กดปุ่ม exit จาก close button ได้
import java.awt.*;
import java.awt.event.*;
public class x extends Frame{
  static x frame = new x();
  // 01 - main
  public static void main(String[] args) {
    frame.init();
  }
  // 02 - init
  public void init() {
    frame.setSize(300,300);
    frame.setLayout(null);
    frame.setVisible(true);   
    frame.addWindowListener(new WindowAdapter(){
      public void windowClosing(WindowEvent e){
        System.exit(0);
      }
    });
  }
  // 03 - paint
  public void paint(Graphics g) {
    Graphics2D p = (Graphics2D)g;
    p.drawOval(30 ,150,100,100);
  }   
}
 // แบบที่ 4 สืบทอด Frame เพื่อแสดง paint และมี ActionListener
// เป็นการทำงาน 2 ส่วนคือแสดงผลกราฟฟิก และควบคุมการทำงานของวัตถุ AWT
import java.awt.*;
import java.awt.event.*;
public class x extends Frame implements ActionListener{
  Button b1 = new Button("exit");
  // 01 - paint
  public void paint(Graphics g) {
    Graphics2D p = (Graphics2D)g;
    p.drawOval(30,150,100,100);
  }               
  // 02 - main
  public static void main(String args[]) {     
    new x().init();
  }
  // 03 - init
  public void init() {
    x frame = new x();
    frame.setSize(400, 400);
    frame.setLayout(null);
    frame.setVisible(true);
    frame.add(b1);
    b1.setBounds(10,40,70,20);
    b1.addActionListener(this);
  }
  // 04 - actionPerformed
  public void actionPerformed(ActionEvent a) {
    if(a.getSource()==b1)  { System.exit(0); }
  }
}
 |