gpt4 book ai didi

java - 顽固的 JComboBox

转载 作者:搜寻专家 更新时间:2023-11-01 01:37:24 25 4
gpt4 key购买 nike

我有一个 JComboBox,如下面的代码所示。当程序启动时,它的 actionPerformed 事件会立即触发,导致一些空指针异常,所以我想从没有选择任何元素开始。但是,由于某种原因它不起作用(无论我做什么,它总是以显示“美元/土耳其里拉”开始)。有人知道吗?

JComboBox comboBox = new JComboBox(new String[]{"USD/TRY", "EUR/TRY", "GBP/TRY"});

comboBox.setSelectedIndex(-1); // doesnt change anything
comboBox.setSelectedIndex(2); // doesnt change anything
comboBox.setSelectedItem(null); // doesnt change anything

更新:像下面这样构建组合框也不会改变任何东西

JComboBox comboBox = new JComboBox(); 

comboBox.addItem("USD/TRY");
comboBox.addItem("EUR/TRY");
comboBox.addItem("GBP/TRY");

这是 SSCCE:

public class MainFrame {

private final JTextArea textArea = new JTextArea();
private IExchangeSource s;

public MainFrame(final IExchangeSource s) {
//build gui
final JComboBox comboBox = new JComboBox();

comboBox.addItem("USD/TRY");
comboBox.addItem("EUR/TRY");
comboBox.addItem("GBP/TRY");

comboBox.setSelectedIndex(-1); // doesnt change anything
//comboBox.setSelectedIndex(2); // doesnt change anything


JFrame f = new JFrame("Currency Converter");
JPanel p = new JPanel(new BorderLayout());
textArea.setName("textarea");
textArea.setWrapStyleWord(true);
textArea.setLineWrap(true);
this.s = s;

comboBox.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
String exchange = (String) comboBox.getSelectedItem();

s.getData(exchange);
}
});

p.add(comboBox, BorderLayout.NORTH);
p.add(textArea, BorderLayout.CENTER);
f.setPreferredSize(new Dimension(300, 300));
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.add(p);
comboBox.setSelectedIndex(0);
f.setVisible(true);
}
}

最佳答案

您的(不完整的)示例调用

comboBox.setSelectedIndex(0);

就在变得可见之前,取消之前的任何设置。 添加监听器之前设置所需的初始索引,并且不要忘记从 EDT 开始, 如 sscce 所示下面。

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class MainFrame {

private final JTextArea textArea = new JTextArea();


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

@Override
public void run() {
new MainFrame();
}
});
}
public MainFrame() {
//build gui
final JComboBox comboBox = new JComboBox();

comboBox.addItem("USD/TRY");
comboBox.addItem("EUR/TRY");
comboBox.addItem("GBP/TRY");

JFrame f = new JFrame("Currency Converter");
JPanel p = new JPanel(new BorderLayout());
textArea.setName("textarea");
textArea.setWrapStyleWord(true);
textArea.setLineWrap(true);

comboBox.setSelectedIndex(-1);
comboBox.addActionListener(new ActionListener() {

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

p.add(comboBox, BorderLayout.NORTH);
p.add(textArea, BorderLayout.CENTER);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setSize(new Dimension(300, 300));
f.add(p);
f.setVisible(true);
}
}

关于java - 顽固的 JComboBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8730116/

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