JComboBox没有显示箭头

Deo*_*Deo 4 java layout swing jcombobox

我一直在搜索这个网站并谷歌搜索我的问题的解决方案,我找不到任何东西.我认为应该只是工作; 但事实并非如此.我的JComboBox的箭头图标没有显示,我找不到任何地方将其可见性设置为true.

这是我的代码:

public class Driver implements ActionListener {

private JTextField userIDField;
private JTextField[] documentIDField;
private JComboBox repository, environment;
private JButton close, clear, submit;
private JFrame window;

    public Driver()
    {
    window = makeWindow();
    makeContents(window);
    window.repaint();
    }

    private JFrame makeWindow()
    {
    JFrame window = new JFrame("");
    window.setSize(500,300);
    window.setLocation(50,50);
    window.getContentPane().setLayout(null);
    window.setResizable(false);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setVisible(true);

    return window;
    }

    private void makeContents(JFrame w)
    {
    makeDropDowns(w);
    w.repaint();
    }

    private void makeDropDowns(JFrame w)
    {       
    String[] repositoryArray = {"Click to select", "NSA", "Finance", "Test"};
    repository = new JComboBox(repositoryArray);
    repository.setSelectedIndex(0);
    repository.addActionListener(this);
    repository.setSize(150,20);
    repository.setLocation(175,165);
    repository.setEditable(false);
    w.add(repository);

    String[] environmentArray = {"Click to select", "Dev", "Test", "Qual"};
    environment = new JComboBox(environmentArray);
    environment.setSelectedIndex(0);
    environment.addActionListener(this);
    environment.setSize(150,20);
    environment.setLocation(175,195);
    //environment.setEditable(false);
    w.add(environment,0);
}

    public void actionPerformed(ActionEvent e) 
    {

    String repositoryID = "null", environmentID = "null";

    if (e.getSource() == repository)
        {
        repositoryID = (String)repository.getSelectedItem();
        }

    if(e.getSource() == environment)
        {
        environmentID = (String)environment.getSelectedItem();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是一个问题图片的链接:

图片

如果有人可以提供帮助那将是非常棒的.

DGo*_*erg 8

看来这不是您遇到的问题,但由于出现了相同的箭头消失问题,因此我发现了此信息。

在我的情况下,这是由于我错误地.removeAll()在上使用的,JComboBox而不是.removeAllItems()当我尝试清空然后JComboBox在刷新所使用的数据后重用时所导致的。只是以为我会将它作为答案,以防万一有人出于类似原因遇到此线程。


tra*_*god 5

您显示的代码有效,但看起来您正在与封闭容器的默认布局作斗争.这ComboTestJPanel默认值FlowLayout.

附录:在一般情况下,千万不能绝对定位,如在你的更新.我已经改变了使用的例子GridLayout; 注释掉setLayout()调用以查看默认值FlowLayout.

在此输入图像描述

import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
* @see https://stackoverflow.com/a/10824504/230513
*/
public class ComboTest extends JPanel {

    private JComboBox repository = createCombo(new String[]{
        "Click to select", "NSA", "Finance", "Test"});
    private JComboBox environment = createCombo(new String[]{
        "Click to select", "Dev", "Test", "Qual"});

    public ComboTest() {
        this.setLayout(new GridLayout(0, 1));
        this.add(repository);
        this.add(environment);
    }

    private JComboBox createCombo(String[] data) {
        final JComboBox combo = new JComboBox(data);
        combo.setSelectedIndex(1);
        combo.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println(e.getActionCommand()
                    + ": " + combo.getSelectedItem().toString());
            }
        });
        return combo;
    }

    private void display() {
        JFrame f = new JFrame("ComboTest");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new ComboTest().display();
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)