2个JLabel同时绘制

时间:2019-03-19 09:36:45

标签: java jlabel

我想同时拥有两个JLabel(创建两个圆圈),并且可以通过键盘移动。 但是,仅显示绿色的Vert,而不显示蓝色的Bleu

public class FenJeu extends JPanel{
    int xPos;
    int yPos;

    public FenJeu(int r, int v, int b) {
        JLabel image = new JLabel();    
        add(new Vert());
        add(new Bleu());
        image.setVisible(true);
        image.setEnabled(true);
        //image.setIcon(new ImageIcon("D:\\terrain.png"));
        //image.setBounds(1,1,1680,1050);
        //image.setPreferredSize(new Dimension(1680,1050));
        add(image);
    }
    public class Vert extends JPanel {

            public Vert() {

                Action leftAction = new AbstractAction() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        xPos -= 50;
                        if (xPos < 0) {
                            xPos = 0;
                        }
                        repaint();
                    }
                };
                Action rightAction = new AbstractAction() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        xPos += 50;
                        if (xPos + 10 > getWidth()) {
                            xPos = getWidth() - 10;
                        }
                        repaint();
                    }
                };
                Action upAction = new AbstractAction() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        yPos -= 50;
                        if (yPos + 10 > getWidth()) {
                            yPos = getWidth() - 10;
                        }
                        repaint();            
                    }
                };
                Action downAction = new AbstractAction() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        yPos += 50;
                        if (yPos < 0) {
                            yPos = 0;
                        }
                        repaint();                      
                    }
                };

                bindKeyStroke(WHEN_IN_FOCUSED_WINDOW, "move.left", KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), leftAction);

                bindKeyStroke(WHEN_IN_FOCUSED_WINDOW, "move.right", KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), rightAction);

                bindKeyStroke(WHEN_IN_FOCUSED_WINDOW, "move.up", KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), upAction);

                bindKeyStroke(WHEN_IN_FOCUSED_WINDOW, "move.down", KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), downAction);           
            }

            protected void bindKeyStroke(int condition, String name, KeyStroke keyStroke, Action action) {
                InputMap im = getInputMap(condition);
                ActionMap am = getActionMap();

                im.put(keyStroke, name);
                am.put(name, action);
            }
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(1680, 1050);
            }

            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g.create();
                //int yPos = (getHeight() - 10) / 2;
                //int xPos = (getWidth() -10) / 2;
                g2d.setPaint(Color.GREEN);
                g2d.fillOval(xPos, yPos, 50, 50);

                g2d.dispose();
            }

    }
           public class Bleu extends JPanel {

            public Bleu() {
                Action lefttAction = new AbstractAction() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        xPos -= 50;
                        if (xPos < 0) {
                            xPos = 0;
                        }
                        repaint();

                    }
                };
                Action righttAction = new AbstractAction() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        xPos += 50;
                        if (xPos + 10 > getWidth()) {
                            xPos = getWidth() - 10;
                        }
                        repaint();

                    }
                };
                Action uppAction = new AbstractAction() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        yPos -= 50;
                        if (yPos + 10 > getWidth()) {
                            yPos = getWidth() - 10;
                        }
                        repaint();
                        System.out.println("oui");
                    }
                };
                Action downnAction = new AbstractAction() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        yPos += 50;
                        if (yPos < 0) {
                            yPos = 0;
                        }
                        repaint();
                    }
                };

                bindKeyStroke1(WHEN_IN_FOCUSED_WINDOW, "move.leftt", KeyStroke.getKeyStroke(KeyEvent.VK_Q, 0), lefttAction);

                bindKeyStroke1(WHEN_IN_FOCUSED_WINDOW, "move.rightt", KeyStroke.getKeyStroke(KeyEvent.VK_D, 0), righttAction);

                bindKeyStroke1(WHEN_IN_FOCUSED_WINDOW, "move.upp", KeyStroke.getKeyStroke(KeyEvent.VK_Z, 0), uppAction);

                bindKeyStroke1(WHEN_IN_FOCUSED_WINDOW, "move.downn", KeyStroke.getKeyStroke(KeyEvent.VK_S, 0), downnAction);

            }
            protected void bindKeyStroke1(int conditionn, String namee, KeyStroke keyStrokee, Action actionn) {
                InputMap om = getInputMap(conditionn);
                ActionMap pm = getActionMap();

                om.put(keyStrokee, namee);
                pm.put(namee, actionn);
            }

            protected void paintComponent1(Graphics f) {
                super.paintComponent(f);
                Graphics2D g3d = (Graphics2D) f.create();
                //int yPos = (getHeight() - 10) / 2;
               // int xPos = (getWidth() -10) / 2;
                g3d.setPaint(Color.BLUE);
                g3d.fillOval(xPos, yPos, 50, 50);


                g3d.dispose();
            }
    }
}

1 个答案:

答案 0 :(得分:0)

  1. Vert面板尺寸为1680x1050(请参见Vert#getPreferredSize()),Bleu没有定义尺寸,因此将设置最小的组件尺寸。

  2. 您可以为paintComponentVert覆盖JPanel的Bleu方法,但是Bleu中的方法名称为paintComponent1。如果您正确的方法名称将绘制蓝色圆圈,但由于尺寸最小,您只会看到其中的一小部分。默认JPanel的布局为FlowLayout,因此您的Bleu面板将位于Vert之后的右侧。