[英]How to resize jscrollpane in jframe java swing?
i am new to java swing, currently i am developing an application using java swing, i am facing the problem is that when i populate my JList with JScrollPane it is displaying on my whole jframe. 我是java swing的新手,目前我正在使用java swing开发应用程序,我面临的问题是,当我用JScrollPane填充JList时,它将显示在整个jframe上。 code:
码:
List<Element> parentElements = xmlUtil.getParentElements();
String title = "License";
JFrame f = new JFrame(title);
String[] parentTags = new String[parentElements.size()];
for(int i=0;i<parentElements.size();i++)
{
System.out.println(parentElements.get(i).getName());
parentTags[i] = parentElements.get(i).getName();
}
JList list = new JList(parentTags);
JScrollPane scrollPane = new JScrollPane(list, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setPreferredSize(new Dimension(50,40));
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = f.getContentPane();
contentPane.add(scrollPane);
f.setSize(900, 700);
f.setVisible(true);
i want output like this so it can cover small area in jframe 我想要这样的输出,以便它可以覆盖jframe中的小区域
Please tell me how to resize it ? 请告诉我如何调整大小? as i am stuck into this problem from very long time.
因为我从很长时间以来一直陷入这个问题。
Add the JScrollPane
to a JPanel
and then add the JPanel
to the JFrame
: 将
JScrollPane
添加到JPanel
,然后将JPanel
添加到JFrame
:
JScrollPane scrollPane = new JScrollPane(list, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = f.getContentPane();
JPanel panel = new JPanel();
panel.add(scrollPane);
panel.setPreferredSize(new Dimension(50,40));
contentPane.add(panel);
Then you can use the setPreferredSize
method of the JPanel
to resize it how you want. 然后,您可以使用
JPanel
的setPreferredSize
方法来调整其大小。
Also consider using a good layout manager. 还可以考虑使用一个好的布局管理器。 I use a combination of two (primarily):
我使用两种(主要)的组合:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.