设置JPanel或JOptionPane的大小 [英] Setting Size of JPanel or JOptionPane

查看:266
本文介绍了设置JPanel或JOptionPane的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想以某种方式设置我的JOptionPane的大小-如果不设置它,它就我的目的而言太大了,看起来不会很好.这是我的代码:

So i want to somehow set the size of my JOptionPane - without setting it, it is just too large for my purposes and won't look nice. here is my code:

   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的大小时:

when i set the size of the JPanel as such:

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

滚动条消失,因此不会显示所有内容.

the scroll bar disappears and hence not everything is displayed.

有什么想法吗? Java n00b ...

any ideas? Java n00b...

推荐答案

请进行以下更改并查看;

Please do the following changes and see;

  1. 请勿将JScrollPane放在另一个面板中.直接在showMessageDialog()中传递JScrollPane.

  1. Do not put the JScrollPane within another Panel. Pass the JScrollPane directly within the showMessageDialog().

还可以使用scrollPane.getViewport().add(list);

Also use scrollPane.getViewport().add(list);

我只是尝试了以下操作,随着项目的增加,滚动条对我可见.

I just tried the following, and the scroll bar is visible for me as the items increased.

 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);

附有屏幕截图;

这篇关于设置JPanel或JOptionPane的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆