繁体   English   中英

返回已填充JComboBox的值,并使用这些值填充JTextField

[英]Return value of filled JComboBox and using the values to fill JTextField

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

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

请帮忙!

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方法上方定义

您有2个关于

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
    }
};
  1. 可以将s作为封闭类的字段(在您的情况下为DateiAdapter类)
  2. 使用actionPerformed方法中的给定s值更新所需的每个GUI组件。 老实说,这是执行此操作的确切位置,因为EDT调用actionPerformed ,所有GUI更改都应在该处进行。
  3. 如果要在DateiAdapter类之外使用s ,请遵循第1点,然后选择2,并为该字段添加public getter。

在你的情况下把questions.setText(alleQuest.getSelectedItem().toString()); actionPerformed中将是最直接的选择。

因此,这似乎基本上是一个上下文问题, questions字段需要在cbActionListener可以引用它的上下文中定义。

最简单的方法之一就是将questions设为类的实例字段,例如...

class DateiAdapter implements ActionListener {

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

    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
                questions.setText(s);
            }
        };

        allQuest.addActionListener(cbActionListener);

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM