Java JComboBox 不显示

标签 java swing layout-manager jcombobox

Java 新手。我无法显示下拉框。

我最初在按钮之前有代码,但是当我运行代码(intelliJ idea)时,下拉菜单和按钮都没有显示。

stackoverflow 需要更多详细信息。不知道该添加什么。我确信这只是语法中的新手错误。

package com.example.guiTest;

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

import java.lang.Object;
import java.awt.Component;
import java.awt.Container;
import javax.swing.JComponent;
import javax.swing.JComboBox;

public class Main {

public static void main(String[] args) {

    JFrame window = new JFrame("Position");
    window.setVisible(true);
    window.setSize(500, 500);
    window.setResizable(true);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    window.setLocation(dim.width/2-window.getSize().width/2, dim.height/2-window.getSize().height/2);;

    JPanel panel = new JPanel();
    panel.setLayout(null);
    window.add(panel);

    JLabel test = new JLabel("This is a test");
    test.setBounds(20,0,120,120);
    panel.add(test);

    JButton button = new JButton("Compile");
    button.setBounds(250, 250, 120, 50);
    panel.add(button);


    String[] noteArray = {"a", "b"};

    JComboBox note = new JComboBox(noteArray);
    note.setPreferredSize(new Dimension(200,130));
    note.setLocation(new Point(200,200));
    note.setEditable(true);
    note.setSelectedIndex(3);
    note.setVisible(true);

    panel.add(note);






}

}

最佳答案

您在面板 JPanel 上使用 null 布局,简单的答案是 null 布局要求您完全指定所有添加组件的大小和位置,即 size 不是 首选尺寸。您设置的是后者,而不是前者。

更好的答案是不要使用 null 布局和 setBounds(...)。虽然 null 布局和 setBounds() 对于 Swing 新手来说似乎是创建复杂 GUI 的最简单、最好的方法,但创建的 Swing GUI 越多,使用它们时遇到的困难就越严重。当 GUI 调整大小时,它们不会调整组件的大小,它们是增强或维护的皇家女巫,当放置在滚动 Pane 中时,它们会完全失败,在所有平台或与原始分辨率不同的屏幕分辨率上查看时,它们看起来非常糟糕.

编辑:
或者您最终将 JComboBox 放置在 JButton 的正上方(正如您的代码所做的那样!)。

编辑:
另一个问题:在将所有组件添加到 GUI 之前,不要在 JFrame 上调用 setVisible(true)

编辑:
还有另一个问题:note.setSelectedIndex(3);
注释组合框中只有 2 个项目,那么为什么要尝试将所选索引设置为 3?这肯定会失败。

例如:

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

public class BetterMain extends JPanel {
    private static final String[] ITEMS = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
    private static final int ROWS = 25;
    private static final int COLUMNS = 40;
    private JButton button = new JButton("Button");
    private JTextField textField = new JTextField(COLUMNS / 2);
    private JComboBox<String> myCombo = new JComboBox<>(ITEMS);
    private JTextArea textArea = new JTextArea(ROWS, COLUMNS);    

    public BetterMain() {
        JPanel topPanel = new JPanel();
        topPanel.add(new JLabel("This is a JLabel"));
        topPanel.add(myCombo);
        topPanel.add(textField);
        topPanel.add(button);

        JScrollPane scrollPane = new JScrollPane(textArea);

        setLayout(new BorderLayout());
        add(scrollPane, BorderLayout.CENTER);
        add(topPanel, BorderLayout.PAGE_START);
    }

    private static void createAndShowGui() {
        BetterMain mainPanel = new BetterMain();

        JFrame frame = new JFrame("BetterMain");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }
}

关于Java JComboBox 不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34834690/

相关文章:

java - 如何更改 JFreechart 中点的颜色

java - Selenium 测试运行不会保存 cookie?

java - 将 REST 查询参数存储为静态字段

java - 在 JComboBox 中显示时区

Java JSpinner.DateEditor 使用 TimeZone March 时钟更改

java - 在 JFrame 中使用 BoxLayout

java - 如何阻止 JTextArea 和 JTextField 拉伸(stretch)?

java - 重写内部FrameClosing方法

java - JTree 不显示

java - 调整 GUI 的大小?