2012-08-25 1 views
0

JLayeredPane 위에 여러 JLabel을 동적으로 이동해야합니다. 문제는 제대로 새로 고쳐지지 않는다는 것입니다. 때때로 그들은 정확하게 움직이며 때로는 전혀 나타나지 않습니다.JLayeredPane에서 JLabel을 이동

내 코드는 다음과 같습니다

JLabel의 추가 패널

JLayeredPane gameBoardPane = new JLayeredPane(); 
gameBoardPane.setLayout(null); 
Dimension gameBoardLabelSize = gameBoardLabel.getPreferredSize(); 
gameBoardLabel.setBounds(30, 30, gameBoardLabelSize.width, gameBoardLabelSize.height); 
gameBoardPane.add(gameBoardLabel, new Integer(10)); 
gameBoardPane.setAlignmentY(Component.TOP_ALIGNMENT); 

// ... 

JPanel gamePane = new JPanel(); 
gamePane.setLayout(new BoxLayout(gamePane, BoxLayout.LINE_AXIS)); 
gamePane.add(gameBoardPane); 
gamePane.add(gameControlPane); 

// ... 

Container contentPane = getContentPane(); 
contentPane.add(gamePane, BorderLayout.PAGE_START); 
SnLFrame.setSize(720,600); 

구축 :

Coordinates position = new Coordinates(); 
position = convertToCoordinates(blockNumber); 
position.x = position.x * 50; 
position.y = position.y * 50; 

Dimension smallPlayer1IconLabelSize = smallPlayer1IconLabel.getPreferredSize(); 
smallPlayer1IconLabel.setBounds(position.x, position.y, smallPlayer1IconLabelSize.width, smallPlayer1IconLabelSize.height); 
gameBoardPane.add(smallPlayer1IconLabel, new Integer(100)); 
SnLFrame.invalidate(); 
SnLFrame.validate(); 
SnLFrame.repaint(); 

는 JLabel의 표시 않을 때 위치는 정확하지만, '아무튼 항상 표시하지 마십시오 ... 무엇이 잘못 되었습니까 ...

+2

[이 답변] (http://stackoverflow.com/a/4687759/522444)에서 비슷한 문제를 처리했습니다. 도움이 될지도 모릅니다. –

+0

하지만 더 도움이 필요하면 실행 불가능한 코드 스 니펫을 기반으로 잘못했을 수있는 일을 추측하는 것이 불가능할 수 있으므로 [sscce] (http://sscce.org)를 작성하고 게시하는 것을 고려하십시오. 지금까지 게시했습니다. –

답변

1

호버 크래프트의 이전 답변을 훑어 보았습니다. 당신의 요구를 충족시킬 것이라고 확신합니다. 그러나 나는 플레이에 저항 할 수 없었습니다.

이것은 예제로서 만 의미가 있습니다. 애니메이션 엔진 (인터넷에서 사용할 수 더 나은 프레임 워크가) 쓰레기이지만, 약간의 재미;) 문제는 내 위치 알고리즘에 실제로 있었다

public class AnimateGlassPane { 

    public static void main(String[] args) { 
     new MyFrame(); 
    } 

    public static class MyFrame extends JFrame { 

     public MyFrame() { 
      setTitle("Testing"); 
      setSize(800, 600); 
      setLocationRelativeTo(null); 
      setDefaultCloseOperation(EXIT_ON_CLOSE); 

      setGlassPane(new MyGlassPane()); 
      getGlassPane().setVisible(true); 

      setVisible(true);    
     } 

    } 

    public static class MyGlassPane extends JPanel { 

     private JLabel[] labels; 
     private JLabel random; 
     private Timer timer; 
     private float progress = 0f; 
     private float increment = 0.1f; 

     public MyGlassPane() { 

      setOpaque(false); 
      setLayout(null); 

      labels = new JLabel[4]; 
      labels[0] = new JLabel("Up/Down"); 
      labels[1] = new JLabel("Down/Up"); 
      labels[2] = new JLabel("Left/Right"); 
      labels[3] = new JLabel("Right/Left"); 

      for (int index = 0; index < labels.length; index++) { 
       labels[index].setSize(labels[index].getPreferredSize()); 
       add(labels[index]); 
      } 

      random = new JLabel("Look at me!"); 
      random.setSize(random.getPreferredSize()); 
      random.setVisible(false); 
      random.setOpaque(true); 
      random.setBackground(Color.RED); 
      random.setForeground(Color.YELLOW); 

      add(random); 

      timer = new Timer(125, new ActionListener() { 

       @Override 
       public void actionPerformed(ActionEvent e) { 
        progress += increment; 
        if (progress < 0) { 
         progress = 0; 
         increment = 0.1f; 
        } else if (progress > 1) { 
         progress = 1; 
         increment = -0.1f; 
        } 

        int width = getWidth() - 1; 
        int height = getHeight() - 1; 

        int x = (width - labels[0].getWidth())/2; 
        int y = Math.round((height * progress)); 
        if (y + labels[0].getHeight() > height) { 
         y = height - labels[0].getHeight(); 
        } else if (y < 0) { 
         y = 0; 
        } 

        labels[0].setLocation(x, y); 

        y = Math.round((height * (1f - progress))); 
        if (y + labels[1].getHeight() > height) { 
         y = height - labels[1].getHeight(); 
        } else if (y < 0) { 
         y = 0; 
        } 

        labels[1].setLocation(x, y); 

        y = (height - labels[2].getHeight())/2; 
        x = Math.round(width * progress); 
        if (x + labels[2].getWidth() > width) { 
         x = width - labels[2].getWidth(); 
        } else if (x < 0) { 
         x = 0; 
        } 

        labels[2].setLocation(x, y); 

        x = Math.round(width * (1f - progress)); 
        if (x + labels[3].getWidth() > width) { 
         x = width - labels[3].getWidth(); 
        } else if (x < 0) { 
         x = 0; 
        } 

        labels[3].setLocation(x, y); 

        repaint(); 

        int value = (int)Math.round(Math.random() * 100d); 
        if (value % 5 == 0) { 

         random.setVisible(!random.isVisible()); 
         x = (int)Math.round(Math.random() * width); 
         y = (int)Math.round(Math.random() * height); 

         if (x + random.getWidth() > width) { 
          x = width - random.getWidth(); 
         } else if (x < 0) { 
          x = 0; 
         } 
         if (y + random.getHeight() > height) { 
          y = height - random.getHeight(); 
         } else if (y < 0) { 
          y = 0; 
         } 

         random.setLocation(x, y); 

        } 
       } 
      }); 

      timer.setRepeats(true); 
      timer.setCoalesce(true); 
      timer.start(); 
     } 
    } 
}