java JList 大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5934686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java JList size
提问by skowron-line
Hi i want to set JList height as height as screen is
嗨,我想将 JList 高度设置为屏幕高度
public Locations() {
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
screenWidth = screen.width;
screenHeight = screen.height;
initComponents();
}
private void initComponents() {
setLayout(new GroupLayout());
add(getJScrollPane0(), new Constraints(new Leading(8, 100, 10, 10), new Leading(5, 135, 10, 10)));
setSize(1366, 768);
}
private JScrollPane getJScrollPane0() {
if (jScrollPane0 == null) {
jScrollPane0 = new JScrollPane();
jScrollPane0.setSize(screenWidth, screenHeight);
jScrollPane0.setViewportView(getJList0());
}
return jScrollPane0;
}
private JList getJList0() {
if (jList0 == null) {
jList0 = new JList();
jList0.setSize(400, screenHeight);
DefaultListModel listModel = new DefaultListModel();
listModel.addElement("item0");
listModel.addElement("item1item1item1item1item1");
listModel.addElement("item2");
listModel.addElement("item3");
jList0.setModel(listModel);
}
return jList0;
}
private static void installLnF() {
try {
String lnfClassname = PREFERRED_LOOK_AND_FEEL;
if (lnfClassname == null)
lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName();
UIManager.setLookAndFeel(lnfClassname);
} catch (Exception e) {
System.err.println("Cannot install " + PREFERRED_LOOK_AND_FEEL
+ " on this platform:" + e.getMessage());
}
}
/**
* Main entry of the class.
* Note: This class is only created so that you can easily preview the result at runtime.
* It is not expected to be managed by the designer.
* You can modify it as you like.
*/
public static void main(String[] args) {
installLnF();
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Locations");
Locations content = new Locations();
content.setPreferredSize(content.getSize());
frame.add(content, BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
But list size is like i draw it in editor, how to change it ??
但是列表大小就像我在编辑器中绘制的一样,如何更改它?
Edit:
编辑:
I have solved the problem by adding
我已经通过添加解决了这个问题
add(getJScrollPane0(), new Constraints(new Leading(8, 100, 10, 10), new Leading(5, screenHeight - 115, 10, 10)));
回答by mKorbel
回答by Ajinkya Jagtap
Each List is associated with JScrollPane. So First set size of JScrollPane associated with list then set size of list with list.setsize(width,height) method
每个 List 都与 JScrollPane 相关联。所以首先设置与列表关联的 JScrollPane 的大小,然后使用 list.setsize(width,height) 方法设置列表的大小
In my program:
在我的程序中:
jScrollPane1.setSize(100,500);
list.setSize(100,500);