Checkbox chk = new Checkbox(); chk.getState() // true or false
package com.java.myapp; import java.awt.Button; import java.awt.Checkbox; import java.awt.EventQueue; import java.awt.Frame; import java.awt.Label; import java.awt.TextArea; 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, 285); setLocation(500, 280); setLayout(null); // Label final Label label = new Label(); label.setAlignment(java.awt.Label.CENTER); label.setText("Result"); label.setBounds(140, 220, 150, 20); add(label); // Checkbox final Checkbox chk = new Checkbox(); chk.setLabel("Checkbox 1"); chk.setBounds(180, 120, 90, 20); add(chk); // Button Button button = new Button(); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { if(chk.getState()) { label.setText("Checkbox is Checked"); } else { label.setText("Checkbox UnChecked"); } } }); button.setBounds(170, 170, 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 ของทีมงานไทยครีเอท