5 Replies - 311 Views - Last Post: 09 February 2018 - 03:40 PM Rate Topic: -----

#1 bman8932  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 24-March 17

Java Main Class Help

Posted 08 February 2018 - 12:52 PM

Whenever I try to run this project it says "no main classes found." If I just run the .java file by itself, it runs fine. I can't figure out how to implement a main class so I can run the project...

Any help would be great. Thanks.

//DrinkMachine.java
import java.awt.Graphics;
import javax.swing.JApplet;
import javax.swing.JOptionPane;
import java.awt.*;
import javax.swing.*;
import java.applet.*;
import java.awt.event.*;
public class DrinkMachine extends JApplet
{
private double balance;
private int colaClicks = 0,lemonclicks=0,grapeClicks=0,rootClicks=0,waterClicks=0;
private JTextField money;
private JButton cola;
private JButton lemmon;
private JButton grape;
private JButton root;
private JButton water;
private JPanel buildButtons,moneyText;
public void init()
{
setLayout(new BorderLayout());
buildButtons();
add(buildButtons, BorderLayout.EAST);
JLabel title=new JLabel("Soft Drinks .75 Each");
title.setFont(new Font("Calibri",Font.BOLD,25));
add(title,BorderLayout.WEST);
add(moneyText,BorderLayout.SOUTH);
}
private void buildButtons()
{
moneyText=new JPanel();
moneyText.setLayout(new FlowLayout());
money = new JTextField(15);
money.setPreferredSize(money.getPreferredSize());
money.setText("0.00");
moneyText.add(new JLabel("Enter amount tendered: "));
moneyText.add(money);
buildButtons = new JPanel();
buildButtons.setLayout(new GridLayout (5,1));
cola = new JButton("Cola");
lemmon = new JButton("Lemmon-Lime soda");
grape = new JButton("Grape Soda");
root = new JButton("Root beer");
water = new JButton("bottle water");
cola.addActionListener(new ButtonHandler());
lemmon.addActionListener(new ButtonHandler());
grape.addActionListener(new ButtonHandler());
root.addActionListener(new ButtonHandler());
water.addActionListener(new ButtonHandler());
buildButtons.add(cola);
buildButtons.add(lemmon);
buildButtons.add(grape);
buildButtons.add(root);
buildButtons.add(water);
}
private class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
double amount = Double.parseDouble(money.getText());
if(amount < 0.75)
JOptionPane.showMessageDialog(null, "Inadequate amount of money ");
else
{
if(e.getSource() == cola )
{
colaClicks++;
if(colaClicks <= 20)
{
balance = (amount - 0.75);
JOptionPane.showMessageDialog(null, colaClicks + " can of cola dispenced "+"\n"+"$"+ balance + " in change given back.");
}
else
JOptionPane.showMessageDialog(null, "Inadequate amount of money ");
}
if(e.getSource() == lemmon)
{
lemonclicks++;
if(lemonclicks <= 20)
{
balance = (amount - 0.75);
JOptionPane.showMessageDialog(null, lemonclicks + " can of lemon dispenced "+"\n"+"$"+ balance + " in change given back.");
}
else JOptionPane.showMessageDialog(null, "Inadequate amount of money ");
}
if(e.getSource() == grape )
{
grapeClicks++;
if(grapeClicks <= 20)
{
balance = (amount - 0.75);
JOptionPane.showMessageDialog(null, grapeClicks + " can of grape dispenced "+"\n"+"$"+ balance + " in change given back.");
}
else JOptionPane.showMessageDialog(null, "Inadequate amount of money ");
}
if(e.getSource() ==root )
{
rootClicks++;
if(rootClicks <= 20)
{
balance = (amount - 0.75);
JOptionPane.showMessageDialog(null, rootClicks + " can of root beer dispenced "+"\n"+"$"+ balance + " in change given back.");
}
else JOptionPane.showMessageDialog(null, "Inadequate amount of money ");
}

if(e.getSource() == water )
{
waterClicks++;
if(waterClicks <= 20)
{
balance = (amount - 0.75);
JOptionPane.showMessageDialog(null, waterClicks + " can of water dispenced "+"\n"+"$"+ balance + " in change given back.");
}
else JOptionPane.showMessageDialog(null, "Inadequate amount of money ");
}
money.setText(""+balance);
}

}//end action performed e

}//end private class button handler

}//end public class drinkmachine



Is This A Good Question/Topic? 0
  • +

Replies To: Java Main Class Help

#2 Martyr2  Icon User is online

  • Programming Theoretician
  • member icon

Reputation: 5147
  • View blog
  • Posts: 13,835
  • Joined: 18-April 07

Re: Java Main Class Help

Posted 08 February 2018 - 01:13 PM

Make sure that you are specifying the path correctly in your "code" attribute of the applet tag in your HTML page. It is typically in relation to where the HTML file is. For instance, if you have your HTML page in a directory called /home and your class in a directory called /home/machines/drinkmachine then you are going to specify
code=machines.drinkmachine.DrinkMachine.class
.

Notice here how the directory structure is included. But also notice we don't specify /home because in relation to your HTML document, it is in the sub folders /machine/drinkmachine

Hope I am making myself clear. :)
Was This Post Helpful? 0
  • +
  • -

#3 NormR  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 660
  • View blog
  • Posts: 4,969
  • Joined: 25-December 13

Re: Java Main Class Help

Posted 08 February 2018 - 01:26 PM

The posted class is defined as an applet. They are run from an html page with an applet tag loaded in an old browser(New ones don't support applets) or with the appletviewer.

There is no main method.

This post has been edited by NormR: 08 February 2018 - 02:28 PM

Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3503
  • View blog
  • Posts: 15,837
  • Joined: 20-September 08

Re: Java Main Class Help

Posted 08 February 2018 - 02:18 PM

Your code tag should have no dots in it. Why? Because it has 'no' package. So the correct attribute would be

code="DrinkMachine"

(.class is not used - it's a class name, not a file - though it does work with)
Was This Post Helpful? 0
  • +
  • -

#5 ndc85430  Icon User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 703
  • View blog
  • Posts: 2,873
  • Joined: 13-June 14

Re: Java Main Class Help

Posted 08 February 2018 - 10:25 PM

It surprises me that you're writing an applet. I thought they weren't used any more!
Was This Post Helpful? 0
  • +
  • -

#6 bman8932  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 24-March 17

Re: Java Main Class Help

Posted 09 February 2018 - 03:40 PM

View PostMartyr2, on 08 February 2018 - 01:13 PM, said:

Make sure that you are specifying the path correctly in your "code" attribute of the applet tag in your HTML page. It is typically in relation to where the HTML file is. For instance, if you have your HTML page in a directory called /home and your class in a directory called /home/machines/drinkmachine then you are going to specify
code=machines.drinkmachine.DrinkMachine.class
.

Notice here how the directory structure is included. But also notice we don't specify /home because in relation to your HTML document, it is in the sub folders /machine/drinkmachine

Hope I am making myself clear. :)/>


Thanks for your help.

View Postg00se, on 08 February 2018 - 02:18 PM, said:

Your code tag should have no dots in it. Why? Because it has 'no' package. So the correct attribute would be

code="DrinkMachine"

(.class is not used - it's a class name, not a file - though it does work with)


View PostNormR, on 08 February 2018 - 01:26 PM, said:

The posted class is defined as an applet. They are run from an html page with an applet tag loaded in an old browser(New ones don't support applets) or with the appletviewer.

There is no main method.


Thanks for your help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1