Link to home
Start Free TrialLog in
Avatar of gnurl
gnurlFlag for Germany

asked on

Swing and background picture

Hi,

I am trying to put some labels and textfields to a JFrame with a jpeg picture as background. I am working with JLayeredPane and Swing, but the picture will not appear in  the background.

Anybody here who can help me with this?

This is my current source ...

___________________________________________
package daframe;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;


public class ChooseKey extends Frame{
   
    private char rotateLeft;
    private char rotateRight;
    private char pause;
    private char shiftLeft;
    private char shiftRight;
    private char dropDown;
    private int height;
    private int width;
   
    public ChooseKey(int width, int height, String pic)
    {
        TextField rotateLeft = new TextField("",1);
        TextField rotateRight = new TextField("",1);
        TextField pause = new TextField("p");
        TextField shiftLeft = new TextField("d");
        TextField shiftRight = new TextField("a");
        TextField dropDown = new TextField("s");
        Sprache dieSprache = new Sprache(spr);
       
        //set size and title for frame
        setTitle("choose keys");
        setSize(width,height);
           
        //create LayeredPane and add icon to label
        JLayeredPane pane = new JLayeredPane();
        //Image laden und auf Label packen
        ImageIcon back = new ImageIcon(pic);
        JLabel label = new JLabel(back);
        //Der LayeredPane eine Dimension zuweisen
        pane.setPreferredSize(new Dimension(width,height));
               
        //label.setLayout(new BorderLayout());
       
        //add picture to layer 0
        pane.add(label,new Integer(0));
       
        //rotateLeft.setColumns(1);
       
        //pane.setLayout(new FlowLayout());
        pane.setLayout(new GridLayout(6,2));            
               
        //create panel and add components for choosing the keys
        JPanel panel1 = new JPanel();
        panel1.setLayout(new FlowLayout(FlowLayout.LEFT));
        panel1.add(rotateLeft);
        panel1.add(new Label("key_rl"));
        panel1.setOpaque(false);
       
        JPanel panel2 = new JPanel();
        panel2.add(rotateRight);
        panel2.setLayout(new FlowLayout(FlowLayout.LEFT));        
        panel2.add(new Label("key_rr"));
        panel2.setOpaque(false);
       
        JPanel panel3 = new JPanel();
        panel3.setLayout(new FlowLayout(FlowLayout.LEFT));
        panel3.add(pause);
        panel3.add(new Label("key_p"));
        panel3.setOpaque(false);
       
        JPanel panel4 = new JPanel();
        panel4.setLayout(new FlowLayout(FlowLayout.LEFT));
        panel4.add(shiftLeft);
        panel4.add(new Label("key_sl"));
        panel4.setOpaque(false);
       
        JPanel panel5 = new JPanel();
        panel5.setLayout(new FlowLayout(FlowLayout.LEFT));
        panel5.add(shiftRight);
        panel5.add(new Label("key_sr"));
        panel5.setOpaque(false);
       
        JPanel panel6 = new JPanel();
        panel6.setLayout(new FlowLayout(FlowLayout.LEFT));
        panel6.add(dropDown);
        panel6.add(new Label("key_dd"));
        panel6.setOpaque(false);
       
                           
        pane.add(panel1, new Integer(1));
        pane.add(panel2, new Integer(1));
        pane.add(panel3, new Integer(1));
        pane.add(panel4, new Integer(1));
        pane.add(panel5, new Integer(1));
        pane.add(panel6, new Integer(1));
        pane.setOpaque(false);
               
        add(pane);
       
        addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            }
        });
    }
   
   
/**
 * testdriver for class
 * @param args
 */    
    public static void main (String args[])
    {
        ChooseKey t = new ChooseKey(500,400,"images/back.jpg");
        t.show();
    }
_________________________________________


Thanks for your advise.

CU
Avatar of bhaskar20001
bhaskar20001
Flag of India image

Hello gnurl,

  which version of jdk you are using.I tested your program using both jdk1.4 and 1.3. But with 1.4 only I got jvm error.
    as with jdk1.3 it is working fine and also image is appearing in background. as you have given relative path for ur image file it depends on from which directory path u  r running ur application. and while testing ur program I commented the "Sprache"  instantiation statement..

--Bhaskar.


   
Avatar of gnurl

ASKER

Hello

--Bhaskar I am using jdk1.4 with Eclipse IDE.
I modified the source (deleted connection to other classes ... as you see  in "Sprache" ) so that ist seems that there is an error in it at the Moment ... I will fix in in some hours (i think) and repost it. Sorry.

The path is ok, but if I use an GridLayout the picture is showed as last component of the Grid ... so it is not an background image but an image in a grid...

Thanks
Avatar of gnurl

ASKER

Hi,

I think I could fix the 1.4 error .... just remove the line with the package declaration und set an closing } at the end of the file.

CU
ASKER CERTIFIED SOLUTION
Avatar of jkrippy
jkrippy

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gnurl

ASKER

Hi jkrippy,

this helps a lot, thank you.

CU gnurl