Results 1 to 2 of 2
- 02-26-2010, 09:15 PM #1
Member
- Join Date
- Oct 2009
- Posts
- 26
- Rep Power
- 0
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:
I know my code isn't very standard and that the names aren't correct. This is just a rough draft.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(); } }
Thanks in advance for any help.
~Jamie
- 02-26-2010, 10:17 PM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,158
- Rep Power
- 5
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.I know my code isn't very standard and that the names aren't correct. This is just a rough draft.
Background Panel show a couple of ways you might do this.
Similar Threads
-
Need help with JFrame background image
By ProGenius in forum New To JavaReplies: 6Last Post: 12-27-2009, 04:17 PM -
JFrame window doesnt change background color
By Addez in forum New To JavaReplies: 7Last Post: 11-07-2009, 09:38 PM -
Background in JFrame ( GUI).
By Twister03 in forum AWT / SwingReplies: 2Last Post: 03-12-2009, 03:24 AM -
setting background color of JFrame form with NetBeans 6.1
By onefootswill in forum New To JavaReplies: 4Last Post: 08-12-2008, 07:02 AM -
GUI... setting my background to an image, im using a JFrame
By newtojava7 in forum New To JavaReplies: 2Last Post: 03-24-2008, 05:29 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks