将单个侦听器链接到多个按钮

时间:2013-01-31 21:15:09

标签: java swing variables actionlistener japplet

您好我将单个侦听器链接到多个按钮时遇到问题。我试图使用内部类,但似乎我在某处弄错了。有人能指出我正确的方向吗? 如果它有助于自动纠正thingy(:D)指向第59行说:

  

" createChampButton无法解析为变量"

代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;




public class GUI extends JApplet{



    public void init(){
        Container guiContainer = getContentPane();
        LayoutManager layout = new FlowLayout();
        guiContainer.setLayout(layout);


        //Create Championship Button
        final JButton createChampButton = new JButton("Create Championship");
        guiContainer.add(createChampButton);



        //Create Club Button
        final JButton createClubButton = new JButton ("Create Club");
        guiContainer.add(createClubButton);


        //Create Athlete Button
        final JButton createAthleteButton = new JButton ("Create Athlete");
        guiContainer.add(createAthleteButton);


        //Print Athletes Button
        final JButton printAthletesButton = new JButton ("Print all Athletes");
        guiContainer.add(printAthletesButton);


        //The quiet Listener
        ButtonListener aListener = new ButtonListener();
        printAthletesButton.addActionListener(aListener);
        createAthleteButton.addActionListener(aListener);
        createClubButton.addActionListener(aListener);
        createChampButton.addActionListener(aListener);

    }

     class ButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent event){
            JButton button = (JButton) event.getSource();
            //if (button.equals(printAthletesButton)){
                //JOptionPane.showMessageDialog(null, "Athlete name is: " +anAthlete.GetAthleteName());
            //  JOptionPane.showMessageDialog(null, "Athlete age is: " + anAthlete.GetAge());
                //}
            if(button.equals(createChampButton)){
                    Championship aChampionship = new Championship("","");
                    aChampionship.champName = JOptionPane.showInputDialog("Enter Championship Name: ");
                    aChampionship.duration = JOptionPane.showInputDialog("Enter Championship Duration: ");
            }       
        }
    }
}
提前谢谢, 克里斯

2 个答案:

答案 0 :(得分:4)

createChampButtoninit()

中的局部变量

要在其他地方访问它,您需要将其更改为类中的字段。

答案 1 :(得分:2)

createChampButton未在您的其他方法中定义,因此范围无法访问该范围。我看到三个选项如何解决这个问题:

1)您使用component.getActionCommand()代替 - 您可以将其与您的JButton所持有的文本进行比较(类似if( evt.getSource().getActionCommand().equals("Create Championship")

2)您可以在init方法中定义ActionListener:

public void init(){
    Container guiContainer = getContentPane();
    LayoutManager layout = new FlowLayout();
    guiContainer.setLayout(layout);


    //Create Championship Button
    final JButton createChampButton = new JButton("Create Championship");
    guiContainer.add(createChampButton);



    //Create Club Button
    final JButton createClubButton = new JButton ("Create Club");
    guiContainer.add(createClubButton);


    //Create Athlete Button
    final JButton createAthleteButton = new JButton ("Create Athlete");
    guiContainer.add(createAthleteButton);


    //Print Athletes Button
    final JButton printAthletesButton = new JButton ("Print all Athletes");
    guiContainer.add(printAthletesButton);


    //The quiet Listener
    ActionListener aListener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent event){
            JButton button = (JButton) event.getSource();
            //if (button.equals(printAthletesButton)){
                //JOptionPane.showMessageDialog(null, "Athlete name is: " +anAthlete.GetAthleteName());
            //  JOptionPane.showMessageDialog(null, "Athlete age is: " + anAthlete.GetAge());
                //}
            if(button.equals(createChampButton)){
                    Championship aChampionship = new Championship("","");
                    aChampionship.champName = JOptionPane.showInputDialog("Enter Championship Name: ");
                    aChampionship.duration = JOptionPane.showInputDialog("Enter Championship Duration: ");
            }       
        }
    };
    printAthletesButton.addActionListener(aListener);
    createAthleteButton.addActionListener(aListener);
    createClubButton.addActionListener(aListener);
    createChampButton.addActionListener(aListener);

}

}

3)您将JComponents定义为实例变量 - 在init()方法之外声明它们(但仍将它们分配到内部)

的问候,
Danyel