package com.java.myapp;
import java.awt.EventQueue;
import javax.swing.JFrame;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import javax.swing.JComboBox;
public class MyForm extends JFrame {
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
MyForm frame = new MyForm();
frame.setVisible(true);
}
});
}
/**
* Create the frame.
*/
public MyForm() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 343, 273);
setTitle("ThaiCreate.Com Java GUI Tutorial");
getContentPane().setLayout(null);
// Combobox
String[] petStrings = { "Bird", "Cat", "Dog", "Rabbit", "Pig" };
final JComboBox comboBox = new JComboBox(petStrings);
comboBox.setSelectedIndex(4);
comboBox.setBounds(113, 76, 107, 20);
getContentPane().add(comboBox);
// Button
JButton btn = new JButton("Button");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(null,
"You selected : " +comboBox.getSelectedItem());
}
});
btn.setBounds(126, 123, 81, 23);
getContentPane().add(btn);
}
}