java - JComboBox 使用 swing 无效,在 Apple 上打印时布局无效,但在 Windows 上不存在 NullPointerException

标签 java swing jcombobox

我正在使用 Swing 和 JCombobox for Java 构建一个简单的 GUI。 GUI 加载到我的 PC 上,但在我的合作伙伴 Apple 计算机上导致 NullPointerException。

我尝试打印 JComboBox,但收到以下“无效”消息。有谁知道什么可能会导致“...无效,布局...”以及为什么 JComboBox 在苹果笔记本电脑上导致 NullPointerException 而不是 Windows 笔记本电脑?

打印 JComboBox 时的消息:

Jcombobox javax.swing.JComboBox[,0,0,0x0,invalid,layout=javax.swing.plaf.metal.MetalComboBoxUI$MetalComboBoxLayoutManager,alignmentX=0.0,alignmentY=0.0,border=,flags=328,maximumSize=,minimumSize=,preferredSize=,isEditable=false,lightWeightPopupEnabled=true,maximumRowCount=8,selectedItemReminder=Search your own pasted text]

当我尝试从 Oracle 教程站点打印 JComboBox 时,实际上发现了相同的“无效”:https://docs.oracle.com/javase/tutorial/uiswing/examples/components/ComboBoxDemoProject/src/components/ComboBoxDemo.java [注意:通过打印 JCombox petlist]:

  public ComboBoxDemo() {
        super(new BorderLayout());

        String[] petStrings = { "Bird", "Cat", "Dog", "Rabbit", "Pig" };

        //Create the combo box, select the item at index 4.
        //Indices start at 0, so 4 specifies the pig.
        JComboBox petList = new JComboBox(petStrings);
        petList.setSelectedIndex(4);
        petList.addActionListener(this);
        System.out.println(petList);

下面也是我们正在制作的项目的代码[只是 JComboBox 部分:

public String comboBox() {
        String str = "Search Lyric Database";
        String[] options = new String[] { "Search Lyric Database", "Search Books Database", "Search your own file", "Search website",
                "Search your own pasted text" };

        JComboBox<String> bookList = new JComboBox<>(options);
        bookList.setSelectedIndex(3);
        System.out.println("JCOMBOBOX" + bookList );

        pane.add(bookList, BorderLayout.PAGE_START);

并注意 Windows 计算机的输出[打印时]中的“无效”。

...javax.swing.JComboBox[,0,0,0x0,invalid,layout=javax.swing.plaf.metal.......

有人知道“无效”是什么意思吗? 有谁知道为什么从 Apple 计算机单击 JCombobox 时会导致 nullPointerException [但在 Windows 上则不然] 以及我们如何解决此问题?

谢谢

最佳答案

“无效”基本上意味着 Component.isValid() 返回 false ( https://github.com/AdoptOpenJDK/openjdk-jdk8u/blob/master/jdk/src/share/classes/java/awt/Component.java#L8134 )。

根据JavaDoc isValid() 方法

Determines whether this component is valid. A component is valid when it is correctly sized and positioned within its parent container and all its children are also valid. In order to account for peers' size requirements, components are invalidated before they are first shown on the screen. By the time the parent container is fully realized, all its components will be valid.

所以这可能只是意味着您从未将此 JComboBox 添加到容器中和/或该容器从未显示过(尚未)。

所以这肯定不是您的 NullPointerException

的原因

关于java - JComboBox 使用 swing 无效,在 Apple 上打印时布局无效,但在 Windows 上不存在 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59212302/

相关文章:

java - double 方法的 null 检测

java - Jlink Spring Boot

java - 导致其他组件不显示的绘制方法

java - JComboBox 设置 selectedItem 不起作用

java - 更改 Java swing 应用程序中的字体样式、大小

java - 获取匿名类或内部类的父类(super class)

java - 如何通过单击菜单项来更改布局?

java - 将 keyEvent 应用于方法

java - JTree添加节点和更新

java - MigLayout 行高和更改字体大小