gpt4 book ai didi

java |我的 Action 监听器没有被调用

转载 作者:行者123 更新时间:2023-12-01 10:09:50 27 4
gpt4 key购买 nike

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

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

这就是我的程序的样子,它应该在此处显示德国国旗: 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);

关于 java |我的 Action 监听器没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36194427/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com