import javax.swing.*;
public class GuiExample {
public static void main(String[] args) {
createGui();
}
private static void createGui() {
final JFrame frame = new JFrame();
JButton button = new JButton("Click me!");
button.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(frame, "Button clicked!");
}
});
frame.getContentPane().add(button);
frame.setSize(100, 80);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
...
...
...
...
...
...
...
...
...
Answer: It should be executed in the AWT thread!
import javax.swing.*;
public class GuiExample {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
createGui();
}
});
}
...
}
or using the new Java8 syntax:
SwingUtilities.invokeLater(() -> createGui());