【发布时间】:2016-04-21 17:02:23
【问题描述】:
我正在编写一个 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);
【问题讨论】:
标签: java swing actionlistener jtextfield jcombobox