【发布时间】:2012-06-13 13:36:55
【问题描述】:
我想用一些JTextArea 和JLabel 制作一个JOptionPane.showOptionDialog。
问题是对话框太小,我没有找到任何解决方案,所以我决定将我的内容放在JScrollPane。
我看到我必须将我所有的JTextArea 和我的JLabel 放在JPanel 中,因为将它们连续添加到JScrollPane 中不允许我正确放置视口。
最后一个问题是我的 JTextArea 正确地换行了单词,但是当我有 2 或 3 个字母长度的单词时,它们会被滚动条隐藏。
SSCCE:
public class myTest extends JFrame
{
public static void main(String[] args)
{
new myTest();
}
public myTest()
{
String myLongString="Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?";
String aLittleString="I am a poor little string which is placed at the bottom of a JOptionPane.";
String[] options = {"OK","NO"};
JLabel titre1 = new JLabel("Title");
JLabel titre2 = new JLabel("Title 2");
Map<TextAttribute,Integer> attributs = new HashMap<TextAttribute, Integer>();
attributs.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
Font police = new Font("Serif", Font.BOLD, 12).deriveFont(attributs);
titre1.setFont(police);
titre2.setFont(police);
JTextArea text3 = new JTextArea(myLongString,5,75);
text3.setLineWrap(true);
text3.setWrapStyleWord(true);
text3.setEditable(false);
Color back = this.getBackground();
text3.setBackground(back);
JTextArea text = new JTextArea(myLongString,5,75);
text.setLineWrap(true);
text.setWrapStyleWord(true);
text.setEditable(false);
text.setBackground(back);
JTextArea text2 = new JTextArea(aLittleString,5,75);
text2.setLineWrap(true);
text2.setWrapStyleWord(true);
text2.setEditable(false);
text2.setBackground(back);
JPanel bas = new JPanel(new BorderLayout());
JPanel basbas = new JPanel(new BorderLayout());
bas.add(titre1,BorderLayout.NORTH);
bas.add(text,BorderLayout.CENTER);
basbas.add(titre2,BorderLayout.NORTH);
basbas.add(text2,BorderLayout.CENTER);
basbas.add(text3,BorderLayout.SOUTH);
bas.add(basbas,BorderLayout.SOUTH);
JScrollPane js = new JScrollPane(bas);
js.setBorder(null);
js.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
js.setViewportView(bas);
JLabel lMessage = new JLabel("A message.");
Object[] params = {js,lMessage};
int n = JOptionPane.showOptionDialog(new JFrame(),
params,
"my dialog",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options,
options[0]);
}
}
我阅读了几个主题,但他们总是在谈论 setWrapStyleWord。 我禁用水平滚动条是因为我不想要它,事实上我不想要一个滚动条用于 2 个字母的越位。
在我看来,问题在于我使用 JPanel 构建滚动条,但我没有找到其他解决方案。
欢迎对我的帖子或我的英语提供任何反馈。
【问题讨论】:
-
Writing a perfect question. 请务必提供一个SSCCE,因为您提供的sn-p 在任何意义上都不能编译:(。只是猜测,尝试注释掉这一行
js.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);,因为你的LineWrap属性是真的,你不需要那个。 -
我将编辑 SSCCE,看来我错过了我的 c/p。我已经尝试不使用scrollbar_horizontal,但是就像我说的很糟糕,当我让JScrollPane 默认情况下,一个水平滚动条只出现2 个字符的横向滚动条。
-
现在我明白了,尝试为您的
JPanel提供一个EmptyBorder,您可以在其中添加JScrollPane,这将满足您的需求:-) -
提供
bas.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 20));解决问题。谢谢 但是我仍然对这个显示问题感到困惑,滚动条不应该与文本冲突。顺便说一句,你能告诉我我的 SSCCE 现在好吗?虽然我的问题已经解决了。 -
@nIcEcOw “所以对我来说,至少导入语句也是一个 SSCCE 的一部分” 这就是我在编写 SSCCE 文档时的意思。 :)
标签: java swing jscrollpane jtextarea joptionpane