Bizarro Swing JList 模型行为

发布于 2024-11-30 14:40:43 字数 836 浏览 0 评论 0原文

我的 Swing 应用程序中遇到一个问题,我通过向其传递一个模型来创建一个简单的 JList - 即使模型已明显填充,JList 仍拒绝显示其自己的模型内容。

DefaultListModel dlm = new DefaultListModel();
String[] modelElems = {"Apple", "Orange", "Banana"};
for(int i = 0; i < modelElems.length; i++)
    dlm.add(i, modelElems[i]);

JList lstFruitList = new JList(dlm);
lstFruitList.setVisible(true);

当我的 Swing 应用程序运行时,我在屏幕上看到 JList,但它完全是空的!我看过无数的例子,翻阅了 Swing 教程,但似乎无法弄清楚发生了什么。有人以前遇到过这种情况吗?!?有什么明显-明显-错误的事情吗?!?

注意:

以下打印语句确实显示我的模型有 3 个元素:

// Prints "Fruit List model has a size of 3"
System.out.println("Fruit List model has a size of " + dlm.size());

但是,如果我尝试循环并打印模型中水果的名称,请调用(String)dlm.get(i) 在每次迭代时(其中 i 是迭代变量),它将每个模型元素打印为 null。 ..

嗯嗯

I am having an issue in my Swing app where I am creating a simple JList by passing it a model - and even though the model is demonstrably populated, the JList refuses to display it's own model's contents.

DefaultListModel dlm = new DefaultListModel();
String[] modelElems = {"Apple", "Orange", "Banana"};
for(int i = 0; i < modelElems.length; i++)
    dlm.add(i, modelElems[i]);

JList lstFruitList = new JList(dlm);
lstFruitList.setVisible(true);

When my Swing app runs, I see the JList on screen, but it is completely empty! I've looked at countless examples, poured over the Swing tutorials, and cannot seem to figure out what is going on. Anybody ever had this happen to them before?!? Anything that is glaringly-obviously-wrong?!?

NOTE:

The following print statement indeed shows that my model has 3 elements:

// Prints "Fruit List model has a size of 3"
System.out.println("Fruit List model has a size of " + dlm.size());

However, if I try to loop through and print the names of the fruits in my model, by calling (String)dlm.get(i) at every iteration (where i is the iteration var), it prints each model element as null...

hmmmm

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

梅倚清风 2024-12-07 14:40:43

该代码对我来说效果很好。一些想法:

  • 我看到您在 JList 上调用了 setVisible ,您究竟如何将其添加到您正在显示的内容中?
  • 您是否在显示 JFrame 之后添加元素?如果我没记错的话,那效果不太好,我想你必须重新粉刷所有东西。
  • 确保您不会在某些时候弄乱列表模型;也许在代码稍后的某个时刻,您要更改它?
  • 确保您实际上正在查看正确的元素;也许你的列表隐藏在其他东西后面?(尝试设置背景颜色,即 lstFruitList.setBackground(Color.BLUE);
  • 最后的想法,你确定你已经正确编译了它吗?我已经有时意外地忘记编译,或者弄乱了某些东西并运行了旧版本的代码,并且对为什么某些东西不起作用感到困惑,

作为参考,这是我运行的代码:

import java.awt.*;
import javax.swing.*;

public class javatest{

    public static void main(String[] args){
            JFrame f = new JFrame("HELLO");
            DefaultListModel dlm = new DefaultListModel();
            String[] modelElems = {"Apple", "Orange", "Banana"};
            for(int i = 0; i < modelElems.length; i++)
                dlm.add(i, modelElems[i]);

            JList lstFruitList = new JList(dlm);
            lstFruitList.setVisible(true);

            JPanel p = new JPanel();
            p.add( lstFruitList );
            f.add( p );
            f.setLocation(0,0);
            f.setSize(400,400);
            f.setVisible(true);
    }
}

That code works fine for me. Some thoughts:

  • I see that you call the setVisible on the JList, how exactly are you adding it to whatever you are displaying?
  • Are you adding the elements after you display your JFrame? If I remember correctly, that won't work well, I think you have to repaint everything.
  • Make sure that you're not messing around with the list model at some point; Maybe at some point later in the code, you're changing it?
  • Ensure that you are actually looking at the right element; maybe your list is hidden behind something else?(try setting the background color, i.e. lstFruitList.setBackground(Color.BLUE);
  • Final thought, are you sure that you've compiled it properly? I've sometimes accidentally forgotten to compile, or had messed up something and was running an older version of the code and was confused as to why something didn't work.

For reference, here's the code that I ran:

import java.awt.*;
import javax.swing.*;

public class javatest{

    public static void main(String[] args){
            JFrame f = new JFrame("HELLO");
            DefaultListModel dlm = new DefaultListModel();
            String[] modelElems = {"Apple", "Orange", "Banana"};
            for(int i = 0; i < modelElems.length; i++)
                dlm.add(i, modelElems[i]);

            JList lstFruitList = new JList(dlm);
            lstFruitList.setVisible(true);

            JPanel p = new JPanel();
            p.add( lstFruitList );
            f.add( p );
            f.setLocation(0,0);
            f.setSize(400,400);
            f.setVisible(true);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文