【问题标题】:Return value of filled JComboBox and using the values to fill JTextField填充的 JComboBox 的返回值并使用这些值填充 JTextField
【发布时间】: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


    【解决方案1】:

    你可以使用

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

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

    【讨论】:

    • 感谢鹤山的建议!
    【解决方案2】:

    关于

    ,您有 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 组件。老实说,这是执行此操作的确切位置,因为 actionPerformed 由 EDT 调用,所有 GUI 更改都应在此处发生。
    3. 如果您希望 sDateiAdapter 类之外使用,请遵循第 1 点,然后选择第 2 点,并为该字段添加公共 getter。

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

    【讨论】:

    • 感谢您的帮助!
    【解决方案3】:

    所以,这基本上,似乎归结为一个上下文问题,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);
            }
        }
    }
    

    【讨论】:

    • MadProgrammer,有没有办法让我们有一天能聊上几分钟?我想问你一些与编程有关的问题,我真的从你和这里的许多其他人那里学到了很多东西,我很想和你讨论一些事情。如果可能,请回复并让我们交换我们的电子邮件或其他东西。我保证我不会烦人:)
    • @MadProgrammer 我有点忙,所以这个chat 可能需要我一些时间来回复,但我有兴趣和你聊天,所以,抓住你:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-03
    • 2014-01-24
    • 1970-01-01
    相关资源
    最近更新 更多