Sam
Sam

Reputation: 374

ActionListener not working in Java

I have created a JPanel over JFrame and added a JButon and JLabel to the JPanel. But the ActionlListener does not seems work. When i click on JButton, Nothing Happens. Please someone Help me. Thanks in Advance. Here is my code

public class Trials implements ActionListener {

    JButton scoreButton;
    JLabel score;
    JPanel MyPanel;
    int ScoreAmount=0;

    public JPanel createPanel()
    {
        JPanel MyPanel =new JPanel();
        MyPanel.setLayout(null);
        MyPanel.setSize(50, 50);
        MyPanel.setBackground(Color.cyan);
        JLabel score =new JLabel(""+ScoreAmount);
        score.setSize(50, 50);
        score.setLocation(250,50);
        score.setForeground(Color.red);
        MyPanel.add(score);         
        JButton scoreButton =new JButton("add");
        scoreButton.setSize(100, 50);
        scoreButton.setLocation(100,50);
        scoreButton.setBackground(Color.red);
        scoreButton.addActionListener(this);
        MyPanel.add(scoreButton);           
        MyPanel.setOpaque(true);
        MyPanel.setVisible(true);
        return MyPanel;         
    }

    public void actionPerformed(ActionEvent e)
    {
      if(e.getSource()==scoreButton)
      {
            ScoreAmount = ScoreAmount+1;
            score.setText(""+ScoreAmount);              
      }
    }

    public static void display()
    {
        JFrame MyFrame = new JFrame();
        Trials tr =new Trials();
        MyFrame.setContentPane(tr.createPanel());
        MyFrame.setSize(500, 500);
        MyFrame.setVisible(true);
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable(){
        public void run(){
            display();
                        }
                });
        }    
}

Upvotes: 1

Views: 107

Answers (2)

mKorbel
mKorbel

Reputation: 109815

you shadowing local variables declared for public class Trials implements ActionListener {

JButton scoreButton;
JLabel score;
JPanel MyPanel;
int ScoreAmount=0;

should be in public JPanel createPanel()

MyPanel = new JPanel();
score = new JLabel("" + ScoreAmount);
scoreButton = new JButton("add");

not

JPanel MyPanel = new JPanel();
JLabel score = new JLabel("" + ScoreAmount);
JButton scoreButton = new JButton("add");

  • remove MyPanel.setLayout(null);, default FlowLayout implemented in JPanel do that by default, then add JComponents to JPanel only MyPanel.add(componentVariable) witout any sizing for JPanels childs

  • call MyFrame.pack() instead of MyFrame.setSize(500, 500);

Upvotes: 3

cowls
cowls

Reputation: 24334

When you define your score button you do not assign the class level field, instead you create a new local variable.

JButton scoreButton =new JButton("add");
scoreButton.setSize(100, 50);
scoreButton.setLocation(100,50);
scoreButton.setBackground(Color.red);
scoreButton.addActionListener(this);
MyPanel.add(scoreButton);

Should become:

this.scoreButton =new JButton("add");
scoreButton.setSize(100, 50);
scoreButton.setLocation(100,50);
scoreButton.setBackground(Color.red);
scoreButton.addActionListener(this);
MyPanel.add(scoreButton);

So when the actionPerformed method is called the scoreButton is probably null

Upvotes: 0

Related Questions