TextField txt = new TextField(); txt.getText(); // get ค่า txt.setText(); // set ค่า
package com.java.myapp; import java.awt.Button; import java.awt.EventQueue; import java.awt.Frame; import java.awt.Label; import java.awt.TextField; import java.awt.event.WindowEvent; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; 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 final Label label = new Label(); label.setAlignment(java.awt.Label.CENTER); label.setText("Result"); label.setBounds(140, 150, 150, 20); add(label); // Text Field final TextField txt = new TextField(); txt.setBounds(135, 60, 150, 20); add(txt); // Button Button button = new Button(); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { label.setText("Hi! : " + txt.getText()); } }); button.setBounds(170, 100, 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 ของทีมงานไทยครีเอท