Results 1 to 4 of 4
Like Tree2Likes
  • 1 Post By Norm
  • 1 Post By DarrylBurke

Thread: KeyListener will not works

  1. #1
    Halfbax is offline Member
    Join Date
    Aug 2015
    Posts
    6
    Rep Power
    0

    Default KeyListener will not works

    Hello.

    I have a issue which I cannot fix by myself. I think so.

    Firstly, my *.class works with a "MouseListener", finely. Then I toggled the "MouseListener"-block and converted it into the "KeyListener Event".
    Now it will not reacting like as the version before.

    Is the possible available to take a look on my code. This would be nice.

    Greets,
    Newcomer Halfbax

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class Fenster extends JFrame {
        
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    Frame(); 
                }
            });
        }
    
        private static void Frame(){        
            JFrame win = new JFrame("Liquidtisch");
    
            win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
            win.setExtendedState(Frame.MAXIMIZED_BOTH);
            win.setUndecorated(true);
            win.add(new MyPanel());
            win.pack();
            win.setVisible(true);
            
        }
    }
    
    class MyPanel extends JPanel {    
    
        public int Felder = 1;
        public int[] pos = new int[19];
        
        public MyPanel() {        
            setBackground(Color.BLACK);
            pos[0] = 150;        
            
    //        addMouseListener(new MouseAdapter() {
    //            public void mousePressed(MouseEvent e) {
    //                addField();
    //            }
    //        });
            
            addKeyListener(new KeyAdapter() {
                public void keyReleased(KeyEvent e) {
                    JOptionPane.showMessageDialog(null, "Test", "Test Titel", JOptionPane.OK_CANCEL_OPTION);
                    
                    if(e.getKeyCode() == KeyEvent.VK_ENTER) {
                        addField();
                    }
                }
            });
        }
        
        public void addField(){
            Felder++;        
            pos[Felder-1] = pos[Felder-2] + 200;
            repaint();
        }
        
        public Dimension getPreferredSize() {
            return new Dimension(250,200);
        }
        
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);  
            g.setColor(Color.WHITE);
            Font font = g.getFont().deriveFont( 20.0f );
            g.setFont( font );
            g.drawString("Liquidtisch v0.3", 200, 100);
    
            for(int i = 0; i < Felder; i++) {
                if(i != Felder-1) {
                    g.setColor(Color.WHITE);
                }
                else {
                    g.setColor(Color.RED);
                }
                
                if(i<11) {
                    g.drawRect(pos[i], 200, 150, 150);
                    g.fillRect(pos[i], 200, 150, 150);
                }
                else {
                    g.drawRect(pos[i-10], 500, 150, 150);
                    g.fillRect(pos[i-10], 500, 150, 150);    
                }
            }
    
        }  
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: KeyListener will not works

    Make sure the component with the keylistener has focus.
    Halfbax likes this.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Halfbax is offline Member
    Join Date
    Aug 2015
    Posts
    6
    Rep Power
    0

    Default Re: KeyListener will not works

    Quote Originally Posted by Norm
    Make sure the component with the keylistener has focus.
    Oh god ...

    This is a little bit embrassing for me :D, I already tried it, but I did not tried it to request the focus of the paint-method. Now it works

    Greets and thank you,
    Halfbax

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: KeyListener will not works

    Halfbax likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. KeyListener works with JFrame but not with JApplet
    By Vampire5 in forum AWT / Swing
    Replies: 2
    Last Post: 06-13-2014, 04:54 PM
  2. Replies: 18
    Last Post: 03-26-2014, 10:43 PM
  3. Keylistener help
    By dafly45 in forum Java Applets
    Replies: 6
    Last Post: 11-07-2013, 02:12 PM
  4. Need help with KeyListener
    By McDucky in forum New To Java
    Replies: 3
    Last Post: 09-16-2012, 05:35 PM
  5. Replies: 2
    Last Post: 05-11-2011, 06:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •