10

複数行のテキスト要素 (JLabel/JTextArea など) を含むダイアログを作成し、単語をラップしたいと考えています。ダイアログの幅を固定したいのですが、テキストの大きさに応じて高さを調整します。私はこのコードを持っています:

import static javax.swing.GroupLayout.DEFAULT_SIZE;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class TextSizeProblem extends JFrame {
  public TextSizeProblem() {

    String dummyString = "";
    for (int i = 0; i < 100; i++) {
      dummyString += " word" + i;  //Create a long text
    }
    JLabel text = new JLabel();
    text.setText("<html>" + dummyString + "</html>");

    JButton packMeButton = new JButton("pack");
    packMeButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        pack();
      }
    });

    GroupLayout layout = new GroupLayout(this.getContentPane());
    getContentPane().setLayout(layout);
    layout.setVerticalGroup(layout.createParallelGroup()
        .addComponent(packMeButton)
        .addComponent(text)
    );
    layout.setHorizontalGroup(layout.createSequentialGroup()
        .addComponent(packMeButton)
        .addComponent(text, DEFAULT_SIZE, 400, 400) //Lock the width to 400
    );

    pack();
  }

  public static void main(String args[]) {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        JFrame frame = new TextSizeProblem();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
      }
    });
  }
}

プログラムを実行すると、次のようになります: (ソース: lesc.se )代替テキスト

しかし、ダイアログを次のようにしたいと思います(パックボタンを押したときのように):( ソース:lesc.se代替テキスト

問題は、レイアウト マネージャーがテキストを画面に表示する前に適切な高さを判断できなかったことだと思います。さまざまな validate()、invalidate()、validateTree() などを試しましたが、成功しませんでした。

4

3 に答える 3

5

私は自分の問題の解決策を見つけました。JLabelをJTextAreaに置き換えると、次のようになります。

JTextArea text = new JTextArea();
text.setText(dummyString);
text.setLineWrap(true);
text.setWrapStyleWord(true);

そして、pack()を呼び出した後、レイアウトマネージャーを呼び出してコンポーネントを再度レイアウトし、その後に別のパックを呼び出します。

pack();
layout.invalidateLayout(this.getContentPane());
pack();

これにより、レイアウトマネージャーは幅に適応します。

完全なコード:

import static javax.swing.GroupLayout.DEFAULT_SIZE;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class TextSizeProblem3 extends JFrame {
  public TextSizeProblem3() {

    String dummyString = "";
    for (int i = 0; i < 100; i++) {
      dummyString += " word" + i;  //Create a long text
    }
    JTextArea text = new JTextArea();
    text.setText(dummyString);
    text.setLineWrap(true);
    text.setWrapStyleWord(true);

    JButton packMeButton = new JButton("pack");
    packMeButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        pack();
      }
    });

    GroupLayout layout = new GroupLayout(this.getContentPane());
    getContentPane().setLayout(layout);
    layout.setVerticalGroup(layout.createParallelGroup()
        .addComponent(packMeButton)
        .addComponent(text)
    );
    layout.setHorizontalGroup(layout.createSequentialGroup()
        .addComponent(packMeButton)
        .addComponent(text, DEFAULT_SIZE, 400, 400) //Lock the width to 400
    );

    pack();
    layout.invalidateLayout(this.getContentPane());
    pack();
  }

  public static void main(String args[]) {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        JFrame frame = new TextSizeProblem3();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
      }
    });
  }
}

(JLabelと同じように見えるように、カスタマイズ(境界線、色など)を追加できますが、省略しました)

于 2009-06-26T12:13:59.077 に答える
5

これがあなたが望むものだと思います:

JLabel label = new JLabel("<html><div style=\"width:200px;\">Lots of text here...</div></html>");
// add the label to some Container.

これにより、JLabel の幅が 200 ピクセルに制限され、テキストに合わせて高さが自動的に調整されます。

于 2011-08-10T15:21:46.653 に答える