gpt4 book ai didi

Java JCombobox 和 JButton 问题

转载 作者:行者123 更新时间:2023-12-02 12:46:28 24 4
gpt4 key购买 nike

我目前正在为计算机科学类(class)开发一个项目,但遇到了问题。我有多个按钮和 2 个组合框,我希望它们都能彼此独立工作。不幸的是,每当我按下按钮时,它都会充当“选择”按钮并激活组合框中当前选择的任何内容!我怎样才能解决这个问题?我的代码片段如下

public void actionPerformed(ActionEvent e) {
// Sound Menu
String soundSelect = (String)soundBox.getSelectedItem();
int soundIndex = soundBox.getSelectedIndex();
switch (soundIndex){
case 0:
stopClip(meowSong);
stopClip(meowRemix);
ImageIcon catDel = new ImageIcon(getClass().getResource("tres-quatro.png"));
delEvent.setIcon(catDel);
ImageIcon catAdd = new ImageIcon(getClass().getResource("im-hungry.png"));
addEvent.setIcon(catAdd);
break;
case 1:
stopClip(meowRemix);
playMeow(meowSong);
catDel = new ImageIcon(getClass().getResource("tres-quatro.png"));
delEvent.setIcon(catDel);
catAdd = new ImageIcon(getClass().getResource("im-hungry.png"));
addEvent.setIcon(catAdd);
break;
case 2:
stopClip(meowSong);
playMeow(meowRemix);
ImageIcon thugCat = new ImageIcon(getClass().getResource("tres-quatro-thug.png"));
delEvent.setIcon(thugCat);
ImageIcon hungryThug = new ImageIcon(getClass().getResource("im-hungry-thug.png"));
addEvent.setIcon(hungryThug);
break;
}


// View Menu
String viewSelect = (String)viewTypes.getSelectedItem();
if(e.getActionCommand().equals("ADD EVENT"))
{
playMeow(meowSound);
createEvent();
}else if(e.getActionCommand().equals("DELETE EVENT"))
{
playMeow(meowSound);
deleteEvent();
}else{
panel.remove(displayMessage2);
panel.add(displayMessage);
panel.remove(text);
}

int selection = viewTypes.getSelectedIndex();
switch (selection)
{
case 0: displayMessage.setText("Enter the Sunday of the week you'd like to view! DD/MM");
panel.add(text);
panel.add(enter);
String date = text.getText();
if(e.getActionCommand().equals("Enter")){
weeklyCalendar(calendar.getHashmap(), date);
displayMessage.setText("Select a CATlendar view or you can ADD or DELETE an event!");
panel.remove(text);
panel.remove(enter);
}
break;
case 1: displayMessage.setText("Enter the date you'd like to view! DD/MM");
panel.add(text);
panel.add(enter);
date = text.getText();
if(e.getActionCommand().equals("Enter")){
playMeow(meowSound);
dailyCalendar(calendar.getHashmap(), date);
displayMessage.setText("Select a CATlendar view or you can ADD or DELETE an event!");
panel.remove(text);
panel.remove(enter);
}
break;
case 2: displayMessage.setText("Press Enter to view monthly");
panel.remove(text);
panel.add(enter);
if(e.getActionCommand().equals("Enter")){
monthlyCalendar();
displayMessage.setText("Select a CATlendar view or you can ADD or DELETE an event!");
panel.remove(enter);
}
break;
}

panel.revalidate();
panel.repaint();
SwingUtilities.windowForComponent(panel).pack();
}

最佳答案

whenever I press a button it works as a "select" button and activates whatever is currently selected in the combobox

这就是 ActionListener 代码的作用。无论哪个组件生成 ActionEvent,您始终都会执行 switch 语句。

为每个组件创建单独的 ActionListener,而不是尝试为所有组件共享单个 ActionListener。然后代码将针对每个组件而特定,并且不需要 if 语句来确定哪个组件生成了事件。

编辑:

How can you have individual actionListeners for each component?

  1. 您可以创建一个实现 ActionListener 接口(interface)的内部类

    comboBox.addActionListener( new MyComboBoxListener() );

    ...

    class MyComboBoxListener implements ActionListener
    {
    @Override
    public void actionPerformed(ActionEvent e)
    {
    // add your code here
    }
    }
  2. 您可以使用匿名内部类

    comboBox.addActionListenener( new ActionListener()
    {
    @Override
    public void actionPerformed(ActionEvent e)
    {
    // add your code here
    }
    });

关于Java JCombobox 和 JButton 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44752471/

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