dimanche 22 février 2015

Add a background to a JFrame



I want to add a background to my JFrame, but I can't get it done. I have searched on the internet to find to some tutorials on it. I am still a newbie and I want to learn these things. This is what I have so far.



package gui;

import java.awt.Desktop;

public class Gui extends JFrame {

private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JLabel BackgroundLabel;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Gui frame = new Gui();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public Gui() {
setTitle("Exile Launcher");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 1000, 563);
contentPane = new JPanel();
contentPane.setBorder(null);
setContentPane(contentPane);
contentPane.setLayout(null);

BufferedImage BackgroundImage = null;
try {
BackgroundImage = ImageIO.read(this.getClass().getResource("/Images/Background.jpg"));
} catch (IOException ex) {
}

JLabel BackgroundLabel = new JLabel(new ImageIcon(BackgroundImage));

add(BackgroundLabel);

JButton HomeButton = new JButton("Home");
HomeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Desktop.getDesktop().browse(new URL("http://www.google.nl").toURI());
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (URISyntaxException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});

HomeButton.setBounds(10, 9, 50, 50);
contentPane.add(HomeButton);

JButton ForumButton = new JButton("Vote");
ForumButton.setBounds(10, 70, 50, 50);
contentPane.add(ForumButton);

JButton VoteButton = new JButton("New button");
VoteButton.setBounds(10, 131, 50, 50);
contentPane.add(VoteButton);
}
}


But I get this error:



java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(ImageIO.java:1388)
at gui.Gui.<init>(Gui.java:59)
at gui.Gui$1.run(Gui.java:36)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:312)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)



Aucun commentaire:

Enregistrer un commentaire