Advertisement
Oslapas

Untitled

Nov 29th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.10 KB | None | 0 0
  1.  import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. class BackgroundImageJFrame extends JFrame
  5. {
  6. JButton b1;
  7. JLabel l1;
  8.     public BackgroundImageJFrame()
  9.     {
  10.     setTitle("Background Color for JFrame");
  11.     setSize(400,400);
  12.     setLocationRelativeTo(null);
  13.     setDefaultCloseOperation(EXIT_ON_CLOSE);
  14.     setVisible(true);
  15. /*
  16.     One way
  17.     -----------------
  18.     setLayout(new BorderLayout());
  19.     JLabel background=new JLabel(new ImageIcon("C:\\Users\\Computer\\Downloads\\colorful design.png"));
  20.     add(background);
  21.     background.setLayout(new FlowLayout());
  22.     l1=new JLabel("Here is a button");
  23.     b1=new JButton("I am a button");
  24.     background.add(l1);
  25.     background.add(b1);
  26. */
  27. // Another way
  28.     setLayout(new BorderLayout());
  29.     setContentPane(new JLabel(new ImageIcon("C:\\Users\\Computer\\Downloads\\colorful design.png")));
  30.     setLayout(new FlowLayout());
  31.     l1=new JLabel("Here is a button");
  32.     b1=new JButton("I am a button");
  33.     add(l1);
  34.     add(b1);
  35.     // Just for refresh :) Not optional!
  36.     setSize(399,399);
  37.     setSize(400,400);
  38.     }
  39.     public static void main(String args[])
  40.     {
  41.     new BackgroundImageJFrame();
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement