-1

I got a little JLabel Counter Program here which basically just should add a Number to a Counter when i press "+1" but when i try to run it it say that it isnt able to find the Main Class in my tCounter.ButtonAction class here's my Code so far hope you can help me.

    package tCounter;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

public class ButtonAction {
 static int Zahl = 0;
    public static void createAndShowGUI()  {

        JFrame frame1 = new JFrame("JAVA");
        frameg1.setText(String.valueOf(Zahl));
        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            JButton button = new JButton("+1");
        //Add action listener to button
        button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e)
            {
                //Execute when button is pressed
                //System.out.println("You clicked the button");
                Zahl = Zahl +1;
               String Penis = Integer.toString(Zahl);
                System.out.println(Zahl);
            }
        });      

        frame1.getContentPane().add(button);
        frame1.pack();
        frame1.setVisible(true);
    }

    private static class frameg1 {

        private static void setText(String valueOf) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        public frameg1() {
        }
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Did you follow the suggestion from your last posting (http://stackoverflow.com/questions/19687480/make-a-simple-java-optionpane-counter). The structure of your program is not the same as the ButtonDemo. It shows you how to structure the code so that the GUI is created on the EDT. Also, if you would follow that structure of that progame then you could define instance variables that are easily accessible from your ActionListener. Also, you should be accepting answers when people help you. – camickr Oct 30 '13 at 20:14
  • 1
    Another wild guess, one that names his/her String variables "Penis"... needs to learn to follow java conventions,.. that should start with a lowercase... "penis". – ThePerson Oct 30 '13 at 20:18
  • Actually i typed in anything just to try if it works i deleted it afterwards cause it didnt help and i forgot to delete that part ;) – Niki Iwan Oct 30 '13 at 20:27
  • And secondly i just started with Java so TRIED to follow your answer and if i add a main class it tells me "illegal start of expression" and ill say sorry for a future mistakes ill make... – Niki Iwan Oct 30 '13 at 20:28
  • So could one of you explain me what i have to change for dummies now so i can maybe learn from it, Thanks – Niki Iwan Oct 30 '13 at 20:29
  • You were given an explanation 4 hours ago. All you need to do is read the tutorial, download the example demo program and make changes to the working program. Its that simple. The example code shows you how to define a main() method and how to create a GUI with components. – camickr Oct 30 '13 at 20:34
  • Thank you very much i was busy after you answerd me first so i actually didnt read the turorial carfully thanks for your patience with me ill read through everything and start a new thread if i'm still having problems.... – Niki Iwan Oct 30 '13 at 20:36

2 Answers2

1

Every Java application requires at least one class that has a public static void main(String args[]) {...} method, which acts as the main entry point for the application.

Take a closer look at the Java Tutorials for more details

Swing also has some special requirements for intialising Swing applictions...

public static void main(String args[]) {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                ex.printStackTrace();
            }

            ButtonAction.createAndShowGUI();
        }
    });
}

See Initial Threads for more details

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
-2

Every java program needs to have a main method. This is the starting point of where the application will start working from. It should be defined as :

public static void main(String arg[]){

}

I think for yours, you want to add this method something like this:

public static void main(String arg[]){
    ButtonAction ba = new ButtonAction();
    ba.createAndShowGUI();
}

What you are doing here is saying, when the application starts, you want to create a ButtonAction object, and you want to call the createAndShowGUI() method on that object which starts everything running.

However, you shouldn't have variables named "penis", so I feel I have probably wasted my time responding here.

ThePerson
  • 3,048
  • 8
  • 43
  • 69
  • -1 code to create a GUI should execute on the Event Dispatch Thread (EDT) as the tutorials demonstrate. That is why I have referred the OP to the tutorial (twice). – camickr Oct 30 '13 at 20:24
  • This looks like a piece of coursework, I would imagine if the OP doesn't know to use a main, it may not be the best time to learn about threads. – ThePerson Oct 30 '13 at 20:49