2010-02-27 2 views
2

JTabbedPane에 2 개의 JPanel이 있고 두 번째 패널이 선택된 패널 (즉, 볼 수있는 패널) 인 경우에도 첫 번째 패널 (해당 애니메이션)의 패널에서 update (g)가 호출되면) 업데이트 된 패널이 화면에 나타납니다. 왜 이런 행동을하며 어떻게이 행동을 피할 수 있습니까?JTabbedPane 스윙 업데이트 오류

답변

5

JComponent의 방법은 "배경을 지우지 않습니다." 보통 examplesJTabbedPane 인 경우 일반적으로 update()을 사용할 필요가 없습니다. 사용량을 나타내는 sscce이 도움이 될 수 있습니다.

부록 1 : update()으로 전화하는 이유가 명확하지 않습니다. 아래는 이상 현상을 나타내지 않는 간단한 애니메이션입니다.

부록 2 : Painting in AWT and Swing: paint() vs. update()을 참조하십시오. actionPerformed()repaint()을 대신 사용할 수 있습니다. 업데이트 (g)가 제 패널 내부 패널 에서 호출

enter image description here

import java.awt.*; 
import java.awt.event.*; 
import java.util.Random; 
import javax.swing.*; 

public class JTabbedTest { 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      //@Override 
      public void run() { 
       JFrame f = new JFrame(); 
       f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

       JTabbedPane jtp = new JTabbedPane(); 
       jtp.setPreferredSize(new Dimension(320, 200)); 
       jtp.addTab("Reds", new ColorPanel(Color.RED)); 
       jtp.addTab("Greens", new ColorPanel(Color.GREEN)); 
       jtp.addTab("Blues", new ColorPanel(Color.BLUE)); 

       f.add(jtp, BorderLayout.CENTER); 
       f.pack(); 
       f.setVisible(true); 
      } 
     }); 
    } 

    private static class ColorPanel extends JPanel implements ActionListener { 

     private final Random rnd = new Random(); 
     private final Timer timer = new Timer(1000, this); 
     private Color color; 
     private int mask; 
     private JLabel label = new JLabel("Stackoverflow!"); 

     public ColorPanel(Color color) { 
      super(true); 
      this.color = color; 
      this.mask = color.getRGB(); 
      this.setBackground(color); 
      label.setForeground(color); 
      this.add(label); 
      timer.start(); 
     } 

     //@Override 
     public void actionPerformed(ActionEvent e) { 
      color = new Color(rnd.nextInt() & mask); 
      this.setBackground(color); 
     } 
    } 
} 
+0

JTabbedPane에 대한 업데이트를 호출하지 않습니다. JTabbedPane의 패널 중 하나에있는 구성 요소에서 호출됩니다. 문제는 해당 패널이 상주하는 경우에도 구성 요소가 항상 화면에 그려지는 것입니다. 선택한 패널. – dominic

+0

ActionPerformed()에 update()를 추가하면 문제가 발생합니다. – dominic

+0

위의 추가 링크를 참조하십시오. – trashgod

2

(ITS 애니메이션)

업데이트를 오버라이드 (...) 방법 오래된 AWT 트릭이므로 Swing과 함께 사용하면 안됩니다.

적절한 방법은 Custom Painting에있는 스윙 튜토리얼의 섹션을 참조하십시오. 또한 애니메이션의 경우 자습서에있는 "Swing Timer 사용법"을 읽으십시오.

+0

+1'update()'를 오버라이드합니까? 누가 알았 겠어! – trashgod

+0

어떻게 오버라이드하거나 심지어 update()를 호출해도 내 문제가 발생합니까? – dominic

+0

IIUC, AWT와 스윙을 혼합합니다. – trashgod