java applet源代码中怎么只有applet类的声明,如:
public class Applet1 extends Applet {...}
这个类的实例在什么地方呢,比如Applet1 app=new Applet1();为什么没有呢?
public class Applet1 extends Applet {...}
这个类的实例在什么地方呢,比如Applet1 app=new Applet1();为什么没有呢?
使用jbuilder在界面上添加jPanel,然后创建一个线程在jPanel上画线.
我先写了一点代码如下,就是一个简单的创建线程,然后循环画线,希望你帮我看看,如果问题解决了我再给100分,多谢了!
package draw;import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.util.Timer;
import java.util.TimerTask;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2006</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
class ClassMyPanel extends JPanel{
// ClassMyPanel (int x1,int y1, int x2, int y2);
int times=1;}class MyThread implements Runnable {AppletDraw app=new AppletDraw();
Graphics g = app.myPanel.getGraphics(); public void run() { g.setColor(Color.red);
int j;
for (int i=0;i<=10;i++){
j=(int)java.lang.Math.round(java.lang.Math.random()*100);
// Thread.sleep(1000);// repaint();
g.drawLine(1,1,j,100);
try{Thread.sleep(1000);}catch(InterruptedException ee){}
// repaint();
}
}
}public class AppletDraw extends Applet {
private boolean isStandalone = false;
public ClassMyPanel myPanel = new ClassMyPanel();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
//Get a parameter value
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
} //Construct the applet
public AppletDraw() {
}
//Initialize the applet
public void init() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
this.setLayout(null);
myPanel.setBounds(new Rectangle(1, 1, 300 , 300));
myPanel.setDoubleBuffered(true);
jButton1.setBounds(new Rectangle(172, 243, 73, 25));
jButton1.setText("jButton1");
jButton1.addActionListener(new AppletDraw_jButton1_actionAdapter(this));
jButton2.setBounds(new Rectangle(236, 273, 73, 25));
jButton2.setText("jButton2");
this.add(jButton1, null);
this.add(myPanel, null);
this.add(jButton2, null);
}
//Get Applet information
public String getAppletInfo() {
return "Applet Information";
}
//Get parameter info
public String[][] getParameterInfo() {
return null; }
void jButton1_actionPerformed(ActionEvent e) { MyThread th= new MyThread();
th.run(); }}class AppletDraw_jButton1_actionAdapter implements java.awt.event.ActionListener {
AppletDraw adaptee; AppletDraw_jButton1_actionAdapter(AppletDraw adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.util.Timer;
import java.util.TimerTask;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2006</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
class ClassMyPanel extends JPanel{
// ClassMyPanel (int x1,int y1, int x2, int y2);
int times=1;}class MyThread extends Thread {AppletDraw app;
public MyThread(AppletDraw app1){
this.app = app1;
}
public void run(){
Graphics g = app.myPanel.getGraphics();
g.setColor(Color.red);
app.jButton2.setText("running");
int j;
for (int i=0;i<=10;i++){
j=(int)java.lang.Math.round(java.lang.Math.random()*100);
// Thread.sleep(1000);// repaint();
g.drawLine(1,1,j,100);
try{Thread.sleep(1000);}catch(InterruptedException ee){}
// repaint();
}
}
}public class AppletDraw extends Applet implements java.awt.event.ActionListener {
private boolean isStandalone = false;
public ClassMyPanel myPanel = new ClassMyPanel();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
//Get a parameter value
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
} //Construct the applet
public AppletDraw() {
}
//Initialize the applet
public void init() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
this.setLayout(null);
myPanel.setBounds(new Rectangle(1, 1, 300 , 300));
myPanel.setDoubleBuffered(true);
jButton1.setBounds(new Rectangle(172, 243, 73, 25));
jButton1.setText("jButton1");
jButton1.addActionListener(this);
jButton2.setBounds(new Rectangle(236, 273, 73, 25));
jButton2.setText("jButton2");
myPanel.add(jButton1, null);
myPanel.add(jButton2, null);
this.add(myPanel, null);
}
//Get Applet information
public String getAppletInfo() {
return "Applet Information";
}
//Get parameter info
public String[][] getParameterInfo() {
return null; }
public void actionPerformed(ActionEvent e){ MyThread th= new MyThread(this);
th.start(); }}