Java Swing JList
我在 Java Swing 中使用 JList,但是当我的对话框打开时,列表没有显示。
private JList getJList() {
if (mylist == null) {
mylist = new JList();
mylist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
mylist.setSize(new Dimension(154, 106));
model.addElement("test");
model.addElement("zwei");
mylist.setVisible(true);
}
return mylist;
}
该列表已定义:
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(getJList(), BorderLayout.CENTER);
}
return jContentPane;
}
它是一个 JContentPane (/Panel),
public fensterdrei(Frame owner) {
super(owner);
initialize();
}
调用 getJContentPane()
的代码:
private void initialize() {
this.setSize(300, 200);
this.setContentPane(getJContentPane());
this.setTitle("Auswahl");
}
I am using a JList in Java Swing, but when my Dialog opens, the List isn't shown.
private JList getJList() {
if (mylist == null) {
mylist = new JList();
mylist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
mylist.setSize(new Dimension(154, 106));
model.addElement("test");
model.addElement("zwei");
mylist.setVisible(true);
}
return mylist;
}
The list is defined:
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(getJList(), BorderLayout.CENTER);
}
return jContentPane;
}
It's a JContentPane (/Panel)
public fensterdrei(Frame owner) {
super(owner);
initialize();
}
the code calling getJContentPane()
:
private void initialize() {
this.setSize(300, 200);
this.setContentPane(getJContentPane());
this.setTitle("Auswahl");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我找不到你在哪里设置 JList 的模型?
请查看Java 编程语言的代码约定
FensterDrei
而不是fensterdrei
myList
而不是mylist
I can't find where you are setting the model of the JList?
Something like
Please have a look at the Code Conventions for the Java Programming Language
FensterDrei
instead offensterdrei
myList
instead ofmylist
它是 getContentPane 而不是 getJContentPane,并且您不应该重载它。
相反,在您的构造函数(或立即调用的其他函数)中,您可以
It's getContentPane not getJContentPane, and You're not supposed to overload it.
Instead, in your constructor (or other function that gets called right away) you do
要回答您的问题,我需要查看调用 getJContentPane 的代码,以确保您实际上在某处添加了该 JPanel。我还需要查看您是否已向 jContentPane 分配了某些内容,因为您仅在该面板为空时才添加列表。
我的猜测是,您实际上并未将返回的面板添加到对话框中,或者 jContentPane 已被分配了一个非空值。
对 myList.setVisible(true) 的调用没有任何意义,因为它尚未添加到窗口中。当对话框变得可见时,它的所有子项也将变得可见。
To answer your question I would need to see the code that calls getJContentPane to make sure that you are actually adding that JPanel somewhere. I would also need to see if you have assigned something to jContentPane since you only add the list if that panel is null.
My guess is that you are not actually adding the returned panel to the dialog or that jContentPane has been assigned a non null value.
The call to myList.setVisible(true) makes no sense since it is not added to a Window yet. When a dialog is made visible all its children will be made visible as well.