JComboBox直到点击才显示

时间:2012-11-11 11:02:52

标签: java swing

我有一堆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);

}

1 个答案:

答案 0 :(得分:1)

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

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);

}

enter image description here

您在ActionListenernew JComboBox(Vector/Object[])时遇到问题。