[英]Link an Applet and JFrame at the same time?
I have two java classes. 我有两个Java类。 One is a JFrame that displays the methods from the applet. 一个是JFrame,它显示小程序中的方法。 I want the TollboothDisplay java to change whenever the Booth applet is ran. 我希望每当运行Booth applet时,TollboothDisplay java都可以更改。 Here are both of the programs. 这是两个程序。 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 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;
}
}
Ended up just making an Applet. 最终只是制作了一个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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.