2012-08-04 4 views
1

현재 새 게임을위한 Java에서 사용자 정의 UI를 만드는 중입니다. 그리고 저는 지금 창을 만들고 있습니다. 어쨌든 창 (JPanel)을 만들고 그 위에 다른 메인 패널을 추가하면 메인 컨텐츠에 대해 메인 패널이 두 개의 다른 위치, 올바른 위치 및 왼쪽 상단에 한 번 두 번 그려집니다 . 사진과 같이 나타냅니다JPanel의 JPanel이 다른 위치에 두 번 그려집니다.

Image of the JPanel drawn twice in different locations
중앙 버튼은 올바른이며, 왼쪽 상단이 아닌 동안 제대로 자리 잡고 있습니다. 검은 색은 기본 패널의 배경입니다.

그리고 여기에 내가 만들려고 해요 윈도우의 코드입니다 :

package gui.elements; 

import graphic.CutSprite; 
import graphic.SpriteStorage; 
import gui.CFont; 

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 

import javax.swing.JPanel; 

public class CWindow extends JPanel { 
    private static final long serialVersionUID = 1L; 

    // The main panel, on which components in the window are to be placed 
    private JPanel panel; 

    private String title; 

    public CWindow(String title) { 
     this(title, 380, 380); 
    } 

    public CWindow(String title, int width, int height) { 
     this.title = title; 

     // Place the main panel of the window 
     panel = new JPanel(); 
     panel.setBackground(Color.BLACK); 
     add(panel); 
    } 

    @Override 
    public void paintComponent(Graphics graphics) { 
     super.paintComponents(graphics); 
    } 

    public JPanel getPanel() { 
     return panel; 
    } 

} 

을 그리고 여기 CWindow 인스턴스화 및 추가 프레임의 :

package gui; 

import java.awt.Color; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

import gui.elements.CWindow; 

public class Screen { 

    private static Screen single = new Screen(); 
    public static Screen get() { return single; } 

    private JFrame frame; 
    private PanelManager panelManager; 
    private ScreenCanvas screenCanvas; 

    /** 
    * Constructor, set the window, and initialize the game. 
    */ 
    public Screen() { 
     frame = new JFrame("Game"); 

     // Frame (window) settings 
     frame.setSize(860, 540); 
     frame.setLocationRelativeTo(null); //Open window in center of screen 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     CWindow w = new CWindow("This is a window!"); 
     frame.add(w); 

     JButton tf9 = new JButton("Dunno?"); 
     w.getPanel().add(tf9); 

     // Display the window 
     frame.setVisible(true); 
    } 


    /** 
    * @return the height of the screen 
    */ 
    public static int getHeight() { 
     return get().frame.getHeight(); 
    } 

    /** 
    * @return the width of the screen 
    */ 
    public static int getWidth() { 
     return get().frame.getWidth(); 
    } 


    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     Screen.get(); 
    } 

} 

답변

3

이 좋아이 발견 대답, 이상한 충분히. xD 창피를 게시 한 후 6 분.

좋아

, 왜 문제가 CWindow 클래스

@Override 
public void paintComponent(Graphics graphics) { 
    super.paintComponents(graphics); 
} 

에 어떤 생각 다음 코드에서 super.paintComponents(graphics);을 없었다,하지만 난 그 라인을 제거 할 때 일했다.

+0

나 자신을 쓰려고했다. D –

+2

대신에 super.paintComponent (graphics);를 호출하면 정상적으로 작동 할 것이라고 확신한다. (.paintComponent에서 's'의 부족을 확인하십시오.) :) –

+0

아름다운, 세미콜론 오류처럼;) 감사합니다 :-) –