How do i set a background image to an extended (2 class) JFrame program?
I need some help to make an image a background for a JFrame.
I tried researching and i found some ways like using the graphics g but i am not sure what class i place it in.
thanks in advance!
here is the first class of my code.
------------------------------------------
Code :
package classes;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class rohwcg extends JFrame
{
// adds the buttons
private JButton minerbutton;
private JButton farmerbutton;
private JButton lumberjackbutton;
private JButton blacksmithbutton;
public rohwcg()
{
super ("Realms of Havenwood Class Guide");
this.getContentPane().setBackground(Color.black);
this.setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
Icon mbutton = new ImageIcon (getClass() .getResource("miner.png"));
minerbutton = new JButton(mbutton);
add(minerbutton);
//farmer button
Icon fbutton = new ImageIcon (getClass() .getResource("farmer.png"));
farmerbutton = new JButton(fbutton);
add(farmerbutton);
//lumberJack button
Icon lbutton = new ImageIcon (getClass() .getResource("lumberjack.png"));
lumberjackbutton = new JButton(lbutton);
add(lumberjackbutton);
//blacksmith button
Icon bbutton = new ImageIcon (getClass() .getResource("blacksmith.png"));
blacksmithbutton = new JButton(bbutton);
add(blacksmithbutton);
I didn't paste all of it because it wasn't needed.
--------------------------------------------------
2nd class
--------------------------------------------------
Code :
package classes;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
public class thehandler {
public static void main(String args [])
{
rohwcg classes1 = new rohwcg();
classes1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
classes1.setSize(700,300);
classes1.setVisible(true);
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension d = tk.getScreenSize();
int x = d.width / 2;
int y = (d.height / 2 ) - classes1.getHeight();
classes1.setLocation(x,y);
}
}
i imported Jframe its just hidden.
Re: How do i set a background image to an extended (2 class) JFrame program?
i am also having the same problem
Re: How do i set a background image to an extended (2 class) JFrame program?
Your favorite search engine will offer plenty of information on your problem. This is not quite the place for such a lengthy set of instructions. Not to mention there are many ways to do it, and no one way is the always the best way to go. It depends on the overall use etc.