-1

Like I said I am new to code, and I now am starting to learn how to make gui's in java here is my code

import java.awt.*;
import java.awt.event.*;




public class gui implements ActionListener{

    private int count = 0;
    private JLabel fat;
    private JButton button;
    private JLabel label;

        
    public gui(){
        JFrame frame = new JFrame();
        button = new JButton("yes");
            button.addActionListener(this);

        fat = new JLabel("number of fatties: 0");
        
        label = new JLabel("are you fat?");
        
        JPanel panel = new JPanel();
        panel.setBorder(BorderFactory.createEmptyBorder(100, 100, 100, 100));
        panel.setLayout(new GridLayout(0, 1));
        panel.add(label);
        panel.add(button);
        panel.add(fat);


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

    }

    public static void main(String[] args) {
        new gui();
        
    }
    
    @Override
    public void actionPerformed(ActionEvent a) {
        count++;
        fat.setText("number of fatties: " + count);
        if (count == 100){
            JFrame frame = new JFrame();
            
            JPanel panel = new JPanel();
            panel.setBorder(BorderFactory.createEmptyBorder(100, 100, 100, 100));
            panel.setLayout(new GridLayout(0, 1));
            
            
            JLabel bigfat = new JLabel("wow to be so fat to take up 100 people, you must be really fat");
            


            panel.add(bigfat);
            frame.add(panel, BorderLayout.CENTER);
            frame.setTitle("wow");
            frame.pack();
            frame.setVisible(true);

   
            

            
    
        }
        }


    }



It runs perfectly fine in visual studio code, but when I use cmd and do java gui in the folder of the .java file, it returns Error: Could not find or load main class gui so I could really use some help. I've looked it up everywhere, but I just dont understand any of it, so if yall could dumb it down for me that would be greatly appreciated

  • Are you compiling the code beforehand? `javac gui` and then `java gui`? – gmdev Nov 19 '20 at 03:08
  • 1
    1) Class names should start with an upper case character 2) Do you have a `Gui.class` file in the directory where you execute the `java Gui` command? – camickr Nov 19 '20 at 03:09
  • 2
    [What does “Could not find or load main class” mean?](https://stackoverflow.com/q/18093928) – Pshemo Nov 19 '20 at 03:11
  • I can't find anything that includes the Swing startup code. – NomadMaker Nov 19 '20 at 03:44
  • 1
    Welcome to Stack Overflow. It appears to me that one thing you need to learn is to search before asking. You will likely get better answers, and faster. – Ole V.V. Nov 19 '20 at 04:04
  • @gmdev whenever I do ```javac gui``` I get ```Class names, 'gui', are only accepted if annotation processing is explicitly requested 1 error``` – Dead_corpse Nov 19 '20 at 04:28
  • @Dead_corpse I apologize, I meant `javac gui.java` and then `java gui`. I forgot to add `.java` in my original comment. `javac` complies the java code and the `.java` is required when compiling. – gmdev Nov 19 '20 at 04:38
  • @gmdev ok I did that and it returns ```Gui.java:4: error: class, interface, or enum expected packagename.packagename2.packagename3.Gui ^ 1 error``` sorry this is all very new to me and like I said before my first gui, and I'm trying to get it to work so that I can send it to a friend and also I changed the class name to Gui, it didn't help – Dead_corpse Nov 19 '20 at 04:40
  • When you change the class name to something else, i.e., from `class gui` to `class Gui`, you must also change the name of the file. `gui.java` should become `Gui.java` – gmdev Nov 19 '20 at 04:53
  • @gmdev wow ok this time it returned with a giant error message I can't include it in the comment, does it have anything to do with the fact I am using CMD and not anything else? – Dead_corpse Nov 19 '20 at 04:56
  • Without seeing the actual traceback, I can't say what the issue is there. You will have to open another question. – gmdev Nov 19 '20 at 14:24

1 Answers1

-2

You have to import swing class try this code

import javax.swing.*;

import java.awt.; import java.awt.event.;

class gui implements ActionListener{

private int count = 0;
private JLabel fat;
private JButton button;
private JLabel label;


public gui(){
    JFrame frame = new JFrame();
    button = new JButton("yes");
    button.addActionListener(this);

    fat = new JLabel("number of fatties: 0");

    label = new JLabel("are you fat?");

    JPanel panel = new JPanel();
    panel.setBorder(BorderFactory.createEmptyBorder(100, 100, 100, 100));
    panel.setLayout(new GridLayout(0, 1));
    panel.add(label);
    panel.add(button);
    panel.add(fat);


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

}

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

}
@Override
public void actionPerformed(ActionEvent a) {
    count++;
    fat.setText("number of fatties: " + count);
    if (count == 100){
        JFrame frame = new JFrame();

        JPanel panel = new JPanel();
        panel.setBorder(BorderFactory.createEmptyBorder(100, 100, 100, 100));
        panel.setLayout(new GridLayout(0, 1));


        JLabel bigfat = new JLabel("wow to be so fat to take up 100 people, you must be really fat");



        panel.add(bigfat);
        frame.add(panel, BorderLayout.CENTER);
        frame.setTitle("wow");
        frame.pack();
        frame.setVisible(true);


    }
}

}