2013-01-07 3 views
0

2 시간 동안 Scrollbar를 사용하여 JEditorPane을 만들려고 시도했지만 나는 포기할 것입니다!왜 프레임이 비어 있습니까?

이 내 코드의 일부입니다

JEditorPane editorPane = new JEditorPane(); 
    URL helpURL = GUIMain.class 
      .getResource("/resources/einleitungstext1.html"); 
    this.setLayout(new GridBagLayout()); 
    GridBagConstraints c = new GridBagConstraints(); 
    try { 
     editorPane.setPage(helpURL); 
    } catch (IOException e) { 
     System.err.println("Attempted to read a bad URL: " + helpURL); 
    } 
    editorPane.setEditable(false); 
    JScrollPane editorScrollPane = new JScrollPane(editorPane); 
    editorScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
    editorScrollPane.setMinimumSize(new Dimension(100, 100)); 
    editorScrollPane.setPreferredSize(new Dimension(main.screenWidth-200, main.screenHeight-200)); 
    c.gridx = 0; 
    c.gridy = 0; 
    this.add(editorScrollPane, c); 
    this.setVisible(true); 

내가 this.add 할 때 (editorScrollPane를, C) 프레임이 비어있는,하지만 난 this.add 할 때 (editorPane를, C) 패널이 표시되어 . this.add (새 JLabel ("test"), c) 프레임도 비어 있습니다.

어디에서 오류가 있습니까?

감사합니다.

P. 꽤 큰 코드이므로 전체 코드를 게시 할 수 없습니다.

+0

어떤 수업이 있나요? 그것은 무엇으로부터 물려 받습니까? – matts

답변

3
  1. 편집기 창로드합니다 내용은,이 시간에 의해 컨테이너가 배치, 내용이 아직 사용중인
  2. 레이아웃 매니저로드와되지 않은에 대한 준비가되어 있음을 의미 할 수있다 당신이 제공 한 제약 조건은 내용의 필요를 충족시키기에 충분하지 않을 수도있는 스크롤 창의 기본 크기를 사용한다는 것을 의미합니다 (이것은 스크롤 창의 기능입니다. 이것은 디자인 된 방식입니다).

enter image description here

public class TestLayout18 { 

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

    public TestLayout18() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       JFrame frame = new JFrame(); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 

       JEditorPane editorPane = new JEditorPane(); 
       try { 
        editorPane.setPage(new URL("http://docs.oracle.com/javase/6/docs/api/javax/swing/JScrollPane.html")); 
       } catch (IOException e) { 
        System.err.println("Attempted to read a bad URL"); 
       } 
       editorPane.setEditable(false); 
       JScrollPane editorScrollPane = new JScrollPane(editorPane); 
       frame.add(editorScrollPane); 

       frame.setSize(400, 400); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 
} 
( BorderLayout 같은) 구성 요소의 적절한 사이즈에 의존하지 않는 사용 가능한 공간이나 레이아웃 매니저의 이상을 사용하는 격려 GridBagLayout-

어느 공급 제약

+0

대단히 감사합니다! – iliketocodeandstuff

0

editorPane에 적절한 크기를 설정합니다. scrollPane는 뷰포트 크기로 찾고 있습니다. 프레임의 최소 크기도 설정할 수 있습니다. 백그라운드에서

+0

editorPane에 기본 크기를 설정하면 아무 것도 변경되지 않습니다. 프레임은 어쨌든 최대화됩니다. 다른 아이디어있어? – iliketocodeandstuff

+0

그리고 왜 단순한 JLabel조차 작동하지 않습니까? – iliketocodeandstuff

+0

뷰포트는 일반적으로 콘텐츠의 기본 크기를 신경 쓰지 않습니다. 즉, 'Scrollable'인터페이스에서 제공하는 정보 (사용 가능한 경우)를 사용합니다. 이 생각을 실제로 따르고 싶다면'Scrollable' 인터페이스를 구현하고'Scrollable # getPreferredScrollableViewportSize'에서 적절한 값을 리턴하는 커스터마이징 된 버전의 편집기를 만드는 것이 더 낫습니다. 몇 가지 개조 또는 다른 레이아웃 관리자가 문제를 해결할 것입니다 – MadProgrammer

관련 문제