我正在编写一个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);
答案 0 :(得分:1)
你可以使用
questions.setText(alleQuest.getSelectedItem().toString());
在ActionListener中。 但是textfileld的问题应该在actionPerformed方法
之上定义答案 1 :(得分:1)
您有2个关于
的选项else:
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
}
};
存储为封闭类的字段(在您的情况下为DateiAdapter类)s
方法中给定的s
值更新所需的每个GUI组件。说实话,这是确切的地方,因为EDT会调用actionPerformed
,所有的GUI都会发生变化。actionPerformed
在s
课外使用,请按照第1点,然后选择2,然后为该字段添加公共getter。在你的情况下推杆
DateiAdapter
中的questions.setText(alleQuest.getSelectedItem().toString());
将是最直接的选择。
答案 2 :(得分:1)
因此,基本上,这似乎是一个上下文问题,<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div id="tomato">
<body>
<button id="totato">total</button>
</body>
</div>
字段需要在questions
可以引用它的上下文中定义。
最简单的方法之一就是让cbActionListener
成为该类的实例字段,例如......
questions