Advertisement
Oslapas

Untitled

Nov 29th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.32 KB | None | 0 0
  1. import java.awt.EventQueue;
  2. import java.awt.GridBagConstraints;
  3. import java.awt.GridBagLayout;
  4. import java.awt.image.BufferedImage;
  5. import java.io.File;
  6. import java.io.IOException;
  7. import javax.imageio.ImageIO;
  8. import javax.swing.ImageIcon;
  9. import javax.swing.JButton;
  10. import javax.swing.JFrame;
  11. import javax.swing.JLabel;
  12. import javax.swing.UIManager;
  13. import javax.swing.UnsupportedLookAndFeelException;
  14.  
  15. public class LabelBackground {
  16.  
  17.     public static void main(String[] args) {
  18.         new LabelBackground();
  19.     }
  20.  
  21.     public LabelBackground() {
  22.         EventQueue.invokeLater(new Runnable() {
  23.             @Override
  24.             public void run() {
  25.                 try {
  26.                     UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  27.                 } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
  28.                 }
  29.  
  30.                 try {
  31.                     // Load the background image
  32.                     BufferedImage img = ImageIO.read(new File("/path/to/your/image/on/disk"));
  33.  
  34.                     // Create the frame...
  35.                     JFrame frame = new JFrame("Testing");
  36.                     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  37.  
  38.                     // Set the frames content pane to use a JLabel
  39.                     // whose icon property has been set to use the image
  40.                     // we just loaded                        
  41.                     frame.setContentPane(new JLabel(new ImageIcon(img)));
  42.  
  43.                     // Supply a layout manager for the body of the content
  44.                     frame.setLayout(new GridBagLayout());
  45.                     GridBagConstraints gbc = new GridBagConstraints();
  46.                     gbc.gridwidth = GridBagConstraints.REMAINDER;
  47.                     // Add stuff...
  48.                     frame.add(new JLabel("Hello world"), gbc);
  49.                     frame.add(new JLabel("I'm on top"), gbc);
  50.                     frame.add(new JButton("Clickity-clackity"), gbc);
  51.  
  52.                     frame.pack();
  53.                     frame.setLocationRelativeTo(null);
  54.                     frame.setVisible(true);
  55.                 } catch (IOException exp) {
  56.                     exp.printStackTrace();
  57.                 }
  58.             }
  59.         });
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement