如何格式化JLabel / JTextField以便文本换行 [英] How to format JLabel/JTextField so text wraps

查看:202
本文介绍了如何格式化JLabel / JTextField以便文本换行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个字符串和JLabel,JLabel显示在JFrame上:

Lets say I have a string and JLabel, the JLabel displayed on a JFrame:

public String content="Hello fellow stack-overflowers, I want to format this output";
public JLabel jL = new JLabel();

执行以下操作后:

jL.setText(content);

我得到这个输出:

Hello fellow stack-overflowers, I want to for..

但是我真正想要的是文本不要超过标签或文本字段的长度,而是每隔4个字左右创建一个换行符,例如:

But what I really want is the text not to keep going right past the length of the label or textfield, but to make a newline every like 4 words or so, something like:

Hello fellow stack-overflowers, I want
to format this output

询问是否需要更多信息。

Ask if more info is needed.

推荐答案

一种可能的解决方案是将标签文本包装在 HTML 标签中并允许底层布局管理器调整大小,例如......

A possible solution is to wrap the label text in HTML tags and allow the underlying layout manager the ability to resize it, for example...

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test100 {

    public static void main(String[] args) {
        new Test100();
    }

    public Test100() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JLabel label;
        private JTextField textField;

        public TestPane() {
            label = new JLabel("<html>Hello fellow stack-overflowers, I want to format this output</html>");
            textField = new JTextField(10);

            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.anchor = GridBagConstraints.NORTHWEST;

            add(label);

            gbc.gridx = 1;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.weightx = 1;
            add(textField, gbc);
        }

    }

}

这个例子没有办法压迫标签分裂,你可能需要稍微调整一下这些值......

This example goes out of it's way to pressure the label to split, you might need to play around with the values a little...

这篇关于如何格式化JLabel / JTextField以便文本换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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