2013-01-18 2 views
1

콘텐츠 창이있는 JFrame이 있습니다. JMenuBar는 창 북쪽에 도킹되어 있으며 남쪽에는 JLabel (상태 표시 줄)이 있습니다.MiG Layout, JScrollBar 및 JTabbedPane

가운데는 JTabbedPane입니다. 각 탭은 "문서"입니다. 뷰포트에 JScrollBar와 JPanel이 포함되어 있습니다.

계속됩니다 (뷰포트의 JPanel에는 JPanels가 더 많거나 더 많을 수 있습니다 ...)하지만이 예제에서는 JPanel (뷰포트에서)이 또는 창 공간에 맞지 않을 수 있으므로 (화면에서 스크롤 막대를 표시 할 수 없거나 강제로 표시 할 수 있음)

창에 맞으면 모든 것이 좋지만 창 높이에 맞추기가 너무 어려울 때 JMenuBar가 맨 위에 부숴집니다.

저는 JMenuBar의 절대 높이를 지정하지 않아도 작동 할 수 있지만 값이 싸지는 않습니다. 처음부터 발생하지 않아야합니다.

여기에 SCCE가 있습니다 (실제로 짧지는 않지만 37 ~ 117 행만보아야하며 // TODO를 사용하여 레이아웃과 관련된 모든 행을 표시했습니다). 또한 문제가 발생했는지 또는 발생하지 않는지 확인하려면 2000 년과 200 년 사이의 88 행에서 높이 값을 변경하십시오. 물론 MiG 레이아웃 라이브러리도 필요합니다.

여기에 코드입니다 :

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.KeyEvent; 
import java.util.ResourceBundle; 

import javax.swing.BorderFactory; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JMenu; 
import javax.swing.JMenuBar; 
import javax.swing.JMenuItem; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTabbedPane; 
import javax.swing.KeyStroke; 
import javax.swing.ScrollPaneConstants; 
import javax.swing.SwingUtilities; 
import javax.swing.UIManager; 
import javax.swing.UIManager.LookAndFeelInfo; 
import javax.swing.UnsupportedLookAndFeelException; 

import net.miginfocom.swing.MigLayout; 

class Menu extends JMenuBar 
{ 
    private static final long serialVersionUID = 1L; 

    public Menu() 
    { 
     JMenu fileMenu = new JMenu("file"); 
     this.add(fileMenu); 
    } 
} 

class DisplayGUI 
{ 
    JTabbedPane documentSelector; 

    void addNewDocument(String name) 
    { 
     Document newDocument = new Document(); 
     newDocument.addChapter(new Chapter(), 1); 
     documentSelector.add(newDocument, name); 
    } 

    public DisplayGUI() 
    { 
     JFrame masterWindow = new JFrame("name"); 

     masterWindow.setSize(1100, 800); 
     masterWindow.setMinimumSize(new Dimension(400, 400)); 
     masterWindow.setLocationRelativeTo(null); 
     masterWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     JPanel rootPanel = new JPanel(); 
     rootPanel.setLayout(new MigLayout()); //TODO Here is layout set for the content pane of the main JFrame 

     Menu menuBar = new Menu(); 
     rootPanel.add(menuBar, "span, north"); //TODO Here is menu bar added to the JFrame, it's docked north 

     JLabel statusBar = new JLabel("Welcome to PLabScript editor! Press File>New to create a new file or go to File>Open to open an existing one."); 
     statusBar.setOpaque(true); 
     statusBar.setBorder(BorderFactory.createLoweredSoftBevelBorder()); 
     rootPanel.add(statusBar, "span, south"); //TODO Here is status bar added to the JFrame, it's docked south 

     documentSelector = new JTabbedPane(JTabbedPane.NORTH); //TODO JTabbedPane set so the tab chooser is on the top 
     rootPanel.add(documentSelector, "grow, push"); //TODO setup so it will take up all the remaining space 


     addNewDocument("Brand new document");  


     masterWindow.setContentPane(rootPanel); 
     masterWindow.setVisible(true); 
    } 
} 

class Document extends JScrollPane 
{ 
    private static final long serialVersionUID = 1L; 
    JPanel basePanel; 

    //methods 
    void addChapter(Chapter chapter, int index) 
    { 
     basePanel.add(chapter, "grow, push, h 2000", index-1); //TODO this here adds a chapter to the basePanel of the JScrollPane which is the a representative of a single document 
     //TODO it height is set to 2000 (and the problem occurs), but if you reduce it enough so it fits the window, problem will dissaper 
    } 

    //constructors 
    public Document() 
    { 
     super(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED); 
     getVerticalScrollBar().setUnitIncrement(20); 

     basePanel = new JPanel(); 
     basePanel.setBackground(Color.RED); 
     basePanel.setLayout(new MigLayout("insets 0")); //TODO "insets 0" is so there is no border thingy around all of the child components 

     setViewportView(basePanel); 
    } 

} 

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

    //constructors 
    Chapter() 
    { 
     setLayout(new MigLayout("insets 0")); //TODO "insets 0" is so there is no border thingy around all of the child components 
     setBackground(Color.MAGENTA); 
    } 
} 


public class Main 
{ 
    public static ResourceBundle language; 

    static boolean setUpLAF() 
    { 
     for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) 
     { 
      if ("Nimbus".equals(info.getName())) 
      { 
       try 
       { 
        UIManager.setLookAndFeel(info.getClassName()); 
       } 
       catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) 

       { 
        return false; 
       } 

       break; 
      } 
     } 
     return true; 
    } 


    public static void main(String[] args) 
    {  
     //SetUpLookAndFeel 
     setUpLAF(); 

     //Display actual GUI 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      @Override 
      public void run() 
      { 
       new DisplayGUI(); 
      } 
     }); 
    } 
} 
+0

miglayout의 버전은 무엇입니까? – Archer

+0

4.0, 나는 얼마 전 그것을 얻었다. – Karlovsky120

+0

버그일까요? 개발자에게 제출해야합니까? – Karlovsky120

답변

1

라인 88 읽어야합니다

basePanel.add(chapter, "grow, push", index-1); //TODO this here adds a chapter to the basePanel of the JScrollPane which is the a representative of a single document 

라인 (100) 읽어야합니다

basePanel.setLayout(new MigLayout("fill,insets 0")); //TODO "insets 0" is so there is no border thingy around all of the child components 

이보십시오.

+1

시도해 보았습니다. 그러나 구성 요소의 절대 높이를 설정하는 것을 피할 수는 없습니다. 나는이 특정 높이를 설정할 필요는 없지만 머리글과 바닥 글과 함께 미리 정의 된 높이를 가진 "페이지"를 포함합니다. 그래서 저는 88 번 줄을 정말로 바꿀 수 없었습니다. (그것은 이미 내 프로그램에서 그렇습니다. 그러나 그것들은 모두 높이를 정의했습니다.) 그리고 no를 변경합니다. 100은 아무것도하지 않았다. – Karlovsky120