[QUOTE=Ezzaral;1742802]The listener would be added your panel. You could also use KeyBindings: [url]http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html[/url][/QUOTE]

Thank you so much for your quick reply:)! I have been trying to add the listener to "panel", "super" and all sorts I can think of to be honest. But I will start looking in to KeyBindings, as it might be better for this code.

Hello everyone on daniweb!:) I'm trying to make a programme which will move a picture to the sides when pressing the right or left arrowkeys. I did successfully put a picture in and I was able to change the side coordinates(called "side" in the programme) to make the picture move. However now I want it to be done with KeyListener. I have been trying to browse the internet for help but I havnt yet found anything which worked. One problem is that I dont know what to add the keylistener to(now its added to "img" because I dont know better). Does anybody know how I can make this work? I'll be so thankfull for your help!:)

Here is the code:
[CODE]
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class Background3
{
public static void main(String[] skit)
{
ImagePanel panel = new ImagePanel(new ImageIcon("Projektbilder/Bakgrund.png").getImage());
JFrame frame = new JFrame();
frame.getContentPane().add(panel);
frame.setSize(500,500);
frame.setVisible(true);

}
}

class ImagePanel extends JPanel
{
private int side=0;

private Image img;

public ImagePanel(String img)
{
this(new ImageIcon(img).getImage());
}

public ImagePanel(Image img)
{
this.img = img;
Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
setSize(size);
setLayout(null);
}

img.addKeyListener(this);

public void keyTyped(KeyEvent e)
{
if(e.getKeyCode() == e.VK_RIGHT)
{
side++;
repaint();
}

else if (e.getKeyCode() == e.VK_LEFT)
{
        side--;
        repaint();
}

}

public void keyPressed(KeyEvent e)
{
}

public void keyReleased(KeyEvent e)
{
}

public void paintComponent(Graphics g)
{
g.drawImage(img, side, 0, null);
}

}
[/CODE]

bearing* !!!! haha

//Angelika

[QUOTE=JamesCherrill;1534760]What do you mean "couldn't solve it"? What exactly can't you do? What does your latest code look like? What errors (if any) does it give you?[/QUOTE]

I have just tried working a little bit on it today. I have removed some unnecessary things in the code and I have made the JLabel a JPanel again(thats why the JLabel was called "panel" before). Not much have changed though:/. I'm not english and find it hard to understand sometimes so I'd love if you know what's wrong with my code that you'd change it in that place it needs to be changed so that I will understand it(: Thank you all so much for baring with me this far, I'm doing my best to understand this.

[CODE]import javax.swing.;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.
;
import java.awt.event.;
import java.lang.
;
import java.awt.event.KeyEvent.;
import java.awt.event.KeyListener.
;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.Graphics.*;

public class Boll extends JFrame
{
JPanel panel=new JPanel();
private int x=250, y=250;
private boolean ball=true;
private final int MAXX=500, MAXY=500;

public static void main(String[] skit)
{
    Boll b=new Boll();
}

public Boll()
{
    super.setSize(MAXX, MAXY);
    super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    super.add(panel);
    panel.setSize(MAXX, MAXY);
    panel.paint();
    panel.addKeyListener(new KeyThing());
    panel.setVisible(true);
    super.setVisible(true); //sätter ut

}

public class KeyThing implements KeyListener
{
    public void keyPressed(KeyEvent e)
    {
        if(e.getKeyLocation()==KeyEvent.VK_LEFT)
        {
            x--;
            panel.repaint();
        }

        else if(e.getKeyLocation()==KeyEvent.VK_RIGHT)
        {
            x++;
            panel.repaint();
        }

        else if(e.getKeyLocation()==KeyEvent.VK_UP)
        {
            y++;
            panel.repaint();
        }

        else if(e.getKeyLocation()==KeyEvent.VK_DOWN)
        {
            y--;
            panel.repaint();
        }
    }

    public void keyTyped(KeyEvent e)
    {

    }

    public void keyReleased(KeyEvent e)
    {

    }

}

public void paintComponent(Graphics g)
{   
    if (ball) ...

i went through this with my teacher and we still couldn't solve it:/ Any more help would be apreciated

It still isn't showing:/ The only thing coming up is a big white box.
Here's the changes that I've made:

[CODE]
import javax.swing.;
import java.awt.
;
import java.awt.event.;
import java.lang.
;
import java.awt.event.KeyEvent.;
import java.awt.event.KeyListener.
;
import java.awt.Graphics.*;

public class Boll
{
JFrame frame=new JFrame("hej");
JLabel panel=new JLabel();
private int x=250, y=250;
private boolean ball=true;
private boolean jump=false;
private final int MAXX=500, MAXY=500;
private int left, right, up, down, side;
private Graphics gr;
private int g, m, v, E, p, a, t; //fysikaliska element

public static void main(String[] skit)
{
    Boll b=new Boll();
}

public Boll()
{
    frame.setSize(MAXX, MAXY);
    frame.setLayout(new GridLayout(1, 1));
    frame.setVisible(true);
    frame.add(panel);
    side=5;
    panel.setVisible(true); //sätter ut
    panel.setSize(MAXX, MAXY);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    panel.addKeyListener(new KeyThing());
    panel.repaint();

}

public class KeyThing implements KeyListener
{
    public void keyPressed(KeyEvent e)
    {
        if(e.getKeyLocation()==KeyEvent.VK_LEFT)
        {
            x-=side;
            panel.repaint();
        }

        else if(e.getKeyLocation()==KeyEvent.VK_RIGHT)
        {
            x+=side;
            panel.repaint();
        }

        else if(e.getKeyLocation()==KeyEvent.VK_UP)
        {
            y+=side;
            panel.repaint();
        }

        else if(e.getKeyLocation()==KeyEvent.VK_DOWN)
        {
            y-=side;
            panel.repaint();
        }
    }

    public void keyTyped(KeyEvent e)
    {

    }

    public void keyReleased(KeyEvent e)
    {

    }

}

public void paintComponent(Graphics g)
    {  
        if (ball)
        {    
            g.setColor(Color.green);    
            g.fillOval(x, y, 30, 30);
        }
    }

}
[/CODE]

Thank you!
//Angelika

Hello everyone on Daniweb!:)
I'm all new with working with swing and I'm having some problems making my paint method work:/. Atm I'm just tryin to make it write a ball, which I can control into different directions.
Right now there is quite alot of unnecessary stuff in my code but please don't bother about that:P.
Here's the code:

[CODE]import javax.swing.;
import java.awt.
;
import java.awt.event.;
import java.lang.
;
import java.awt.event.KeyEvent.;
import java.awt.event.KeyListener.
;
import java.awt.Graphics.*;

public class Boll
{
JFrame frame=new JFrame("hej");
JLabel panel=new JLabel();
private int x=250, y=250;
private boolean ball=true;
private boolean jump=false;
private final int MAXX=500, MAXY=500;
private int left, right, up, down, side;
private Graphics gr;
private int g, m, v, E, p, a, t; //fysikaliska element

public static void main(String[] skit)
{
    Boll b=new Boll();
}

public Boll()
{
    frame.setVisible(true);
    frame.pack();
    frame.add(panel);
    side=5;
    panel.setVisible(true); //sätter ut
    panel.setSize(MAXX,MAXY);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    panel.addKeyListener(new KeyThing());
    panel.paint(gr);

}

public class KeyThing implements KeyListener
{
    public void keyPressed(KeyEvent e)
    {
        if(e.getKeyLocation()==KeyEvent.VK_LEFT)
        {
            x-=side;
            panel.repaint();
        }

        else if(e.getKeyLocation()==KeyEvent.VK_RIGHT)
        {
            x+=side;
            panel.repaint();
        }

        else if(e.getKeyLocation()==KeyEvent.VK_UP)
        {
            y+=side;
            panel.repaint();
        }

        else if(e.getKeyLocation()==KeyEvent.VK_DOWN)
        {
            y-=side;
            panel.repaint();
        }
    }

    public void keyTyped(KeyEvent e)
    {

    }

    public void keyReleased(KeyEvent e)
    {

    }

}

public void paint(Graphics g)
{
    if (ball==true)
    {
        g=panel.getGraphics();
        g.setColor(Color.green);
        g.fillOval(x, y, 30, 30);
        g.dispose();
    }
}

}[/CODE]

Huge thanks to everyone for taking time to help me!:) I really appreciate it.

//Angelika

ye another girl!:D <33

Helloooo:) <3

[QUOTE=quuba;1470263]used equals on incompatible types (JButton vs. String) use Plan[0][1].getText().equals("O")[/QUOTE]

Thank you so much!:)

Hello!
My name is Angelika and I'm 17 years old. I live in Sweden and is currenly studying Java in my education and it has turned out to be like a little side hobby:)

Looking forward to learn some new things from you all(: x

I'm currently working on a tic-tac-toe program and it is working to the point that it runs and X's and O's appear. However when the right combination is made or the board gets full the method "Resultat();" should recognize it and "Clear();" should clear out the board and a JFrame should open asking if i want to exit or restart. This doesn't happen. The board just gets filled without reacting:/

If any one has a clue what I'm doing wrong please help me out.
Thank you in advance!:)

(please dont mind that some of the variables have wierd names, im not english)
//Angelika

[CODE]import javax.swing.;
import java.awt.
;
import java.awt.event.;
import java.lang.
;
import javax.swing.JButton;
import javax.swing.JFrame;

public class Knapp
{
private JButton [][]Plan=new JButton[3][3]; //3*3 squares
private JButton exit, restart;
private JLabel winO, winX, tie;
private int loop=0;
JFrame frame=new JFrame("Tic-Tac-Toe"); //main JFrame
JFrame PopUp1=new JFrame("Grattis!");
JFrame PopUp2=new JFrame("Grattis!");
JFrame PopUp3=new JFrame("Oavgjort");

public static void main(String[] skit)
{
    Knapp k=new Knapp();
}

public Knapp()
{
    frame.setLayout(new GridLayout(3,3));

    Plan[0][2]=new JButton("");                   
    Plan[0][2].addActionListener(new MyAction());
    frame.add(Plan[0][2]);

    Plan[1][2]=new JButton("");
    Plan[1][2].addActionListener(new MyAction());
    frame.add(Plan[1][2]);

    Plan[2][2]=new JButton("");
    Plan[2][2].addActionListener(new MyAction());
    frame.add(Plan[2][2]);

    Plan[0][1]=new JButton("");
    Plan[0][1].addActionListener(new MyAction());
    frame.add(Plan[0][1]);

    Plan[1][1]=new JButton("");
    Plan[1][1].addActionListener(new MyAction());
    frame.add(Plan[1][1]);

    Plan[2][1]=new JButton("");
    Plan[2][1].addActionListener(new MyAction());
    frame.add(Plan[2][1]);

    Plan[0][0]=new JButton("");
    Plan[0][0].addActionListener(new MyAction());
    frame.add(Plan[0][0]);

    Plan[1][0]=new JButton("");
    Plan[1][0].addActionListener(new MyAction());
    frame.add(Plan[1][0]);

    Plan[2][0]=new JButton("");
    Plan[2][0].addActionListener(new MyAction());
    frame.add(Plan[2][0]);

    frame.setSize(500,500);                         
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //***************************************************                 

    PopUp1.setSize(200,200);                                           
    PopUp1.setVisible(false);
    PopUp1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    PopUp1.setLayout(new GridLayout(2,1));
    winO=new JLabel("Grattis! Spelare O vann!");
    PopUp1.add(winO);
    JPanel panel1=new JPanel();
    panel1.setLayout(new GridLayout(1,2));
    restart=new JButton("Ny omgång");
    exit=new JButton("Avsluta");
    restart.addActionListener(new AnotherAction());
    exit.addActionListener(new AnotherAction());
    panel1.add(restart);
    panel1.add(exit);

    PopUp2.setSize(200,200);    
    PopUp2.setVisible(false);
    PopUp2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    PopUp2.setLayout(new GridLayout(2,1));
    winX=new JLabel("Grattis! Spelare ...