我有一堆JComboBox,我已将它们放在JPanel中,但是单击它们后才能看到JComboBoxes。有谁知道我可能做错了什么,初始化代码如下所示:

public class DelegateUI extends JFrame implements Observer, ActionListener{
public static int vertexPoints[][]= {
    {586, 584}, {586, 754}, {83, 754}, {83, 582}, // 1 - 4
    {414, 246}, {340, 514}, {479, 514},  // 5 - 8
    {618, 108}, {700, 203}, {493, 336}, {492, 126}, // 9 - 11
    {935, 123}, {937, 470}, {729, 473}, {730, 125}, // 12 - 15
    {772, 557}, {656, 672}, {608, 410},
    {1042, 120}, {1104, 179}, {1071, 508}, {967, 169}   // 19 - 22
    };
private Model model;
private JPanel topPanel;
private JComboBox searchType;
private JComboBox startVertex;
private JComboBox goalVertex;
Object [] searchTypes = { "Breadth First", "Uniform Cost", "Greedy Best First", "A*"};
Object [] vertexNumbers = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15",
        "16", "17", "18", "19", "20", "21", "22"};



public DelegateUI(Model m){

    this.model = m;
    m.addObserver(this);
    setSize(1200, 1000);

    topPanel = new JPanel();
    topPanel.setBackground(Color.BLACK);
    searchType = new JComboBox(searchTypes);
    startVertex = new JComboBox(vertexNumbers);
    goalVertex = new JComboBox(vertexNumbers);

    searchType.setSelectedIndex(0);
    searchType.addActionListener(this);

    startVertex.setSelectedIndex(3);
    startVertex.addActionListener(this);
    startVertex.setActionCommand("start");

    goalVertex.setSelectedIndex(19);
    goalVertex.addActionListener(this);
    goalVertex.setActionCommand("goal");

    topPanel.add(searchType);
    topPanel.add(startVertex);
    topPanel.add(goalVertex);


    this.setLayout(new BorderLayout());
    this.add(topPanel, BorderLayout.NORTH);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);

}

最佳答案

我使用了您的代码,它对我有用:

public void createGUI(){

        setSize(1200, 1000);

        topPanel = new JPanel();

        JComboBox searchType = new JComboBox(new Object[]{"1", "2"});
        JComboBox startVertex = new JComboBox(new Object[]{"1", "2"});
        JComboBox goalVertex = new JComboBox(new Object[]{"1", "2"});

        searchType.setSelectedIndex(1);
        startVertex.setSelectedIndex(1);
        startVertex.setActionCommand("start");

        goalVertex.setSelectedIndex(1);
        goalVertex.setActionCommand("goal");

        topPanel.add(searchType);
        topPanel.add(startVertex);
        topPanel.add(goalVertex);


        this.setLayout(new BorderLayout());
        this.add(topPanel, BorderLayout.NORTH);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

}




听起来您在ActionListenernew JComboBox(Vector/Object[])上有问题。

关于java - JComboBox直到单击才显示,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13330524/

10-13 09:18