0

First I am a beginner in java. I'm making a window with small button and a label (with 0 in default position), when I click on the button the label will change to 1 and when I tap another click the button will be 2. But, I have an error in calling the method.

my code:

package prototype;

import java.awt.BorderLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.*;


public class Prototype {

public static int count;

public static JLabel l;

public void Proto()
{       
JFrame f = new JFrame(); 

        JButton b = new JButton("click");

         JLabel lo = new JLabel("0");

        JPanel p = new JPanel();

        f.setBounds(120,120,500,500);

        b.addActionListener(new MyAction());

        p.add(lo);

        p.add(b);

        f.getContentPane().add(p,BorderLayout.CENTER);


        f.show();}

public class MyAction implements ActionListener {



    @Override

    public void actionPerformed(ActionEvent e) {

        count++;

        l.setText(Integer.toString(count));}





    public static void main(String[] args) {
       //I want to call the proto method but it give me an eror

        new proto();

    }}}
Henry
  • 2,953
  • 2
  • 21
  • 34
  • `new Prototype().Proto()` should call the correct method and class – SomeJavaGuy Apr 12 '17 at 13:59
  • Some semantics first: class names starts with Uppercase. Your class is `Prototype`. That's good. Now method names starts with lowercase. Here your method is `Proto` which should be proto. And when calling it, you need to construct `Prototype` first (by the `new` keyword). Then use `.proto()` on the constructed object. – KarelG Apr 12 '17 at 14:00
  • You seem to have forgotten to include the actual error in your question. Please [edit] the question and add that. – RealSkeptic Apr 12 '17 at 14:00
  • `l` is neither initialised nor has it been added to any component. When the `ActionListener` is triggered, your code will generate a `NullPointerException`. My first suggestion is, remove the `static` reference, you need to solve these kind of issues without resulting to `static`, this will require some modification to your code design – MadProgrammer Apr 12 '17 at 23:02

2 Answers2

0
public class Prototype extends JFrame{

    private static int count;

    private JLabel l;

    public Prototype() {
        super();

        JButton b = new JButton("click");

        l = new JLabel("0");

        JPanel p = new JPanel();

        b.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                count++;

                l.setText(Integer.toString(count));

            }

        });

        p.add(l);

        p.add(b);

        this.getContentPane().add(p, BorderLayout.CENTER);
        this.pack();
        this.setVisible(true);
    }

    public static void main(String...args){
        Prototype p=new Prototype();
    }

}

I changed the method to a constructor, to have the possibility of creating a object of type Prototype and directly create a frame with it. Also I extended the class with JFrame to not need to create an extra JFrame. Next step was to remove the ActionListener class and creating a new ActionListener while adding it to the button. In my eyes this is useful if you have several buttons with different functionalities, so you can see the function of the button directly just by looking at the code of the button. and the last step was to create a new Object of type Prototype in the main method

XtremeBaumer
  • 6,275
  • 3
  • 19
  • 65
  • thanks bro it show me the window but i have problem in clicks... but u solve answer my question :) – Medo Fsociety Apr 12 '17 at 14:10
  • whats the problem with the clicks? a better coding style would be to have a starter class with only the main method i posted. in that way you can separate GUI and processing – XtremeBaumer Apr 12 '17 at 14:14
  • Yeah brother I understand you I solve the click with chnage the name of ' l ' variable to 'lo' , but whene you say about the style of coding i m just trying to understand classes how it work it s my first step in java can i give you my gmail if i had another problems ? – Medo Fsociety Apr 12 '17 at 14:18
  • just post here on stackoverflow, i am pretty active. and even if i am not, others are – XtremeBaumer Apr 12 '17 at 14:21
  • A better solution - however :P - I would have favoured `pack` over `setBounds` and it doesn't explain 1- what the problem the OP was having and 2- Why this solution would be better - yes it works, but what has the OP actually learned, other than to copy and paste code – MadProgrammer Apr 12 '17 at 23:03
  • @MadProgrammer you are right, i will add some explanation – XtremeBaumer Apr 13 '17 at 06:43
-1

If I we're you use a SwingWorker instead of manually setting the text of JLabel. Because this is not a proper way updating your GUI. This should be done using SwingWorker. Please read about publish and processmethod.

Francisunoxx
  • 1,440
  • 3
  • 22
  • 45
  • Ok my brother i will read about it , i try to understand the source code of GUI thats why i write it manully but you are right i will use SwingWorker this time ,THANKS :) – Medo Fsociety Apr 12 '17 at 14:25
  • No problem. Trigger your SwingWorker in ActionListener :) – Francisunoxx Apr 12 '17 at 15:03
  • I can't see how a `SwingWorker` is needed to solve this issue, as the `ActionListener` would be triggered within the context of the EDT by the button - In any case, it won't solve this issue because `l` is neither initialized or add to the UI, so the OP will be suffering from a `NullPointerException` – MadProgrammer Apr 12 '17 at 23:00