我希望使用JOptionPane.showOptionDialog
和JTextArea
制作一个JLabel
。
问题是对话框太小而我找不到任何解决方案,所以我决定将我的内容放在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
构建滚动条但我找不到其他解决方案。
对我的帖子或英语的任何反馈都很受欢迎。
答案 0 :(得分:5)
您正在将JScrollPane
添加到JPanel
和JLabel
的{{1}}父级。这样,JTextArea
会认为它的宽度大于渲染文本的宽度。
我建议您将滚动条添加到JTextArea
中(使滚动窗格成为父级)。然后textarea将知道实际宽度(不包括滚动条)。
JTextArea
答案 1 :(得分:4)
发表评论回答。请尝试向您要添加JPanel
的{{1}}提供一个Empty Border,或者向JScrollPane
添加一个EmptyBorder。