增加 JPanel 的高度及其内容

发布于 2024-12-13 12:29:30 字数 1682 浏览 1 评论 0原文

我有一个示例面板,其中包含其他组件,在我的例子中是 JTextArea。我想随着 JTextArea 高度的增加而增加面板的高度,并且宽度固定。我已经为面板设置了固定宽度。

public class Panel extends JPanel {

    public Panel() {
        setPreferredSize(new Dimension(300, 85));
        setLayout(new BorderLayout());
        JPanel pic = new JPanel(new FlowLayout(FlowLayout.LEFT));
        pic.setBackground(Color.GRAY);
        pic.add(new JLabel(new ImageIcon("img/icon.png")));




        JPanel status = new JPanel(new FlowLayout(FlowLayout.LEFT));
        status.setBackground(Color.GRAY);

        JTextArea textArea = new JTextArea();

        String text = "The quick brown fox jumps over the lazy dog. "
                + "The quick brown fox jumps over the lazy dog. "
                + "The quick brown fox jumps over the lazy dog. "
                + "The quick brown fox jumps over the lazy dog. "
                + "The quick brown fox jumps over the lazy dog. "
                + "The quick brown fox jumps over the lazy dog. "
                + "The quick brown fox jumps over the lazy dog.";
        textArea.setText(text);
        textArea.enable(false);

        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        add(textArea);
        status.add(textArea);

        add(pic, BorderLayout.WEST);
        add(status, BorderLayout.CENTER);

        setBorder(BorderFactory.createEtchedBorder());
    }

    public static void main(String[] args) {
        JFrame f = new JFrame("Panel Test");

        f.add(new Panel());

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.pack();
        f.setVisible(true);
    }
}

我不想要 JTextArea 的滚动条。我希望它的高度与完整查看其内容所需的高度相同。

I have a sample panel which have other component, in my case JTextArea. I want to increase the height of the panel as the height of JTextArea increases, with fixed width. I have set fixed width for panel.

public class Panel extends JPanel {

    public Panel() {
        setPreferredSize(new Dimension(300, 85));
        setLayout(new BorderLayout());
        JPanel pic = new JPanel(new FlowLayout(FlowLayout.LEFT));
        pic.setBackground(Color.GRAY);
        pic.add(new JLabel(new ImageIcon("img/icon.png")));




        JPanel status = new JPanel(new FlowLayout(FlowLayout.LEFT));
        status.setBackground(Color.GRAY);

        JTextArea textArea = new JTextArea();

        String text = "The quick brown fox jumps over the lazy dog. "
                + "The quick brown fox jumps over the lazy dog. "
                + "The quick brown fox jumps over the lazy dog. "
                + "The quick brown fox jumps over the lazy dog. "
                + "The quick brown fox jumps over the lazy dog. "
                + "The quick brown fox jumps over the lazy dog. "
                + "The quick brown fox jumps over the lazy dog.";
        textArea.setText(text);
        textArea.enable(false);

        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        add(textArea);
        status.add(textArea);

        add(pic, BorderLayout.WEST);
        add(status, BorderLayout.CENTER);

        setBorder(BorderFactory.createEtchedBorder());
    }

    public static void main(String[] args) {
        JFrame f = new JFrame("Panel Test");

        f.add(new Panel());

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.pack();
        f.setVisible(true);
    }
}

And I don't want scrollbar for JTextArea. I want it to take as much height as it takes for complete viewing of its content.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

⊕婉儿 2024-12-20 12:29:30

除非文本区域的大小已知,否则无法确定文本区域的首选大小。也就是说,在知道文本区域的宽度之前,无法完成文本的换行。所以你需要给文本区域一个宽度。

另外,您不能为面板指定首选大小,因为这将违背使用 pack() 的目的;

// setPreferredSize(new Dimension(300, 85));
...
textArea.setText(text);
textArea.setSize(300, 1);

The preferred size of the text area can't be determined unless the size of text area is known. That is the wrapping of the text can't be done until the width of the text area is known. So you need to give the text area a width.

Also, you can't give the panel a preferred size since this will defeat the purpose of using pack();

// setPreferredSize(new Dimension(300, 85));
...
textArea.setText(text);
textArea.setSize(300, 1);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文