2014-02-22 4 views
-1

내 JFrame의 오른쪽에 JXTaskPaneContainer이 있는데 하이퍼 링크와 레이블이 있습니다. 윈도우의 주요 부분은 다음과 같아야합니다창 크기를 조정할 때 JXTaskPaneContainer가 잘못 커짐

  • 텍스트의 왼쪽에 큰 지역은
  • 메시지와 오른쪽 조치 이제

에 작은 영역, 문제는 추가 점이다

enter image description here

그것은을 가진 경우에도 괴상 가져옵니다 새로운 요소가 많이 컨테이너가 왼쪽으로 성장하고 텍스트를 통해 원인 JXTaskPaneContainerjavax.swing.JScrollPane(video/yt)입니다.

문제는 JXLabel과 관련이있는 것처럼 보입니다. 단어 줄 바꿈이 의심 스럽습니까? 이 문제를 해결하는 방법에 대한 아이디어가 있으십니까?

[예] 요청에 따라 독립된 예제입니다. Full source on github.

package layoutproblemSSCEC; 

import java.awt.BorderLayout; 
import java.awt.Container; 
import java.awt.EventQueue; 
import java.awt.event.ActionEvent; 
import java.beans.PropertyChangeListener; 

import javax.swing.AbstractAction; 
import javax.swing.Action; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JScrollPane; 
import javax.swing.JTextArea; 

import org.jdesktop.swingx.JXButton; 
import org.jdesktop.swingx.JXLabel; 
import org.jdesktop.swingx.JXTaskPane; 
import org.jdesktop.swingx.JXTaskPaneContainer; 

public class SwingWindow { 

    private JFrame frame; 
    String dolorem = "Sed ut perspiciatis unde omnis iste natus error sit " 
      + "voluptatem accusantium doloremque laudantium, totam rem aperiam, " 
      + "eaque ipsa quae ab illo inventore veritatis et quasi architecto " 
      + "beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem " 
      + "quia voluptas sit aspernatur aut odit aut fugit, sed quia " 
      + "consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. " 
      + "Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, " 
      + "consectetur, adipisci velit, sed quia non numquam eius modi tempora " 
      + "incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut " 
      + "enim ad minima veniam, quis nostrum exercitationem ullam corporis " 
      + "suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? " 
      + "Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse " 
      + "quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat " 
      + "quo voluptas nulla pariatur?"; 

    /** 
    * Launch the application. 
    */ 
    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        SwingWindow window = new SwingWindow(); 
        window.frame.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    /** 
    * Create the application. 
    */ 
    public SwingWindow() { 
     initialize(); 
    } 

    /** 
    * Initialize the contents of the frame. 
    */ 
    private void initialize() { 
     frame = new JFrame(); 
     frame.setBounds(100, 100, 450, 300); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     final JXTaskPaneContainer panels = new JXTaskPaneContainer(); 
     JScrollPane scrollpanel = new JScrollPane(panels); 

     Container content = frame.getContentPane(); 
     content.add(new JTextArea(dolorem), BorderLayout.CENTER); 
     content.add(scrollpanel, BorderLayout.EAST); 
     content.add(new JXButton(new AbstractAction("Click me") { 

      public void actionPerformed(ActionEvent arg0) { 
       JXTaskPane panel = new JXTaskPane("test"); 
       JXLabel lbl = new JXLabel(dolorem); 
       lbl.setLineWrap(true); 
       panel.add(lbl); 
       panels.add(panel); 
      } 
     }), BorderLayout.NORTH); 

     panels.add(new JXTaskPane("Click the button, then resize")); 
    } 

} 
+1

체크 - taskPane (또는 컨테이너, 세부 사항을 잊었는) prefSize을 존중하는 관리자를 필요로한다. 도움이되지 않는 경우 SSCCE를 게시하여 문제를 시연하십시오 – kleopatra

+0

감사합니다. GridBagLayout을 시도해 보겠습니다. –

+0

반복 : 문제를 나타내는 SSCCE 표시 - 간결한 답변을 얻을 수 있다고 확신합니다 .-) – kleopatra

답변

0

내 질문에 대한 간결한 대답을 원합니다. 나는 그렇지 않다는 것을 두려워한다. BorderLayout 대신 GridBagLayout을 기반으로 새 레이아웃을 만들어서 문제를 해결했습니다.

최소 기본 크기 설정 문제도있었습니다. setMinimumSize(new Dimension(200, 127))을 호출하지 않고도 600의 기본 너비를 광고 했음에도 불구하고 컨테이너의 너비가 무한정 줄어들어 보였습니다.이 위치의 출처는 아직 확실하지 않습니다. 수치는, 컴퍼넌트의 초기 추천 사이즈로부터의 것입니다.

tutorial on oracle's site 여기에서 도움이되었습니다. 중요한 부분은 작동하는 무게를 찾는 것입니다. 0보다 큰 가중치 값은 구성 요소가 커지거나 축소되는 것을 허용합니다.

이 레이아웃 코드가 지금과 같은 모습입니다 : LayoutManager에

frmvanInput = new JFrame(); // window 
// ... captions, titles, etc 

frmvanInput.setContentPane(new JPanel(new GridBagLayout())); 

// ... more initializing 

/** 
* LAYOUTS 
*/ 
final Container contentPane = frmvanInput.getContentPane(); 

// the menu bar is on top, stretches all the way right 
GridBagConstraints menuBarLayout = new GridBagConstraints(); 
// top left 
menuBarLayout.anchor = GridBagConstraints.FIRST_LINE_START; // push to the top left 
menuBarLayout.gridx = 0; 
menuBarLayout.gridy = 0; 
// manage width 
menuBarLayout.gridwidth = 2; // stretch 
menuBarLayout.fill = GridBagConstraints.HORIZONTAL; // fill all x-space 
menuBarLayout.weightx = 0.5; // a weight > 0 allows for resizing 
//add 
contentPane.add(menuBar, menuBarLayout); 

// the text field is under the menu on the left and shares a row with the errors panel/scroll pane 
GridBagConstraints textfieldLayout = new GridBagConstraints(); 
// top left 
textfieldLayout.anchor = GridBagConstraints.FIRST_LINE_START; // push to the top left 
textfieldLayout.gridx = 0; // col left 
textfieldLayout.gridy = 1; // row center 
// manage stretching 
textfieldLayout.weightx = 0.5; // resizable 
textfieldLayout.weighty = 0.5; // resizable 
textfieldLayout.fill = GridBagConstraints.BOTH; 
// add 
contentPane.add(txtEditor, textfieldLayout); 

errorScrollPane = new JScrollPane(containerTaskPanel); 
// prevent indefinate shrinking 
errorScrollPane.setMinimumSize(new Dimension(200, 127)); 

GridBagConstraints scrollpanelLayout = new GridBagConstraints(); 
// middle, right 
scrollpanelLayout.anchor = GridBagConstraints.FIRST_LINE_END; 
scrollpanelLayout.gridx = 1; // right row 
scrollpanelLayout.gridy = 1; // center column 
// stretch to fill 
scrollpanelLayout.weightx = 0.5; // resizable 
scrollpanelLayout.weighty = 0.5; // resizable 
scrollpanelLayout.fill = GridBagConstraints.BOTH; 
// add our controls to the visible world 
contentPane.add(errorScrollPane, scrollpanelLayout); 

// the emitter panel has the bottom row to itself 
GridBagConstraints emitterpanelLayout = new GridBagConstraints(); 
// bottom left 
emitterpanelLayout.anchor = GridBagConstraints.LAST_LINE_START; 
emitterpanelLayout.gridx = 0; // col left 
emitterpanelLayout.gridy = 2; // row bottom 
// stretch width 
emitterpanelLayout.weightx = 0.5; 
emitterpanelLayout.gridwidth = 2; 
// manage height: 
emitterpanelLayout.weighty = 0.1; 
emitterpanelLayout.fill = GridBagConstraints.BOTH; 
// add 
contentPane.add(emitterScrollPane, emitterpanelLayout); 
+0

[setXXSize를 사용하지 마십시오.] (http://stackoverflow.com/a/7229519/203657) – kleopatra

관련 문제