2014-06-05 8 views
0

일부 GUI 객체 중첩 작업을하고 있습니다. 나는 그것의 대부분을 지금까지 작동시키지 만, 내가 알 수없는 가장 최근의 부분을 얻었습니다. 그것은 간단한 문제일지도 모르고 중첩의 깊이가 문제를 일으키는 것은 아니라고 생각합니다. 그렇지만 내가 가지고있는 것을 열거 할 것입니다.JSplitPane 내의 JScrollPane에 JPanel 추가

지금까지 내가 가지고 JFrame 포함하는 JTabbedPane이있는 JPanel, 그것은이 JTabbedPane이있다 2 JPanels. 현재 JPanels 중 하나를 작업하고 있습니다. 및 JSplitPane을 포함하는 JSplitPane이 포함되어 있습니다. 두 번째 JSplitPane에는 두 개의 JScrollPane이 있습니다. JScrollPane 중 하나는 JPanel입니다.

이것은 모두 마지막으로 JPanel까지 올바르게 작동합니다. 제대로 작동하는지 테스트하기 위해 배경을 추가했습니다. 내 문제는 배경이 기본적으로 JScrollPane 내의 테두리로 표시된다는 것입니다. 배경이없는 것처럼 패널의 내부는 회색입니다.

여기 내 코드 중 일부입니다. 누군가가 유용하다고 생각한다면 더 많은 것을 제공 할 수 있습니다.

이것은 JTabbedPane 내에있는 JPanel입니다.

public class MapEditor extends JPanel{ 

    int height, width; 
    final int DIVIDER_SIZE = 7; 
    final int HORIZONTAL_DIVIDER_PLACE = 300; 
    final int VERTICAL_DIVIDER_PLACE = 200; 

    //side bar divider separates main page and left (vertical divider) 
    //side bar division separates top and bottom of sidebar (horizontal divider) 
    JSplitPane sideBarDivider; 
    JSplitPane sideBar; 

    //scroll pane to hold tiles panel, map panel 
    JScrollPane toolContainer; 
    JScrollPane tileContainer; 
    JScrollPane mapContainer; 

    //panels held inside JScrollPanes above 
    JPanel toolPanel; 
    JPanel tilePanel; 
    JPanel mapPanel; 

    public MapEditor(int w, int h){ 
     this.width = w; 
     this.height = h; 
     setLayout(new BorderLayout()); 
     //we can just pass the final variables in here because it's in the top left 
     toolPanel = new ToolPanel(HORIZONTAL_DIVIDER_PLACE, 
       VERTICAL_DIVIDER_PLACE); 
     sideBar = splitScreen(); 
     add(sideBar); 
    } 

    //main split between map and tools/tiles 
    public JSplitPane splitScreen(){ 
     mapContainer = new JScrollPane(); 
     sideBar = buildSideBar(); 
     //add map container and sidebar 
     sideBarDivider = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, 
       sideBar, mapContainer); 
     sideBarDivider.setSize(width, height); 
     //set divider size and position 
     sideBarDivider.setDividerLocation(HORIZONTAL_DIVIDER_PLACE); 
     sideBarDivider.setDividerSize(DIVIDER_SIZE); 
     return sideBarDivider; 
    } 

    //small split between tools and tiles 
    public JSplitPane buildSideBar(){ 
     toolContainer = new JScrollPane(); 
     toolContainer.add(toolPanel); 
     tileContainer = new JScrollPane(); 
     //add tile & tool containers 
     sideBar = new JSplitPane(JSplitPane.VERTICAL_SPLIT, 
       toolContainer, tileContainer); 
     //set divider size and position 
     sideBar.setDividerSize(DIVIDER_SIZE); 
     sideBar.setDividerLocation(VERTICAL_DIVIDER_PLACE); 
     return sideBar; 
    } 
} 

이것은 제게 문제가되는 패널입니다.

public class ToolPanel extends JPanel{ 

    int width, height; 

    public ToolPanel(int w, int h){  
     this.width = w; 
     this.height = h; 
     setLayout(new BorderLayout()); 
     setSize(w, h); 
     setBackground(Color.BLACK);  
    } 
} 

아무도 내가 잘못 아무것도 볼? 필자는 Java GUI 구성 요소에 익숙하지 않으며 누구 에게든 도움을 주어야 할 것입니다.

답변

2
toolContainer = new JScrollPane(); 
    toolContainer.add(toolPanel); 

JScrollPane에 구성 요소를 추가하지 마십시오. 구성 요소가 스크롤 창의 "뷰포트"에 추가됩니다.

당신은 사용할 수 있습니다

toolContainer = new JScrollPane(toolPanel); 

나 또한

toolContainer = new JScrollPane(); 
    toolContainer.setViewportView(toolPanel); 

, 사용해서는 안 :

setSize(w, h); 

구성 요소의 레이아웃 매니저의 추천 사이즈를 결정합니다 패널에 추가 된 comopnents에 기반한 구성 요소.

사용자 정의 페인팅을 수행하는 경우 적절한 크기를 반환하려면 ToolPanel의 getPreferredSize() 메소드를 대체해야합니다.

+0

문제를 해결해 주셔서 감사합니다. –