How do you create a simple GUI in Java

Creating a simple GUI in Java can be accomplished using the Swing framework, which provides a set of components for building graphical user interfaces. Below is an example of a basic Java application that creates a window with a button. When the button is clicked, a message is displayed.

import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; public class SimpleGUI { public static void main(String[] args) { JFrame frame = new JFrame("Simple GUI Example"); JPanel panel = new JPanel(); JButton button = new JButton("Click Me!"); button.addActionListener(e -> JOptionPane.showMessageDialog(frame, "Button clicked!")); panel.add(button); frame.add(panel); frame.setSize(300, 200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }

Java GUI Swing JFrame JButton JPanel ActionListener JOptionPane