2014-04-04 2 views
0

MigLayout을 사용하여 부모 요소의 높이를 높이기 위해 표현식을 사용하려고합니다. "height 100 %"그러나 MigLayout에는 일종의 피드백 루프가있어 높이가로드 될 때 최대 값으로 이동합니다. 이 문제는 MiGPanel이 JScrollPane에 포함될 때만 발생합니다. JScrollPane을 처리하는 코드 행을 제거하고 MiGPanel을 JFrame에 추가하면 프로그램이 의도 한대로 실행됩니다. 이 과자를보기 위해 부울 문을 추가했습니다.Java에서 MigLayout을 사용하여 높이 또는 너비가 100 %로 설정된 경우 하위가 부모의 경계를 벗어나 확장됩니다.

참고 : 이 수정 프로그램을 보았지만 프로그램 하단에 여백이없는 솔루션을 사용하고 싶습니다. 여기 How to prevent MigLayout from exceeding a container's bounds

코드입니다 :

import java.awt.BorderLayout; 
import java.awt.Color; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import net.miginfocom.swing.MigLayout; 

public class StackOverFlowQuestion { 
    public static void main(String[] args) { 

     JPanel MiGPanel = new JPanel(); 
     MiGPanel.setBackground(Color.red); 
     MiGPanel.setLayout(new MigLayout()); 

     JPanel greenPanel = new JPanel(); 
     greenPanel.setBackground(Color.green); 
     MiGPanel.add(greenPanel, "pos 0 0, width 33%, height 100%"); 

     JPanel yellowPanel = new JPanel(); 
     yellowPanel.setBackground(Color.yellow); 
     MiGPanel.add(yellowPanel, "pos 33% 0, width 33%, height 100%"); 

     JPanel bluePanel = new JPanel(); 
     bluePanel.setBackground(Color.blue); 
     MiGPanel.add(bluePanel, "pos 66% 0, width 34%, height 100%"); 


     JFrame mainFrame = new JFrame(); 
     mainFrame.setLayout(new BorderLayout()); 

     boolean scroll = true; 
     if(scroll){ 
      JScrollPane scrollPane = new JScrollPane(); 
      scrollPane.add(MiGPanel); 
      scrollPane.setViewportView(MiGPanel); 
      mainFrame.add(scrollPane); 
     }else{ 
      mainFrame.add(MiGPanel); 
     } 

     mainFrame.add(scrollPane); 
     mainFrame.pack(); 
     mainFrame.setSize(900, 600); 
     mainFrame.setVisible(true); 
     mainFrame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 
    } 
} 
+0

이 문제가있는 유일한 사람이 될 수 있습니까? –

답변

0

많은 잠못드는 밤 이후, 나는 해결책을했다 :

greenPanel.setLayout(new BorderLayout()); 
    greenPanel.add(new JLabel("test")); 
    MiGPanel.add(greenPanel, "pos 0 0 33% 100%"); 

    JPanel yellowPanel = new JPanel(); 
    yellowPanel.setBackground(Color.yellow); 
    MiGPanel.add(yellowPanel, "pos 33% 0 66% 100%"); 

    JPanel bluePanel = new JPanel(); 
    bluePanel.setBackground(Color.blue); 
    MiGPanel.add(bluePanel, "pos 66% 0 100% 100%"); 

절대 요소와 함께 사용하지 말아야 위치와 높이 대신, 당신 속성 "pos xy x1 y1"메서드를 사용해야합니다.

관련 문제