Results 1 to 13 of 13

Thread: Layout problem

  1. #1
    Flopet17 is offline Member
    Join Date
    Mar 2014
    Posts
    60
    Rep Power
    0

    Red face Layout problem

    Hey guys,
    I want to move a label in a Matrics of Panels with KeyListener, I want to do something like the game 2048.
    But I can't see the label,I have to edit the size of frame to see it... why?
    If I use layout(null) i see the Matrics but not the Panel. If I comment out the layout(null) I see the button across the whole JFrame and If I try to setVisible of the button to false, the KeyListener is not working anymore...
    Java Code:
    package matrics;
    
    import java.awt.Color;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import javax.swing.BorderFactory;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class Matrics extends JFrame implements KeyListener{
        
        JPanel p[][] = new JPanel[4][4];
        public int i=1,j=1;
        public JButton b = new JButton("Here");
        public JPanel b1 = new JPanel();
        
        public void Framer() {
            b.addKeyListener(this);
            b1.setBackground(Color.red);
            setLayout(null);
            setTitle("Matrics");
            setVisible(true);
            setSize(610,630);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            for(int i = 0; i<4;i++){
                for(int j = 0; j<4;j++){
                    p[i][j]= new JPanel();
                }
            }
            for(int i = 0; i<4;i++){
                for(int j = 0 ; j<4;j++)
                {   
                    p[i][j].setBounds(i*150,j*150,150,150);
                    p[i][j].setBorder(BorderFactory.createLineBorder(Color.black));
                    add(p[i][j]);
                }
            }
            add(b);
        }
        @Override
        public void keyTyped(KeyEvent arg0) {
        }
    
        @Override
        public void keyPressed(KeyEvent e){
            int keyCode = e.getKeyCode();
            if(keyCode == e.VK_LEFT){
                i--;
                System.out.println("left");
            }
            if(keyCode == e.VK_RIGHT){
                i++;
                System.out.println("right");
            }
            if(keyCode == e.VK_UP){
                j--;
                System.out.println("up");
            }
            if(keyCode == e.VK_DOWN){
                j++;
                System.out.println("down");
            }
            p[i][j].add(b1);
        }
    
        @Override
        public void keyReleased(KeyEvent arg0) {
        }
        
        Matrics(){
            Framer();
        }
        public static void main(String[] args) {
            Matrics m = new Matrics();
            m.setVisible(true);
        }
    
    }
    Thank you :)

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Layout problem

    How do you want to control the layout of the components added to the JFrame?
    There are several layout managers that might be better than the default: border
    When setLayout(null) is executed, the layout manager is removed. The code is then responsible for doing the laying out of the added components.

    NOTE: Single letter variable names make it hard to understand what the variable holds and what it is used for. Its better to give a meaningful name to a variable. For example the name p does not say what is in it.
    Last edited by Norm; 04-03-2014 at 08:04 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Flopet17 is offline Member
    Join Date
    Mar 2014
    Posts
    60
    Rep Power
    0

    Default Re: Layout problem

    This code sometimes work, sometimes doesn't. Why?
    If I run it, and press the arrows, it's working but not always, and I don't understand why...

    Java Code:
    package matrics;
    
    import java.awt.Color;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import javax.swing.BorderFactory;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class Matrics extends JFrame implements KeyListener{
        
        JPanel p[][] = new JPanel[6][6];
        public int i=1,j=1;
        public JButton b = new JButton("Here");
        public JButton b1 = new JButton();
        
        public void Framer() {
            b.addKeyListener(this);
            b.setVisible(true);
            setLayout(null);
            b1.setBackground(Color.red);
            b1.setBounds(0,0,100,100);
            setTitle("Matrics");
            setVisible(true);
            setSize(630,630);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            for(int i = 0; i<6;i++){
                for(int j = 0; j<6;j++){
                    p[i][j]= new JPanel();
                }
            }
            for(int i = 0; i<6;i++){
                for(int j = 0 ; j<6;j++)
                {   
                    p[i][j].setBounds(i*100,j*100,100,100);
                    p[i][j].setBorder(BorderFactory.createLineBorder(Color.black));
                    add(p[i][j]);
                }
            }
            add(b);
        }
        @Override
        public void keyTyped(KeyEvent arg0) {
        }
    
        @Override
        public void keyPressed(KeyEvent e){
            int keyCode = e.getKeyCode();
            if(keyCode == e.VK_LEFT){
                i--;
                System.out.println("left");
            }
            if(keyCode == e.VK_RIGHT){
                i++;
                System.out.println("right");
            }
            if(keyCode == e.VK_UP){
                j--;
                System.out.println("up");
            }
            if(keyCode == e.VK_DOWN){
                j++;
                System.out.println("down");
            }
            p[i][j].add(b1);
            b1.setVisible(true);
            repaint();
        }
    
        @Override
        public void keyReleased(KeyEvent arg0) {
        }
        
        Matrics(){
            Framer();
        }
        public static void main(String[] args) {
            Matrics m = new Matrics();
            m.setVisible(true);
        }
    
    }

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Layout problem

    What is the user supposed to do to see the code execute as you expect?
    What does the user do and what is the code supposed to do in response?

    To get key events the component needs to have the focus.
    Last edited by Norm; 04-03-2014 at 08:55 PM.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Flopet17 is offline Member
    Join Date
    Mar 2014
    Posts
    60
    Rep Power
    0

    Default Re: Layout problem

    The user have to press one of the arrows and the Label will show up and start moving when you press the arrow.
    This is what I don't understand... Why the Label only appears when you press the arrow? And it only works sometimes, not always when you run the project.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Layout problem

    Why the Label only appears when you press the arrow?
    when and where is the Label supposed to appear?

    ??? Where is there any label in the code????


    This brings up the problem with single letter variable names. When I look at the code and see:
    add(b)
    or
    .add(b1);
    what is being added?
    Last edited by Norm; 04-03-2014 at 09:16 PM.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Flopet17 is offline Member
    Join Date
    Mar 2014
    Posts
    60
    Rep Power
    0

    Default Re: Layout problem

    I apologize... It was a Panel, not a Label... My mystake... The Panel that shows up in a red squeare is b1... it supposed to be a button and this is why is b1...
    This code is just for testing, and I did it just for practice, and I wasn't very carefull with variables... I'll focus more in the future :)

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Layout problem

    Why the Label only appears when you press the arrow?
    when and where is the Label/button supposed to appear?
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    Flopet17 is offline Member
    Join Date
    Mar 2014
    Posts
    60
    Rep Power
    0

    Default Re: Layout problem

    In the panel p[1][1], and when you move it, for example to the right it will be added to p[1][2] and so on...

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Layout problem

    Where does the button get added to panel p[1][1] before a key press?

    The question was:
    Why the Label only appears when you press the arrow?
    Answer: because that is the way the code was written.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    Flopet17 is offline Member
    Join Date
    Mar 2014
    Posts
    60
    Rep Power
    0

    Default Re: Layout problem

    Quote Originally Posted by Norm View Post
    Where does the button get added to panel p[1][1] before a key press?
    How would you do this project? :)

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Layout problem

    How would you do this project? :)
    What's it supposed to do?
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,049
    Rep Power
    6

    Default Re: Layout problem

    Quote Originally Posted by Flopet17 View Post
    How would you do this project? :)
    How is that an answer to the question asked!?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. GUI Layout Problem
    By Zelaine in forum New To Java
    Replies: 3
    Last Post: 12-31-2013, 07:12 PM
  2. Layout problem
    By bmL in forum Java Applets
    Replies: 3
    Last Post: 06-24-2012, 04:24 AM
  3. Layout Problem
    By kris5228 in forum AWT / Swing
    Replies: 6
    Last Post: 01-09-2012, 09:38 PM
  4. Problem with layout manager
    By mDennis10 in forum New To Java
    Replies: 8
    Last Post: 09-05-2011, 03:02 AM
  5. Layout problem
    By miller.bartek in forum AWT / Swing
    Replies: 3
    Last Post: 01-11-2011, 07:50 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
  •