Button button = new Button(); // Event Handler button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { // Event } });
package com.java.myapp; import java.awt.Button; import java.awt.EventQueue; import java.awt.Frame; import java.awt.Label; import java.awt.event.WindowEvent; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JOptionPane; public class MyForm extends Frame { public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { MyForm form = new MyForm(); form.setVisible(true); } }); } public MyForm() { // Create Form Frame super("ThaiCreate.Com Java GUI Tutorial"); setSize(434, 256); setLocation(500, 280); setLayout(null); // Label Label label = new Label(); label.setAlignment(java.awt.Label.CENTER); label.setText("ThaiCreate.Com"); label.setBounds(140, 100, 150, 20); add(label); // Button Button button = new Button(); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { JOptionPane.showMessageDialog(null, "Great Java Tutorial!", "Hey!", JOptionPane.WARNING_MESSAGE); } }); button.setBounds(170, 150, 90, 24); button.setLabel("Submit"); add(button); // Close addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(WindowEvent evt) { System.exit(0); } }); } }
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท