I'm working on a java program in netbeans and I have it working except to run it I have to right click the main file and tell it to run instead of just clicking the run button.

Any help would be appreciated.

Sincerely yours;

jdm

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package FirstGUI;

import java.awt.Color;
import java.awt.Container;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

 class FirstGui extends JPanel implements ActionListener {

  private JButton yellowButton = new JButton("Yellow");

  private JButton blueButton = new JButton("Blue");

  private JButton redButton = new JButton("Red");

  private JButton greenButton = new JButton("Green");

  public FirstGui() {
    add(redButton);
    add(greenButton);
    add(blueButton);
    add(yellowButton);

    yellowButton.addActionListener(this);
    blueButton.addActionListener(this);
    redButton.addActionListener(this);
    greenButton.addActionListener(this);
  }

  public void actionPerformed(ActionEvent evt) {
    Object source = evt.getSource();
    Color color = getBackground();

    if (source == yellowButton)
      color = Color.yellow;
    else if (source == blueButton)
      color = Color.blue;
    else if (source == redButton)
      color = Color.red;
    else if (source == greenButton)
        color = Color.green;

    setBackground(color);
    repaint();
  }

  public static void main(String[] args) {
    JFrame frame = new JFrame("Color Changing Demo");
    frame.setSize(500, 200);
    frame.setLocation(100,300);
    frame.setResizable(false);

    frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    }
    );

    Container contentPane = frame.getContentPane();
    contentPane.setBackground(Color.white);
    contentPane.add(new FirstGui());

    frame.show();
  }
}

Set the main class in the project properties/preferences.

I think I did that. Here is the error code i get:

run:
Exception in thread "main" java.lang.NoClassDefFoundError: firstgui/Main
Caused by: java.lang.ClassNotFoundException: firstgui.Main
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

Java is case-sensitive.

I have the project named the same as the package. I'm confused.

FirstGUI is not the same as firstgui .

Sorry, here is the version that I fixed that in, but it still is the same.

//package
package firstgui;

//libraries
import java.awt.Color;
import java.awt.Container;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

//declaring class and telling it to use JPanel and ActionListener
 class firstgui extends JPanel implements ActionListener 
{

  //declaring buttons
  private JButton yellowButton = new JButton("Yellow");

  private JButton blueButton = new JButton("Blue");

  private JButton redButton = new JButton("Red");

  private JButton greenButton = new JButton("Green");

	 //declaring firstgui
	 public firstgui() 
	 {
	
		 //telling the program to add the buttons
		 add(redButton);
		 add(greenButton);
		 add(blueButton);
		 add(yellowButton);

		 //telling the buttons to listen for an event
		 yellowButton.addActionListener(this);
		 blueButton.addActionListener(this);
		 redButton.addActionListener(this);
		 greenButton.addActionListener(this);
	 }

  //declaring the event
  public void actionPerformed(ActionEvent evt) 
	{
		//telling the prgoram to get the color name of the button
		Object source = evt.getSource();
		Color color = getBackground();

		//if loop to change the background color
		if (source == yellowButton)
			color = Color.yellow;
		
		else if (source == blueButton)
			color = Color.blue;
		
		else if (source == redButton)
			color = Color.red;
		
		else if (source == greenButton)
			color = Color.green;

		//change the background color
		setBackground(color);
		repaint();
  }

   //main
    public static void main(String[] args) 
	{
		//declaring the JFrame
		JFrame frame = new JFrame("Color Changing Demo");
		frame.setSize(500, 200);
		frame.setLocation(100,300);
		frame.setResizable(false);

		//close
		//frame.addWindowListener(new WindowAdapter() {
	//public void windowClosing(WindowEvent e)
	//{
       // System.exit(0);
	//}
   // }
   // );

		//getting the content of the contentPane
		Container contentPane = frame.getContentPane();
		
		//set default background color to white
		contentPane.setBackground(Color.white);
		
		//add first gui
		contentPane.add(new firstgui());
		
		//show frame
		frame.show();
  }

    class WindowTerminator extends WindowAdapter {

   public void windowClosing (WindowEvent e)  {
      System.exit(0);
   }
}
}

and here is the error message:


run:
Exception in thread "main" java.lang.NoClassDefFoundError: firstgui/Main
Caused by: java.lang.ClassNotFoundException: firstgui.Main
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

Because he configured it that way, of course. ;-)

@OP P.S. if your "main" class is called "firstgui" your properties/preferences should designate "firstgui" not Main.

Because he configured it that way, of course. ;-)

@OP P.S. if your "main" class is called "firstgui" your properties/preferences should designate "firstgui" not Main.

Sorry about that, I finally figured out what you were trying to tell me. I'm new to netbeans and java so I was thinking it had something to do with the code.

Thanks for putting up with me and for all of the help.

I really appreciate it.

Sincerely yours;

jdm

This article has been dead for over six months. Start a new discussion instead.