Java:JFrame 对话框中的文本未换行

发布于 2024-11-10 15:18:31 字数 1436 浏览 1 评论 0原文

我正在使用 JFrame 来呈现一个带有一些文本和 2 个按钮的消息框。如何让文本根据框的大小自动换行?

这是我当前的代码:

        dialogFrame = new JFrame();

        JButton newButton = new JButton("New");
        newButton.addActionListener(new newUploadAction());

        JButton resumeButton = new JButton("Resume");
        resumeButton.addActionListener(new resumeUploadAction());

        //dialogFrame.setUndecorated(true);

        JPanel addPanel = new JPanel();

        JPanel addPanel2 = new JPanel();

        addPanel.add(newButton);
        addPanel.add(resumeButton);

        String text = "<html><p>A previous control file exists for this file. ";
        text += "Would you like to initiate a new transfer or resume the previous one?</p></html>";

        JLabel testLabel = new JLabel(text);

        //testLabel.setPreferredSize(new Dimension(1, 1)); 

        addPanel2.add(testLabel);


        Container content = dialogFrame.getContentPane();


        //content.setBackground(Color.white);
        content.setLayout(new BoxLayout(content, BoxLayout.PAGE_AXIS)); 

        content.add(addPanel2);
        content.add(addPanel);


        dialogFrame.setSize(200,200);
        dialogFrame.setVisible(true);
        dialogFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

我在某处读到调用

  testLabel.setPreferredSize(new Dimension(1, 1)); 

会导致我想要的换行行为,但这只会导致文本根本不显示。

I'm using a JFrame to present a message box with some text and 2 buttons. How do I get the text to wrap automatically based on the size of the box?

Here's my current code:

        dialogFrame = new JFrame();

        JButton newButton = new JButton("New");
        newButton.addActionListener(new newUploadAction());

        JButton resumeButton = new JButton("Resume");
        resumeButton.addActionListener(new resumeUploadAction());

        //dialogFrame.setUndecorated(true);

        JPanel addPanel = new JPanel();

        JPanel addPanel2 = new JPanel();

        addPanel.add(newButton);
        addPanel.add(resumeButton);

        String text = "<html><p>A previous control file exists for this file. ";
        text += "Would you like to initiate a new transfer or resume the previous one?</p></html>";

        JLabel testLabel = new JLabel(text);

        //testLabel.setPreferredSize(new Dimension(1, 1)); 

        addPanel2.add(testLabel);


        Container content = dialogFrame.getContentPane();


        //content.setBackground(Color.white);
        content.setLayout(new BoxLayout(content, BoxLayout.PAGE_AXIS)); 

        content.add(addPanel2);
        content.add(addPanel);


        dialogFrame.setSize(200,200);
        dialogFrame.setVisible(true);
        dialogFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

I read somewhere that calling

  testLabel.setPreferredSize(new Dimension(1, 1)); 

would cause the wrapping behavior I want, but that just resulted in the text not showing up at all.

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

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

发布评论

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

评论(1

流殇 2024-11-17 15:18:31

您可以将文本放置在 JTextArea 中,并在 JTextArea 上调用 setLineWrap(true)setWrapStyleWord(true) 。如果您希望它看起来更像 JLabel 风格,只需根据您的喜好更改 JTextArea 的颜色设置即可。

编辑1

还有另一件事可以工作:考虑让持有的JPanel使用BorderLayout

JPanel addPanel2 = new JPanel(new BorderLayout()); //!! added BorderLayout

这样JLabel添加将填充addPanel2

You could place the text in a JTextArea and call setLineWrap(true) and setWrapStyleWord(true) on the JTextArea. If you want it to look more JLabel-ish, then just change the color settings of the JTextArea to your liking.

EDIT 1

Also another thing that could work: consider having the holding JPanel use a BorderLayout:

JPanel addPanel2 = new JPanel(new BorderLayout()); //!! added BorderLayout

So that the JLabel added will fill the addPanel2.

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