我有两个java类。一个是显示applet方法的JFrame。我想要在运行Booth applet时更改TollboothDisplay java。这是两个程序。 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;
}
}
答案 0 :(得分:-1)
结束只是制作一个Applet。这样的方式要容易得多。 这是
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();
}
}