Home / ພື້ນຖານ Desktop app / GUI_Component / Java Button
ສຳລັບ Button ຫຼື JButton (javax.swing.JButton) ຈັດຢູ່ໃນກຸ່ມຂອງ Component ໃຊ້ສຳລັບສ້າງປຸ່ມ Button ແບບ Classic ທຳມະດາ ຫຼືຈະໃຊ້ເປັນຮູບພາບກໍໄດ້ ໂດຍໃນ Button ນີ້ເຮົາສາມາດສ້າງ Event Handler ໄດ້ ເຊັ່ນ Event ທີ່ເກີດຈາກການຄລິກເປັນຕົ້ນ ແລະ ນອກຈາກນີ້ເຮົາຍັງສາມາດໃສ່ພວກຮູບພາບຂອງ icons ລົງໃນ Button ໄດ້ອີກດ້ວຍ
public class MyForm extends JFrame {
public MyForm() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 150, 362, 300);
setTitle("Kittisay Java");
setLayout(null);
// Button 1
JButton btn1 = new JButton("Button 1");
btn1.setBounds(129, 49, 99, 23);
add(btn1);
// Button 2
JButton btn2 = new JButton();
btn2.setText("Button 2");
btn2.setBounds(111, 74, 128, 64);
add(btn2);
// Button 3 (Icon)
JButton btn3 = new JButton(new ImageIcon(getClass().getResource("icon.png")));
btn3.addActionListener((e) -> {
JOptionPane.showMessageDialog(null, "Hello");
});
btn3.setBounds(156, 150, 64, 64);
add(btn3);
}
public static void main(String[] args) {
MyForm frm = new MyForm();
frm.setVisible(true);
}