Snippets‎ > ‎Java Examples‎ > ‎

Hello World Java with Swing GUI

BY MARKUS SPRUNCK


This small example shows a minimal Swing application with one button and message box to say hello.

Example Code

// File 01: HelloSwing .java

package com.sw_engineering_candies.example;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public final class HelloSwing {

public static void main(final String[] args) {
final HelloSwing app = new HelloSwing();
app.buildAndDisplayGui();
}

private void buildAndDisplayGui() {
final JFrame frame = new JFrame("Minimal Swing Application");
buildContent(frame);
frame.setMinimumSize(new Dimension(300, 180));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocation(400, 300);
frame.setVisible(true);
}

private void buildContent(final JFrame aFrame) {
final JPanel panel = new JPanel();
panel.add(new JLabel("Please press the button:"));

final JButton okButton = new JButton("OK");
okButton.addActionListener(new ShowDialog(aFrame));
panel.add(okButton);

aFrame.getContentPane().add(panel);
}

private static final class ShowDialog implements ActionListener {

ShowDialog(final JFrame aFrame) {
fFrame = aFrame;
}

@Override
public void actionPerformed(final ActionEvent aEvent) {
JOptionPane.showMessageDialog(fFrame, "Hello World");
}

private final JFrame fFrame;
}
}

Sponsored Link