Java:JFrame 对话框中的文本未换行
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将文本放置在
JTextArea
中,并在JTextArea
上调用setLineWrap(true)
和setWrapStyleWord(true)
。如果您希望它看起来更像 JLabel 风格,只需根据您的喜好更改JTextArea
的颜色设置即可。编辑1
还有另一件事可以工作:考虑让持有的
JPanel
使用BorderLayout
:这样
JLabel
添加将填充addPanel2
。You could place the text in a
JTextArea
and callsetLineWrap(true)
andsetWrapStyleWord(true)
on theJTextArea
. If you want it to look more JLabel-ish, then just change the color settings of theJTextArea
to your liking.EDIT 1
Also another thing that could work: consider having the holding
JPanel
use aBorderLayout
:So that the
JLabel
added will fill theaddPanel2
.