5

我一直在搜索这个网站和谷歌来解决我的问题,但我找不到任何东西。我认为它应该可以正常工作;但是,事实并非如此。我的 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();
        }
    }
}

这是问题图片的链接:

图片

如果有人可以提供帮助,那就太棒了。

4

4 回答 4

10

这似乎不是您遇到的问题,但是由于箭头消失的相同问题,我发现了这篇文章。

在我的情况下,这是由于我错误地使用.removeAll()JComboBox而不是.removeAllItems()当我试图清空然后JComboBox在刷新我正在使用的数据后重用。只是想我会将其作为答案,以防其他人出于类似原因遇到此线程。

于 2015-01-06T23:14:51.080 回答
5

您显示的代码有效,但看起来您正在与封闭容器的默认布局作斗争。这里,ComboTest是 aJPanel默认为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();
            }
        });
    }
}
于 2012-05-30T21:26:01.563 回答
2

I had the same issue. I fixed it by revalidating and repainting the panel with the following code :

myPanel.revalidate();
myPanel.repaint();
于 2014-09-06T22:41:15.957 回答
1

也许有点晚了,但对于那些仍在寻找一种简单且安全的方式来使用 JComboBox 的人来说,可以使用这个:

public class FixedJComboBox<E>
        extends JComboBox<E> {
    // Copied constructors
    public FixedJComboBox() {
        super();
    }
    public FixedJComboBox(ComboBoxModel<E> aModel) {
        super(aModel);
    }
    public FixedJComboBox(E[] items) {
        super(items);
    }
    public FixedJComboBox(Vector<E> items) {
        super(items);
    }

    @Override
    public void setBounds(int x, int y, int width, int height) {
        super.setBounds(x, y, width, height);

        // The arrow is the first (and only) component
        // that is added by default
        Component[] comps = getComponents();
        if (comps != null && comps.length >= 1) {
            Component arrow = comps[0];
            // 20 is the default width of the arrow (for me at least)
            arrow.setSize(20, height);
            arrow.setLocation(width - arrow.getWidth(), 0);
        }
    }
}

如此处所述,该错误是由错误地将箭头的位置和大小设置为 引起的(0,0),然后是一些重绘问题。通过简单地覆盖该setBounds()函数,在 UI/布局管理器错误地更新了箭头后,箭头总是被纠正。

此外,由于新组件是在旧组件之后添加的(即更高的索引),因此箭头将始终位于数组中的第一个元素(假设您不删除并重新添加箭头)。

此类的缺点是箭头的宽度现在由常量而不是 UI/布局管理器确定。

于 2018-08-15T21:52:12.503 回答