3

所以我想以某种方式设置我的 JOptionPane 的大小 - 如果不设置它,它对于我的目的来说太大了,而且看起来不太好。这是我的代码:

   JScrollPane scrollpane = new JScrollPane(); 
   String categories[] = { "1. Problem One Problem One Problem One Problem One Problem One Problem One Problem One Problem One Problem One", "2. Problem Two", "3. Extended Family", "4. Extended Family", "5. Extended Family"};
   JList list = new JList(categories);
   errorListCellRenderer cellRenderer = new errorListCellRenderer();
   list.setCellRenderer(cellRenderer);
   scrollpane = new JScrollPane(list);
   JPanel panel = new JPanel(); 
   panel.add(scrollpane);
   JOptionPane.showMessageDialog(null, panel, "Error List",  
                                          JOptionPane.PLAIN_MESSAGE);

当我这样设置JPanel的大小时:

panel.setPreferredSize(new Dimension(500, 200));

滚动条消失,因此并未显示所有内容。

有任何想法吗?爪哇n00b...

4

3 回答 3

15

如果您只想更改 JOptionPane 窗口的大小,可以使用 UIManager 来实现。像这样:

UIManager.put("OptionPane.minimumSize",new Dimension(500,500)); 
JOptionPane.showMessageDialog(null, "Hi " );
于 2014-10-09T21:29:15.307 回答
6

给滚动窗格一个preferredSize,然后直接将它添加到optionPane

String categories[] = { "1. Problem One Problem One Problem One Problem One Problem One Problem One Problem One Problem One Problem One",
                       "2. Problem Two", "3. Extended Family", "4. Extended Family", "5. Extended Family"};
JList list = new JList(categories);
JScrollPane scrollpane = new JScrollPane(list);
scrollpane.setPreferredSize(new Dimension(300,125));//<-----any size you want
JOptionPane.showMessageDialog(null, scrollpane, "Error List",JOptionPane.PLAIN_MESSAGE);
于 2013-01-13T05:10:03.953 回答
5

请进行以下更改并查看;

  1. 不要将 JScrollPane 放在另一个面板中。直接在 showMessageDialog() 中传递 JScrollPane。

  2. 也使用 scrollPane.getViewport().add(list);

编辑:我刚刚尝试了以下操作,随着项目的增加,滚动条对我来说是可见的。

 JScrollPane scrollpane = new JScrollPane(); 
       String categories[] = { "1. Problem One Problem One Problem One Problem One Problem One Problem One Problem One Problem One Problem One", "2. Problem Two", "3. Extended Family", "4. Extended Family", "5. Extended Family"
               ,"6. Extended Family","7. Extended Family","8. Extended Family","9. Extended Family"};
       JList list = new JList(categories);

       scrollpane = new JScrollPane(list);

       JPanel panel = new JPanel(); 
       panel.add(scrollpane);

       scrollpane.getViewport().add(list);
       JOptionPane.showMessageDialog(null, scrollpane, "Error List",  
                                              JOptionPane.PLAIN_MESSAGE);

附上截图;

在此处输入图像描述

于 2013-01-13T01:34:05.477 回答