2016-04-07 12 views
1

問題を示すためにSSCCEを作成しました。私はきれいにラップする複数行のJLabelを持っていますが、JFrameにパックすると、行の折り返しに必要な余分な高さが考慮されないので、ウィンドウをスクロールまたはサイズ変更する必要があります。私はそうのように、正しいサイズで初期化されるようにウィンドウを好むだろう複数行JLabelが正しくパックされない

enter image description here

:ここでは作成時にウィンドウです

enter image description here

は私が塗りのすべての組み合わせを試してみましたが、成長、スパン、最小、優先、最大など、しかし私は私が望む振る舞いを得ることができません。私もJTextAreasを試しましたが、JLabelsよりもさらに大きな問題を抱えています。あなたは、必ずしもあなたのJPanel内のすべての内容を変更する必要はありません

package SSCCE; 

import net.miginfocom.swing.MigLayout; 
import java.awt.*; 
import javax.swing.*; 
import static javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS; 
import static javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS; 

public class App 
{ 
    public static void main(String[] args) { 
     App program = new App(); 
     SwingUtilities.invokeLater(program::run); 
    } 

    private void run() { 
     JFrame w = new JFrame(); 
     w.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 



     // "main" shall contain everything to be packed into the JFrame. 
     JPanel main = new JPanel(); 
     main.setLayout(new MigLayout(
       "debug", 
       "[][][][][]", // cols 
       "[][][fill, grow]" // rows 
     )); 

     // First row (search UI) 
     main.add(new JLabel("Chinese character:"), "spany 2"); 
     main.add(new JTextField("type here"), "growx, spanx 3"); 
     main.add(new JButton("Search"), "spany 2, wrap"); 

     // Second row (checkboxes) 
     main.add(new JCheckBox("box1")); 
     main.add(new JCheckBox("box2")); 
     main.add(new JCheckBox("box3"), "wrap"); 



     // Third row setup: a 'content' JPanel to be viewed by a JScrollPane, the latter which is to be added to the main JPanel. 
     JPanel content = new JPanel(); 
     content.setLayout(new MigLayout(
       "debug, wrap 4", 
       "[left][][][fill, grow]", // cols 
       "[top][fill]" // rows 
     )); 

     // First row of 'content' JPanel: The multi-line JLabel which wraps beautifully but doesn't pack properly. 
     JLabel tutorial = new JLabel("<html>" + 
       "**WELCOME**<br>This is an English-language interface for the Chinese character etymology search engine  'XiaoXue', " + 
       "produced by the Taiwanese academic institution Academia Sinica (中央研究院).<br> NOTE: if the text  between " + 
       "those brackets doesn't display, Chinese language support is not configured on your computer." + 
       "<br><br>" + 
       "**HOW TO USE**<br>Simply enter a single Chinese character into the search field and press 'enter' or  the " + 
       "Search button. The checkboxes below the search field are non-operational and merely proof-of-concept  for a future capability to " + 
       "combine resources from different databases.<br>NOTE: supports traditional Chinese characters (and those  shared with Japanese), " + 
       "but not simplified." 
       + "</html>"); 
     tutorial.setPreferredSize(new Dimension(1,1)); // This encourages the JLabel to wrap. 
     content.add(tutorial, "grow, spanx 4"); 

     // Second row of 'content' JPanel: 
     content.add(new JLabel("label1"), ""); 
     content.add(new JLabel("label2"), ""); 
     content.add(new JLabel("label3"), ""); 
     content.add(new JLabel("label4"), ""); 

     // JScrollPane view of the 'content' JPanel 
     JScrollPane contentScrollPane = new JScrollPane(content, VERTICAL_SCROLLBAR_ALWAYS, HORIZONTAL_SCROLLBAR_ALWAYS); 
     contentScrollPane.getVerticalScrollBar().setUnitIncrement(20); 
     contentScrollPane.getHorizontalScrollBar().setUnitIncrement(50); 

     // Third row finally added to main JPanel. 
     main.add(contentScrollPane, "span, width :100%:"); 



     w.add(main); 
     w.pack(); 
     w.setVisible(true); 
    } 
} 

答えて

-1

は、ここに私の単一ファイルSSCCEです。最初からJFrameのサイズを変更することができます。 w.setSize(200, 400);などを入れてJFrameのサイズを変更してみてください。

もう1つの解決方法はw.pack()ですが、それがうまくいくかどうかはわかりません。

+0

私はむしろダイナミックなソリューションを探しています。私のシステムにウィンドウを正しくパックするために必要なサイズはプラットフォームによって異なりますが、テキストを変更するときはいつでも再計算したくありません。 –

+0

'setSize()'メソッドを使用すると、自動的にはサイズ変更されません( 'pack()'メソッドを使用していないことが認められています) 'setResizable(false)'メソッドを使用してあなたはまた、コンピュータから画面の解像度を取得し、それを使って調べることができます。 – Eames

+1

これに気をつけてください[pitfall](http://stackoverflow.com/a/12532237/230513)。 – trashgod

関連する問題