Java Swing JList

发布于 2024-08-19 06:33:05 字数 990 浏览 2 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

禾厶谷欠 2024-08-26 06:33:05

我找不到你在哪里设置 JList 的模型?

mylist = new JList();    
mylist.setModel(model);

请查看Java 编程语言的代码约定

FensterDrei而不是 fensterdrei
myList而不是mylist

I can't find where you are setting the model of the JList?

Something like

mylist = new JList();    
mylist.setModel(model);

Please have a look at the Code Conventions for the Java Programming Language

FensterDreiinstead of fensterdrei
myListinstead of mylist

生来就爱笑 2024-08-26 06:33:05

它是 getContentPane 而不是 getJContentPane,并且您不应该重载它。

相反,在您的构造函数(或立即调用的其他函数)中,您可以

getContentPane().setLayout(new BorderLayout());
getContentPane().add(getJList(), BorderLayout.CENTER);

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

getContentPane().setLayout(new BorderLayout());
getContentPane().add(getJList(), BorderLayout.CENTER);
你是年少的欢喜 2024-08-26 06:33:05

要回答您的问题,我需要查看调用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文