2012-02-11 6 views
4

paintComponent가있는 JPanel을 JFrame에 추가하는 데 문제가 있습니다.
이것이 프레임에 추가하는 유일한 경우 작동합니다. 그러나 레이아웃 관리자를 추가하고 JFrame에 다른 구성 요소를 추가하자마자 그림이있는 패널이 더 이상 표시되지 않습니다. ,프레임에 paintComponent 패널을 추가 할 수 없습니다.

내가 안녕하세요 칠려고하고 있지 않다 현실에서 (부호를 그립니다 패널이 :

작동 코드이며, JPanel을 성공적으로 표시됩니다 ...이 명확하게하려면

public class SignFrame extends JFrame { 

// the constructor method 
public SignFrame() { 

    super("This is the title of the Sign Frame"); 
    setSize(300,500); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    // make a container for the frame 
    Container content = getContentPane(); 

    // call from the drawing panel 
    SignPanel2 signpanel = new SignPanel2(); 
    // change class variable of SignPanel 
    signpanel.hello = 5; 
    signpanel.repaint(); 


    // add signpanel to container 
    content.add(signpanel); 

    setContentPane(content); 

    setVisible(true); 

} 

} 
: 여기에 단순히 코드)

public class SignPanel2 extends JPanel { 
public int hello; 

public void paintComponent(Graphics comp) { 
    Graphics g = (Graphics) comp; 
    g.setColor(Color.LIGHT_GRAY); 
    g.fillRect(70, 250, 150, 150); 

    g.setColor(Color.BLACK); 

    if (hello > 0) 
     g.drawString("h",135, 270); 

    if (hello > 1) 
     g.drawString("h e",135, 270); 

    if (hello > 2) 
     g.drawString("h e l",135, 270); 

    if (hello > 3) 
     g.drawString("h e l l",135, 270); 

    if (hello > 4) 
     g.drawString("h e l l o",135, 270); 
} 

} 

난에 패널을 넣어 프레임이다

메인 클래스

public class TheSignMain { 

public static void main (String[] args) { 

    SignFrame signframe = new SignFrame(); 

} 

} 

위의 작품을 완벽하게 정상적으로 나에게 그것에서 원하는 그림과 프레임을 제공합니다.

프레임에 다른 구성 요소를 추가하고 레이아웃 관리자를 추가하면 더 이상 그림이 표시되지 않습니다. repaint()를 사용하더라도.
레이아웃 관리자를 포함해야합니다. 그렇지 않으면 그림이있는 패널을 추가하지만 다른 구성 요소는 추가하지 않습니다. 이것은 현재 프레임 클래스가 보이는 방식이며, 여기에서 문제가 발생합니다.

공용 클래스 SignFrame 내가 자바에 완전히 새로운 오전, 그래서 어떤 도움이 많이 이해할 수있을 것이다 JFrame의 {

// the constructor method 
public SignFrame() { 

    super("This is the title of the Sign Frame"); 
    setSize(300,500); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    // make a container for the frame 
    Container content = getContentPane(); 


    // need a layout manager to decide the arrangement of the components on the container 
    FlowLayout flo = new FlowLayout(); 
    // designate the layout manager to the container 
    content.setLayout(flo); 


    // make components 
    JPanel buttons = new JPanel(); 
    JButton play = new JButton("Play"); 
    JButton pause = new JButton("Pause"); 
    JButton stop = new JButton("Stop"); 
    // add components to a panel 
    buttons.add(play); 
    buttons.add(pause); 
    buttons.add(stop); 
    // add panel to frame container 
    content.add(buttons); 


    // call from the drawing panel 
    SignPanel2 signpanel = new SignPanel2(); 
    // change class variable of SignPanel 
    signpanel.hello = 5; 
    signpanel.repaint(); 


    // add signpanel to container 
    content.add(signpanel); 

    setContentPane(content); 

    setVisible(true); 

} 

} 

을 확장합니다. 모든 코드와 도움에 감사드립니다.

+1

+1하지만, 좋은 질문입니다. 오늘 무언가를 배울 수있게 도와주었습니다 :-) –

+0

@Gagandeep ... 나는 당신의 대답에 대해 무엇을했는지 모르겠습니다. JB Nizet 옆에있는 틱 아이콘을 클릭하여 허용 된 답변 (즉, 문제가 해결됨)을 표시했으나 회신이 사라졌습니다. 다른 답장은 삭제하지 않을 것이라고 생각했습니다. 사과드립니다. 오늘 등록 만 했으므로 포럼이 운영되는 방식에 익숙하지 않습니다. – phy2sma

+0

Nah는 고의적이었습니다. 나는 그 자신을 삭제했습니다. 당신이 원했던 것과 JB Nizet이 당신에게 말했던 것이 그토록 좋습니다. 그냥 미소 지어주세요, 걱정할 것 :-) –

답변

4

테스트를 거치지는 않았지만 흐름 레이아웃에서 패널의 기본 크기를 사용하고 있으며 [0, 0] 차원이 아닌 다른 값을 반환하려면 getPreferredSize()을 재정의하지 않았을 것입니다.

setHello() 메서드에서 hello 변수의 변경을 캡슐화하여 repaint()을 호출해야합니다. 호출 코드는 다시 칠할 필요가 없습니다. panl은 언제 다시 칠해야 하는지를 알아야하고 repaint 자체를 호출해야합니다.

+0

안녕하세요, 빠른 답변에 감사드립니다. 매우 감사. 나는 코딩에 정말 새롭다. 나는 일주일 전에 학습과 글쓰기를 시작했다. 정확히 어디에 getPrefferedSize() 행을 넣을까요? 프레임이있는 수업 에서요? signpanel.getPrefferedSize (150,150); – phy2sma

+0

실제로 코딩을 시도하고 'getPrefferedSize() 메소드가 SignPanel2 유형에 대해 정의되지 않았습니다'라고 말합니다. .. – phy2sma

+0

메소드의 이름은 getPreferredSize()이며 인수를 사용하지 않습니다. 사용자 정의 패널 (SignPanel2)에 구현해야하며 JComponent의 getPreferredSize() 메소드를 재정의해야합니다. http://docs.oracle.com/javase/6/docs/api/javax/swing/JComponent.html#getPreferredSize% 28 % 29 –

관련 문제