2017-01-03 10 views
1

를 왼쪽으로 추가하고, 나는 두 가지 구성 요소, 다른 상단에 하나를 배치하고 싶습니다.BoxLayout에서는 내가 <code>BoxLayout</code> (페이지 축)이있는 JPanel에있어 마진

enter image description here

내 문제는 어떻게이 제거 할 수있는 큰 lipsum 상자의 왼쪽에 여백을 무엇입니까? 상위 구성 요소를 추가하지 않으면 여백이 없습니다. 여기

enter image description here

번째 화상 headerPanel를 첨가하지 않음으로써 만들어 내 코드이다 :

JLabel commandLabel = new JLabel(command); 
    JLabel paramLabel = new JLabel(params); 
    JLabel descLabel = new JLabel("<html><body style='width: 200px;'>" + description + "</body></html>"); 
    Font baseFont = commandLabel.getFont(), commandFont, paramFont, descFont; 

    commandFont = baseFont.deriveFont(Font.BOLD); 
    paramFont = baseFont.deriveFont(Font.ITALIC); 
    descFont = baseFont.deriveFont(Font.PLAIN); 

    commandLabel.setFont(commandFont); 
    paramLabel.setFont(paramFont); 
    descLabel.setFont(descFont); 
    descLabel.setAlignmentX(LEFT_ALIGNMENT); 
    descLabel.setBorder(BorderFactory.createStrokeBorder(new BasicStroke())); 
    JPanel headerPanel = new JPanel(new FlowLayout(FlowLayout.LEADING)); 
     headerPanel.add(commandLabel); 
     headerPanel.add(paramLabel); 
    this.add(headerPanel); 
    this.add(descLabel); 

이 클래스 JPanel 연장하고

을 D '단순히 pack()이다하는 JFrame에 첨가
+2

'여기 내 code'의 - A는 적절한 [mcve] 문제를 보여줍니다 해당 게시물. 제공된 코드는 컴파일/실행할 수 없습니다. – camickr

답변

1

관찰 된 행동이 접점에 중간 JPanel를 사용하여, 예상되는 디스플레이가 얻을 수있는 출처 나는 말할 수 있지만

JLabel commandLabel = new JLabel(command); 
    JLabel paramLabel = new JLabel(params); 
    JLabel descLabel = new JLabel("<html><body style='width: 200px;'>" + description + "</body></html>"); 
    Font baseFont = commandLabel.getFont(), commandFont, paramFont, descFont; 

    commandFont = baseFont.deriveFont(Font.BOLD); 
    paramFont = baseFont.deriveFont(Font.ITALIC); 
    descFont = baseFont.deriveFont(Font.PLAIN); 

    commandLabel.setFont(commandFont); 
    paramLabel.setFont(paramFont); 
    descLabel.setFont(descFont); 
    descLabel.setAlignmentX(LEFT_ALIGNMENT); 
    descLabel.setBorder(BorderFactory.createStrokeBorder(new BasicStroke())); 
    JPanel headerPanel = new JPanel(new FlowLayout(FlowLayout.LEADING)); 
    JPanel descPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));// added 
    headerPanel.add(commandLabel); 
    headerPanel.add(paramLabel); 

    descPanel.add(descLabel);// added 

    this.add(headerPanel); 
    this.add(descPanel);// modified 
+0

나는 (배터리에서 dev에 기계)이, 감사를 시도 할 것이다 –

1

내 문제가 큰 lipsum 상자의 왼쪽에 여백, 내가이 제거 할 수있는 방법 : 라벨에, 오히려 JLabel 직접 추가하는 것보다?

당신은 당신의 구성 요소의 정렬의 일관성을 확인해야합니다. 모든 구성 요소의 정렬 "X"속성이 왼쪽 정렬되어야합니다.

나는 JLabel가 중심이 사용할 필요가 있도록 정렬 같은데요 :

descLabel.setAlignmentX(JLabel.LEFT_ALIGNMENT); 

이 더 많은 정보와 예제 How to Use BoxLayout에 스윙 튜토리얼 Fixing Alignment Problems 섹션을 참조하십시오.