fork download
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5.  
  6. class SimpleAWTAndSwingExample {
  7. public static void main(String[] args) {
  8. // Use SwingUtilities to ensure that GUI is created on the Event Dispatch Thread
  9. SwingUtilities.invokeLater(() -> {
  10. // Create a new JFrame
  11. JFrame frame = new JFrame("AWT and Swing Example");
  12. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  13.  
  14. // Create a panel to hold components
  15. JPanel panel = new JPanel();
  16. panel.setLayout(new FlowLayout()); // Set layout manager
  17.  
  18. // Create a label
  19. JLabel label = new JLabel("Click the button!");
  20.  
  21. // Create a button
  22. JButton button = new JButton("Click Me!");
  23.  
  24. // Add an ActionListener to the button
  25. button.addActionListener(new ActionListener() {
  26. @Override
  27. public void actionPerformed(ActionEvent e) {
  28. label.setText("Button Clicked!");
  29. }
  30. });
  31.  
  32. // Add the label and button to the panel
  33. panel.add(label);
  34. panel.add(button);
  35.  
  36. // Add the panel to the frame
  37. frame.add(panel);
  38.  
  39. // Pack the frame to fit the preferred sizes of its components
  40. frame.pack();
  41.  
  42. // Set the frame location to center of the screen
  43. frame.setLocationRelativeTo(null);
  44.  
  45. // Set the frame to be visible
  46. frame.setVisible(true);
  47. });
  48. }
  49. }
Success #stdin #stdout #stderr 0.27s 67076KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Exception in thread "AWT-EventQueue-0" java.awt.HeadlessException: 
No X11 DISPLAY variable was set, but this program performed an operation which requires it.
	at java.desktop/java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:197)
	at java.desktop/java.awt.Window.<init>(Window.java:538)
	at java.desktop/java.awt.Frame.<init>(Frame.java:423)
	at java.desktop/javax.swing.JFrame.<init>(JFrame.java:224)
	at SimpleAWTAndSwingExample.lambda$main$0(Main.java:11)
	at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:313)
	at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770)
	at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
	at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
	at java.base/java.security.AccessController.doPrivileged(AccessController.java:389)
	at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
	at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:740)
	at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
	at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)