2014-04-21 2 views
0

그래서 자바와 스윙을 사용하고 JSplitPane 양쪽에 균등하게 분할 된 창을 프로그램하려고합니다. 나는 JSplitPane을 가지고 있지만, 한면은 거의 전체 창 크기이고 다른 한면은 작습니다.JSplitPane을 균등하게 분할하는 방법은 무엇입니까?

package com.harrykitchener.backup; 

import javax.swing.*; 

import java.awt.*; 
import java.awt.event.*; 
import java.io.*; 

public class Main 
{ 
    private JMenuBar menuBar; 
    private JMenu fileMenu, editMenu, helpMenu; 
    private JPanel leftPanel, rightPanel; 
    private JButton openButton; 

    public Main() 
    { 
     JPanel mainCard = new JPanel(new BorderLayout(8, 8)); 
     menuBar = new JMenuBar(); 
     fileMenu = new JMenu("File"); 
     editMenu = new JMenu("Edit"); 
     helpMenu = new JMenu("Help"); 
     menuBar.add(fileMenu); 
     menuBar.add(editMenu); 
     menuBar.add(helpMenu); 
     mainCard.add(menuBar); 

     leftPanel = new JPanel(); 

     rightPanel = new JPanel(); 


     JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, rightPanel); 

     JFrame window = new JFrame("Pseudo code text editor"); 
     window.setJMenuBar(menuBar); 
     window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     window.getContentPane().add(splitPane); 
     window.setSize(1280, 720); 
     window.setLocationRelativeTo(null); 
     window.setVisible(true); 
    } 

    public static void main(String args[]) 
    { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       new Main(); 
      } 
     }); 
    } 

} 

enter image description here

답변

0

splitPane.setResizeWeight(0.5); 

사용

은 자세한 내용은 java doc를 참조하십시오.

1

다른 대답에서 언급 한 것처럼 setResizeWeight은 하나의 해결책 일 수 있습니다. 그러나이 ... 음, 은 크기 조정 가중치를으로 설정하여 원치 않는 방식으로 분할 창의 동작을 변경합니다.

실제로는 에 디바이더 위치를 설정하려면 일 수 있습니다. 이 경우, 당신은 분할 창 구현의 일부 특수성으로 인해 그러나

splitPane.setDividerLocation(0.5); 

, 부를 수있는,이 분할 창이 표시되었다 후 을 수행해야합니다. 그것은 다음

setDividerLocation(splitPane, 0.5); 

/** 
* Set the location of the the given split pane to the given 
* value later on the EDT, and validate the split pane 
* 
* @param splitPane The split pane 
* @param location The location 
*/ 
static void setDividerLocation(
    final JSplitPane splitPane, final double location) 
{ 
    SwingUtilities.invokeLater(new Runnable() 
    { 
     @Override 
     public void run() 
     { 
      splitPane.setDividerLocation(location); 
      splitPane.validate(); 
     } 
    }); 
} 

를 호출 할 수 있습니다 : 내 응용 프로그램의 경우, 나는 EDT에 그것을 설정하는 작업을 넣어 디바이더의 위치를 ​​설정 보류하는 작은 유틸리티 방법을 만들어

관련 문제