Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Moving JPanel with Keylisteners

  1. #1
    Junior Member
    Join Date
    Jul 2018
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Moving JPanel with Keylisteners

    Heyho, I am kinda stuck at a part in my code. Or more, two parts. For one, the image 'Kiste' should have an invisible background, but it kind of gets one. I haven't found out yet what I could do to solve this.

    The way bigger problem is, that when I press any key, most times nothing happens. Only sometimes it moves out of the frame. Does anyone know?
    I never worked with keybindings before and I don't really have the time to learn how to use them.

    Thank you!

    import java.awt.Color;
    import java.awt.Font;
    import java.awt.event.KeyEvent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
     
     
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JLabel;
    import javax.swing.JTextArea;
    import java.awt.image.*;
    import java.io.*;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyListener; 
     
    public class gui implements KeyListener{
     
    	static JFrame jf = new JFrame();
    	public static JTextArea ta;
    	static JPanel panel = new JPanel();
    	static JPanel bPanel = new JPanel();
    	static int screenbreite = 1250;
    	static int screenhoehe = 1000;
    	static int bildhoehe = 800;
    	static int kposbreite = screenbreite/2-100;
    	static int kposhoehe = bildhoehe/2-100;
     
     
    	public static void main(String[] args) throws IOException 
    	{
     
    		jf.setSize(screenbreite, screenhoehe);
    		jf.setLocationRelativeTo(null);
    		jf.setBackground(Color.BLACK);
    		jf.setName("Adventure");
    		jf.setResizable(false);
    		jf.requestFocus();
    		panel.setBounds(0, 0, screenbreite, bildhoehe);
    		BufferedImage image = ImageIO.read(new File("..."));
    	    JLabel label = new JLabel(new ImageIcon(image));
    	    panel.add(label);
    	    jf.add(panel);
     
    		jf.setLayout(null);
     
    		ta = new JTextArea();
            ta.setText("Beispieltext");
            ta.setBounds(0, 800, screenbreite, 200);
            ta.setBackground(Color.gray);
            ta.setForeground(Color.black);
            ta.setEditable(false);
            ta.setVisible(true);
            ta.setFont(new Font("Times New Roman",Font.BOLD,30));
            ta.setLineWrap(true);
            jf.add(ta); 
     
    	    jf.setVisible(true);
    	    kistenBewegen();
     
    	}
    	public static void kistenBewegen() throws IOException
    	{
    		bPanel.setBounds(kposbreite, kposhoehe, 200, 200);
    		//bPanel.setOpaque(false);
     
    		BufferedImage kiste = ImageIO.read(new File("..."));
    		JLabel bLabel = new JLabel(new ImageIcon(kiste));
    		bPanel.add(bLabel);
    		jf.add(bPanel);
    		jf.addKeyListener(new KeyAdapter()
    		{	
    			public void keyPressed(KeyEvent e) 
    			{			
     
    				if(e.getKeyCode() == KeyEvent.VK_DOWN)
                    {
     
    					bPanel.setLocation(bPanel.getX(),bPanel.getY()+50);
    					bPanel.repaint();
                    }
    				if(e.getKeyCode() == KeyEvent.VK_UP)
                    {
     
    					bPanel.setLocation(bPanel.getX(),bPanel.getY()-50);
    					bPanel.repaint();
                    }
    				if(e.getKeyCode() == KeyEvent.VK_LEFT)
                    {
    					bPanel.setLocation(bPanel.getX()-50,bPanel.getY());
    					bPanel.repaint();
                    }
    				if(e.getKeyCode() == KeyEvent.VK_RIGHT)
                    {
    					bPanel.setLocation(bPanel.getX()+50,bPanel.getY());
    					bPanel.repaint();
                    }			
    			}		
    		});
    	}
     
     
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,387
    Thanks
    52
    Thanked 2,304 Times in 2,262 Posts

    Default Re: Moving JPanel with Keylisteners

    when I press any key, most times nothing happens.
    Can you explain what you want the code to do when any key is pressed?

    Please copy the full text of any error messages and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Moving content (graphics) inside a JPanel
    By Nesh108 in forum Java Theory & Questions
    Replies: 6
    Last Post: February 1st, 2012, 12:21 PM
  2. KeyListeners and KeyEvents
    By worsewicked in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: January 7th, 2012, 07:21 PM
  3. [SOLVED] KeyListeners Require Focus...
    By snowguy13 in forum AWT / Java Swing
    Replies: 2
    Last Post: January 6th, 2012, 07:15 AM
  4. Question about KeyListeners
    By Jams in forum Java Theory & Questions
    Replies: 5
    Last Post: November 6th, 2011, 09:08 AM
  5. KeyListeners: Automatic Focus?
    By bgroenks96 in forum Java Theory & Questions
    Replies: 32
    Last Post: June 24th, 2011, 09:03 PM