Swing:按钮上方是否自动调整文本大小? [英] Swing: An automatic resizing Text above a button?

查看:116
本文介绍了Swing:按钮上方是否自动调整文本大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种执行以下操作的方法:

I am looking for a way to do the following:

具有固定宽度的JDialog.

Have a JDialog with a fixed width.

其中是一个JTextArea(或您建议的任何一个更好的组件...),它接收长度可变的文本(介于0到30行之间).

In it is a JTextArea (or whatever you suggest is a better component...) which receives a text of varying length (somewhere between 0 and 30 line)

在该文本下方是一个按钮.

Below that text is a button.

对话框的高度会自动调整大小,以确保显示所有文本"和按钮.

The dialog is automatically sized in height to make sure all the Text AND the button is being displayed.

我最接近解决方案的是这个,但是JTextArea似乎不知道自动换行后有多大!

The closest I have come to a solution is this, but the JTextArea does not seem to know how large it is after it did the automatic line breaks!

    public PleaseResize(){
    super();

    Container cp = this.getContentPane();
    cp.setLayout(new BoxLayout(cp, BoxLayout.Y_AXIS));

    JTextArea area = new JTextArea();

    area.setColumns(20);
    area.setLineWrap(true);
    area.setEditable(false);
    area.setWrapStyleWord(true);
    area.setText("Once upon a midnight dreary, while I pondered, weak and weary, over many a quaint an curious volume of forgotten lore.");


    cp.add(area);

    cp.add(new JButton("Hallo"));


    this.pack();

}

不幸的是,不是滚动文本.

在此之前,我曾问过这是稍微不同的方法:调整大小自动LineWrap 之后正确对话,但是也许JTextArea毕竟是错误的组件?我使用了错误的LayoutManager吗?但是,所有这些似乎都无法确定对话框的大小.为什么在向文本外部添加换行符后,JTextArea无法传达其高度?

I have asked this is a slightly different way before here: Resize Dialog properly after automatic LineWrap, but perhaps the JTextArea is the wrong component after all? Am I using the wrong LayoutManager? All of them seem unable to determine how large the dialog should be, though. Why does the JTextArea fail to communicate it's height after adding line-breaks to the text to the outside?

这是工作代码:

    public PleaseResize() throws BadLocationException {
    super();

    Container cp = this.getContentPane();
    cp.setLayout(new BorderLayout());

    JTextArea area = new JTextArea();
    area.setColumns(20);
    area.setLineWrap(true);
    area.setEditable(false);
    area.setWrapStyleWord(true);
    area.setText("Once upon a midnight dreary, while I pondered, weak and weary, over many a quaint an curious volume of forgotten lore.");
    System.out.println(area.getLineCount());

    JScrollPane pane = new JScrollPane(area);
    cp.add(pane, BorderLayout.CENTER);
    cp.add(new JButton("Hallo"), BorderLayout.SOUTH);

    this.pack();
    this.pack();


}

对我来说,两次打包似乎还是很奇怪,但这确实解决了调整大小的问题:).

Packing twice still seems a bit weird to me, but it does solve the resizing problems :).

推荐答案

尝试此方法以根据其模型调整JTextArea的大小.

Try this aproach to resize JTextArea based in its model.

        Rectangle modelToView = area.modelToView(area.getDocument().getLength());
        if (modelToView != null) {
            area.setSize(area.getWidth(), Math.max(area.getHeight(), modelToView.height + modelToView.y));
            area.doLayout();
        }

这篇关于Swing:按钮上方是否自动调整文本大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆