import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class MyGUI {
public static void main(String[] args) {
JFrame frame = new JFrame("My First GUI");
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create components
JLabel label = new JLabel("Welcome to GUI Programming!");
JButton button = new JButton("Click me!");
// Add components to the frame
frame.setLayout(new FlowLayout()); // Set layout manager
frame.add(label);
frame.add(button);
// Add ActionListener to button
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(frame, "Button clicked!");
}
});
frame.setVisible(true);
}
}