Results 1 to 2 of 2
  1. #1
    dunafrothint is offline Member
    Join Date
    Oct 2009
    Posts
    26
    Rep Power
    0

    Default How to add a background image to JFrame

    I know I can add the image using a panel and adding an ImageIcon to the LayeredPane, but that overwrites my swing components, except for my top button.

    Here's my code:

    Java Code:
    package platinum;
    import java.awt.Color;
    import javax.swing.*;
    import static javax.swing.GroupLayout.Alignment.*;
    import java.awt.event.*;
    public class LogIn {
        JFrame frame = new JFrame();
        JButton Click = new JButton("Sign In"), Hiya = new JButton("Exit");
        JTextField Text = new JTextField(1), Text2 = new JTextField(1);
        JLabel Username = new JLabel("Username"), Password = new JLabel("Password");
        GroupLayout layout= new GroupLayout(frame.getContentPane());
        public LogIn(){
            frame.getContentPane().setLayout(layout);
            layout.setAutoCreateContainerGaps(true);
            layout.setHorizontalGroup(
                layout.createParallelGroup()
                .addGroup(layout.createSequentialGroup()
                    .addGap(20)
                    .addGroup(layout.createParallelGroup(LEADING)
                        .addComponent(Username)
                        .addComponent(Password))
                    .addGap(20)
                    .addGroup(layout.createParallelGroup(LEADING)
                        .addComponent(Hiya)
                        .addComponent(Click)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(10)
                            .addGroup(layout.createParallelGroup(LEADING)
                                .addComponent(Text, Click.getPreferredSize()
                                .width-20, Click.getPreferredSize()
                                .width-20, Click.getPreferredSize().width-20)
                                .addComponent(Text2)))).addGap(20))
            );
            layout.linkSize(SwingConstants.HORIZONTAL, Hiya, Click);
            layout.linkSize(SwingConstants.HORIZONTAL, Text, Text2);
            layout.setVerticalGroup(
                layout.createParallelGroup(LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(Hiya)
                    .addGap(20)
                    .addGroup(layout.createParallelGroup(LEADING)
                    .addComponent(Username)
                    .addComponent(Text))
                    .addGap(20)
                    .addGroup(layout.createParallelGroup(LEADING)
                    .addComponent(Password)
                    .addComponent(Text2))
                    .addGap(20)
                    .addComponent(Click))
            );
            Hiya.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    frame.dispose();
                }
            });
            Click.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    Validation val = new Validation();
                    if (val.Username.equals(Text.getText()) && val.Password.equals(Text2.getText())) System.out.println(val.Username + " has just signed in");
                }
            });
            frame.setUndecorated(true);
            Username.setForeground(Color.white);
            Password.setForeground(Color.white);
            frame.getContentPane().setBackground(Color.black);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }
        public static void main(String args[]){
            new LogIn();
        }
    }
    I know my code isn't very standard and that the names aren't correct. This is just a rough draft.

    Thanks in advance for any help.

    ~Jamie

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    I know my code isn't very standard and that the names aren't correct. This is just a rough draft.
    What does a rough draft have to do with anything. It take no more time to use proper names that it does to use improper names. In fact it takes less time because you don't have to go back and change them. If you want us to take the time to look at your code, then you take the time to make it as readable as possible.

    Background Panel show a couple of ways you might do this.

Similar Threads

  1. Need help with JFrame background image
    By ProGenius in forum New To Java
    Replies: 6
    Last Post: 12-27-2009, 04:17 PM
  2. JFrame window doesnt change background color
    By Addez in forum New To Java
    Replies: 7
    Last Post: 11-07-2009, 09:38 PM
  3. Background in JFrame ( GUI).
    By Twister03 in forum AWT / Swing
    Replies: 2
    Last Post: 03-12-2009, 03:24 AM
  4. setting background color of JFrame form with NetBeans 6.1
    By onefootswill in forum New To Java
    Replies: 4
    Last Post: 08-12-2008, 07:02 AM
  5. Replies: 2
    Last Post: 03-24-2008, 05:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •