JAVA |我的动作监听器没有被调用

JAVA | My action listener isn't being called

我正在使用动作侦听器来更改链接到组合框的 JLabel 图标。我让它工作但在添加一些代码以打开对话框后它停止调用动作侦听器(IDK如何破坏它)。我从我保存的备份中再次尝试,但无法正常工作。

目前它加载默认图标,但更改组合框中的值不会像以前那样调用侦听器。

这是我的程序的样子,它应该在这里显示德国国旗: demo_Image

这是我的代码:

package LTranslator;
import java.awt.event.ActionEvent;
import javax.swing.*;

public class LangTranslator extends javax.swing.JFrame {

public LangTranslator() {
    initComponents();
}

/**Calls the action listener*/
public void actionPerformed(ActionEvent e) {
    JComboBox cb = (JComboBox)e.getSource();
    String flagImage = (String)cb.getSelectedItem();
    updateLabel(flagImage);

}

/**Changes the icon */
protected void updateLabel(String name) {
    ImageIcon icon = createImageIcon("/images/" + name + ".png");
    imgOne.setIcon(icon);
    imgOne.setToolTipText("A drawing of a " + name.toLowerCase());
    if (icon != null) {
        imgOne.setText(null);
    } else {
        imgOne.setText("Image not found");
    }
}

/** Returns an ImageIcon, or null if the path was invalid. */
protected static ImageIcon createImageIcon(String path) {
    java.net.URL imgURL = LangTranslator.class.getResource(path);
    if (imgURL != null) {
        return new ImageIcon(imgURL);
    } else {
        System.err.println("Couldn't find file: " + path);
        return null;
    }
}

private void initComponents() {

    comboOne = new javax.swing.JComboBox<>();
    btnGetLanguage = new javax.swing.JButton();
    btnTranslate = new javax.swing.JButton();
    txtGetLanguage = new javax.swing.JTextField();
    cbxOne = new javax.swing.JCheckBox();
    cbxTwo = new javax.swing.JCheckBox();
    cbxThree = new javax.swing.JCheckBox();
    cbxFour = new javax.swing.JCheckBox();
    imgOne = new javax.swing.JLabel();
    jScrollPane1 = new javax.swing.JScrollPane();
    taOne = new javax.swing.JTextArea();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setTitle("Translator");

    comboOne.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Japanese", "Swedish", "German", "Dutch" }));
    comboOne.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            comboOneActionPerformed(evt);
        }
    });

    btnGetLanguage.setText("Get Language");
    btnGetLanguage.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnGetLanguageActionPerformed(evt);
        }
    });

    btnTranslate.setText("Translate");
    btnTranslate.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnTranslateActionPerformed(evt);
        }
    });

    cbxOne.setText("Hello World");

    cbxTwo.setText("How are you?");

    cbxThree.setText("What day is it today?");

    cbxFour.setText("Goodbye");

    imgOne.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Japanese.png"))); // NOI18N

    taOne.setColumns(20);
    taOne.setRows(5);
    taOne.setEnabled(false);
    jScrollPane1.setViewportView(taOne);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(46, 46, 46)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(comboOne, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(35, 35, 35)
                    .addComponent(btnGetLanguage)
                    .addGap(43, 43, 43)
                    .addComponent(txtGetLanguage))
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(cbxFour)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(cbxTwo)
                                .addComponent(cbxOne)
                                .addComponent(cbxThree))
                            .addGap(31, 31, 31)
                            .addComponent(btnTranslate, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGap(18, 18, 18)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 243, Short.MAX_VALUE)))
            .addContainerGap())
        .addGroup(layout.createSequentialGroup()
            .addGap(140, 140, 140)
            .addComponent(imgOne)
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(35, 35, 35)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(comboOne, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(btnGetLanguage)
                .addComponent(txtGetLanguage, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(imgOne)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 25, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(cbxOne)
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(cbxTwo)
                        .addComponent(btnTranslate, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addComponent(cbxThree)
                    .addGap(17, 17, 17)
                    .addComponent(cbxFour))
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(17, 17, 17))
    );

    pack();
}// </editor-fold>

您将一个 ActionListener 添加到调用 comboOneActionPerformed(evt) 的 ComboBox,但触发更改的方法名为 actionPerformed(ActionEvent e)。改变这个,它应该工作。我想知道这是否可以编译。 您还可以实现 ActionListener,然后像这样添加 ActionListener:comboOne.addActionListener(this);