0

I've assembled a basic GUI but the debugger says there is no main method even though there is a static void main(String[] args) here is my code

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

public class GUI implements ActionListener {

    static void main(String[] args) {
        new GUI();
    }

    public GUI() {
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        JButton button = new JButton(String.valueOf(Game.Compounds));
        button.addActionListener(this);

        panel.setBorder(BorderFactory.createEmptyBorder(30, 30, 10, 30));
        panel.setLayout(new GridLayout(0, 1));
        panel.add(button);

        frame.add(panel, BorderLayout.CENTER);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("GUI");
        frame.pack();
        frame.setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        Game.Compounds = Game.Compounds + Game.CPC;
    }
}

What is wrong with it

3 Answers3

1

You have missed the "public" modifier. The signature of main method is: public static void main(String [] args)

Steven
  • 86
  • 8
0

java always search the main function to start the application.

but you did a little mistake, main should be public, so change the declaration like this public static void main(String[] args)

modify your code like below:

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

public class GUI implements ActionListener {

   public static void main(String[] args) {
        new GUI();
    }

    public GUI() {
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        JButton button = new JButton(String.valueOf(Game.Compounds));
        button.addActionListener(this);

        panel.setBorder(BorderFactory.createEmptyBorder(30, 30, 10, 30));
        panel.setLayout(new GridLayout(0, 1));
        panel.add(button);

        frame.add(panel, BorderLayout.CENTER);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("GUI");
        frame.pack();
        frame.setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        Game.Compounds = Game.Compounds + Game.CPC;
    }
}
Mahamudul Hasan
  • 2,745
  • 2
  • 17
  • 26
0

But if you don't specify anything it should be public by def

Akila
  • 1