gpt4 book ai didi

java - JComboBox 在单击之前不显示

转载 作者:行者123 更新时间:2023-12-01 15:03:14 25 4
gpt4 key购买 nike

我有一堆 JComboBox,我已将其放入 JPanel 中,但 JComboBox 在单击之前不可见。有谁知道我可能做错了什么,初始化代码如下所示:

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

}

enter image description here

听起来您对 ActionListenernew JComboBox(Vector/Object[]) 有一些问题。

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com