Advertisement
heroofhyla

Untitled

Aug 19th, 2015
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.21 KB | None | 0 0
  1. package com.aezart.simpleswingexample;
  2.  
  3. import java.awt.Dimension;
  4. import java.awt.FlowLayout;
  5. import java.awt.event.ActionEvent;
  6.  
  7. import javax.swing.AbstractAction;
  8. import javax.swing.JButton;
  9. import javax.swing.JFrame;
  10. import javax.swing.JOptionPane;
  11. import javax.swing.SwingUtilities;
  12.  
  13. public class SimpleSwingExample {
  14.     JFrame mainWindow = new JFrame();
  15.     JButton helloButton = new JButton(new HelloAction());
  16.    
  17.     class HelloAction extends AbstractAction{
  18.         public HelloAction(){
  19.             super("Click me!");
  20.         }
  21.         @Override
  22.         public void actionPerformed(ActionEvent arg0) {
  23.             JOptionPane.showMessageDialog(mainWindow, "Hello, World.", "ALERT", JOptionPane.INFORMATION_MESSAGE);
  24.         }
  25.     }
  26.    
  27.     public SimpleSwingExample(){
  28.         mainWindow.setTitle("Simple Swing Example");
  29.         mainWindow.setPreferredSize(new Dimension(320,240));
  30.         mainWindow.setLayout(new FlowLayout());
  31.         mainWindow.add(helloButton);
  32.         mainWindow.pack();
  33.         mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  34.         mainWindow.setLocationRelativeTo(null);
  35.         mainWindow.setVisible(true);
  36.     }
  37.    
  38.     public static void main(String[] args){
  39.         SwingUtilities.invokeLater(() -> {
  40.             SimpleSwingExample s = new SimpleSwingExample();
  41.         });
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement