0

I have two java classes. One is a JFrame that displays the methods from the applet. I want the TollboothDisplay java to change whenever the Booth applet is ran. Here are both of the programs. TollboothDisplay.java

    package Booth;
/**
 * Write a description of class TollboothDisplay here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
@SuppressWarnings("serial")
public class TollboothDisplay extends JFrame
{
private JLabel count;
private JLabel money;
private Booth myBooth;
    /**
     * Constructor for objects of class TollboothDisplay
     */
    public TollboothDisplay()
    {
        myBooth = new Booth();
        count = new JLabel("Number of Cars: " + myBooth.traffic());
        money = new JLabel("Amount Collected: $" + myBooth.profit());
        getContentPane().add(count,BorderLayout.NORTH);
        getContentPane().add(money,BorderLayout.SOUTH);
        addMouseListener(new click());
        myBooth.add(myBooth);
    }
    public void update()
    {
        count.setText("Number of Cars: " + myBooth.traffic());
        money.setText("Amount Collected: $" + myBooth.profit());
        repaint();
    }
    public void paint(Graphics g)
    {
        paintComponents(g);   
    }
    @SuppressWarnings("deprecation")
    public static void main(String args[])
    {
        TollboothDisplay myDisplay = new TollboothDisplay();
        myDisplay.setSize(200,200);
        myDisplay.show();
        myDisplay.setTitle("Tollbooth Version 1");
    }
    private class click implements MouseListener
    {
        public void mousePressed(MouseEvent e){}
        public void mouseReleased(MouseEvent e){}
        public void mouseEntered(MouseEvent e){}
        public void mouseExited(MouseEvent e){}
        public void mouseClicked(MouseEvent e){
        myBooth.addCar();
        update();}
    }
}

Booth.java

    package Booth;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
 * Class Booth - write a description of the class here
 * 
 * @author (your name) 
 * @version (a version number)
 */
@SuppressWarnings({ "serial", "unused" })
public class Booth extends Applet implements ActionListener
{
    //Panel panel;
    Label label;
    Button b;
    int countCars;
   public void init()
   {
       //panel= new Panel();
       b=new Button("Add Car");
       label = new Label();
       //add(panel);
       add(b);
       add(label);
       b.addActionListener(this);
       countCars = 0;
    }
    public void actionPerformed(ActionEvent e)
    {   countCars++;
        label.setText(""+countCars);
        validate();
        invalidate();
    }
    public int traffic() {
        return countCars;
    }
    public double profit() {
        double price = countCars * 3.25;
        return price;
    }
    public int addCar() {
        return countCars;
    }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Why the bad design? Instead, probably better is to make up your mind: is this supposed to be an applet or a stand-alone application? Whichever is chosen, scrap the old code and re-design from the ground up to be the best applet or stand-alone program possible. If you use decent MVC principles, then you can use the core of your code for either or both without any kludges whatsoever. Also please provide more details about your code and your problem. Your problem description is lacking in these details that would help us understand what you're really trying to do. – Hovercraft Full Of Eels Feb 16 '15 at 18:10
  • It is for a school project. I agree. I hate the format, so I'll probably just make it all in an applet. It would make more sense. – user3776741 Feb 16 '15 at 18:12
  • Regardless, 1) your GUI coding should be to the JPanel, not to the top level window. Then you can use the GUI portion of your code in a JApplet (not an Applet, please), or a JFrame, and again your model code, the logical underpinnings should be separate from all. 2) Don't combine Swing with AWT components. Stick with all Swing. – Hovercraft Full Of Eels Feb 16 '15 at 18:14
  • Let me know if I can clean it up in any way. – user3776741 Feb 16 '15 at 18:46
  • Possible [duplicate](http://stackoverflow.com/q/12449889/230513). – trashgod Feb 17 '15 at 00:23

1 Answers1

-1

Ended up just making an Applet. It was just a lot easier that way. Here it is

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
@SuppressWarnings({ "serial", "unused" })
public class Booth extends Applet implements ActionListener
{
    //Panel panel;
    Label displayCars;
    Label profit;
    Label traffic;
    Label total;
    Button a;
    Button m;
    int countCars;
    int cars;
   public void init()
   {
       //panel= new Panel();
       a = new Button("Cars in line");
       m = new Button("Car passes through");
       displayCars = new Label();
       profit = new Label();
       traffic = new Label();
       total = new Label();
       //add(panel);
       add(a);
       add(m);
       add(displayCars);
       add(profit);
       add(traffic);
       add(total);
       a.addActionListener(this);
       m.addActionListener(this);
       countCars = 0;
       cars = 0;
    }
    public void actionPerformed(ActionEvent e)
    { 
        if (e.getSource() == a)
        {
        countCars++;
        cars++;
        }
        else if (e.getSource() == m)
        {
        countCars--;
        }
        displayCars.setText(" "+countCars);
        profit.setText("Estimated Profit: $" + cars * 3.25);
        traffic.setText("Cars Left in line: " + countCars);
        total.setText("Total Cars: " + cars);
        validate();
        invalidate();
    }
}