您好我在将单个侦听器链接到多个按钮时遇到问题。我正在尝试使用内部类,但似乎我在某处弄错了。有人可以指出我正确的方向吗?如果它有助于自动更正的东西( :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: ");
}
}
}
}
在此先感谢,克里斯