gpt4 book ai didi

java - 返回填充的 JComboBox 的值并使用该值填充 JTextField

转载 作者:行者123 更新时间:2023-12-01 21:53:21 26 4
gpt4 key购买 nike

我正在编写一个 JFrame,它通过 JMenubar 打开 JOptionPane。 JOptionPane 确实有一个包含许多不同字符串值的组合框。 JComboBox 之后有一个 JTextfield。

我希望将在 JComboBox 中选择的文本插入到下一个 JTextField 中,并在每次我在 JComboBox 中选择新字符串值时进行更新。

请帮忙!

class DateiAdapter implements ActionListener {

public void actionPerformed(ActionEvent event) {

JMenuItem change = (JMenuItem) event.getSource();

JComboBox alleQuest = new JComboBox(ah.getLine(1)); //this ComboBox gets a lot of different values from a different class

// Actionlistener
ActionListener cbActionListener = new ActionListener() {

public void actionPerformed(ActionEvent e) {

String s = (String) allQuest.getSelectedItem(); //gets the selected string of the JComboBox
System.out.println("\n" + s); // I want to use String s outside of here, too
}
};

allQuest.addActionListener(cbActionListener);

JTextField questions = new JTextField(); //THIS is the TextField I want to insert the different values of the ComboBox above
JTextField a2 = new JTextField();
JTextField b2 = new JTextField();
JTextField c2 = new JTextField();
JTextField answ2 = new JTextField();

if (change == itemChange) {

final JComponent[] input = new JComponent[] {
new JLabel("You change your question here:"),
allQuest,
quest2,
a2,
b2,
c2,
answ2, };

JOptionPane.showMessageDialog(null, input, "Change Question", JOptionPane.PLAIN_MESSAGE);

最佳答案

你可以使用

questions.setText(alleQuest.getSelectedItem().toString());

在 ActionListener 内。但是问题textfileld应该定义在actionPerformed方法之上

关于java - 返回填充的 JComboBox 的值并使用该值填充 JTextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34822301/

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