我写了一个小例子。Exp2.java是一个JApplet程序,上面只有一个按钮。点击后弹出一个JDialog框,上面有自己定义的JPanel。在这个JPanel中有一个标签JLabel。我想让上面的文字可以在JApplet中实现更改。可是更改时JDialog看不到变化,好象死住了一样。请教各位谁能帮俺看看代码?Exp2.java--------------------------------
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import javax.swing.tree.*;
import java.net.*;
import java.io.*;
import java.util.Vector;
import javax.swing.filechooser.FileSystemView;
public class Exp2 extends JApplet {
boolean isStandalone = false; MyJDialog dia=new MyJDialog((Frame)null,"测试",true);
JButton btnUp=new JButton("上传...");
JPanel pnl=new JPanel();
int ii; //Construct the applet
public Exp2() {
} //Initialize the applet
public void init() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
} //Component initialization
private void jbInit() throws Exception {
setBackground(Color.white);
Container c=getContentPane();
c.add(btnUp,BorderLayout.NORTH);
c.add(pnl,BorderLayout.CENTER); //上传
btnUp.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){ //显示进度窗口
dia=new MyJDialog((Frame)null,"打开窗口",true);
dia.setLocation(300,200);
dia.pack();
dia.show(); //循环处理一些事务
for(ii=0;ii<10;ii++){
//处理一些事务
System.out.println("aaa"+ii);
sendFile();
try{
Thread.sleep(1000);
}catch(Exception ex){}
} //关闭窗口
//dia.dispose();
}
});
} //Get Applet information
public String getAppletInfo() {
return "Applet Information";
} //Get parameter info
public String[][] getParameterInfo() {
return null;
} public void sendFile(){ //在打开的窗口中显示动态更新的内容
SwingUtilities.invokeLater(new Runnable(){
public void run(){
dia.setLabel1("目录:"+ii);
dia.validateSF();
dia.validate();
dia.pack();
dia.show();
}
});
} class MyJDialog extends JDialog{
ExSendFilePanel sf=new ExSendFilePanel();
Container c;
public MyJDialog(Frame f1,String title,boolean b){
super((Frame)null,title,false);
c=getContentPane();
c.add(sf,BorderLayout.CENTER);
}
public Dimension getPreferredSize(){
return new Dimension(460,210);
}
public void setLabel1(String s){
sf.setLabel1(s);
}
public void validateSF(){
sf.validate();
}
class ExSendFilePanel extends JPanel{
JLabel lbl1=new JLabel("目录:999");
public ExSendFilePanel(){
setLayout(null);
lbl1.setBounds(5,0,400,64);
add(lbl1);
}
public Dimension getPreferredSize(){
return new Dimension(460,210);
}
public void setLabel1(String s){
lbl1.setText(s);
}
}
}
}
下面是exp2.html的内容----------------------------------------------
<html>
<head>
<title>文件上传器</title>
</head>
<body>
<OBJECT classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
WIDTH = "924" HEIGHT = "600" NAME = "TestApplet" ALIGN = "top" VSPACE = "0" HSPACE = "0" codebase="http://java.sun.com/products/plugin/1.2/jinstall-12-win32.cab#Version=1,2,0,0">
<PARAM NAME = CODE VALUE = "Exp2.class" >
<PARAM NAME = CODEBASE VALUE = "." >
<PARAM NAME = NAME VALUE = "TestApplet" ><PARAM NAME="type" VALUE="application/x-java-applet;version=1.2">
<COMMENT>
<EMBED type="application/x-java-applet;version=1.2" java_CODE = "Exp2.class" java_CODEBASE = "." NAME = "TestApplet" WIDTH = "924" HEIGHT = "600" ALIGN = "top" VSPACE = "0" HSPACE = "0" pluginspage="http://java.sun.com/products/plugin/1.2/plugin-install.html"><NOEMBED></COMMENT></NOEMBED></EMBED>
</OBJECT></body>
</html>
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import javax.swing.tree.*;
import java.net.*;
import java.io.*;
import java.util.Vector;
import javax.swing.filechooser.FileSystemView;
public class Exp2 extends JApplet {
boolean isStandalone = false; MyJDialog dia=new MyJDialog((Frame)null,"测试",true);
JButton btnUp=new JButton("上传...");
JPanel pnl=new JPanel();
int ii; //Construct the applet
public Exp2() {
} //Initialize the applet
public void init() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
} //Component initialization
private void jbInit() throws Exception {
setBackground(Color.white);
Container c=getContentPane();
c.add(btnUp,BorderLayout.NORTH);
c.add(pnl,BorderLayout.CENTER); //上传
btnUp.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){ //显示进度窗口
dia=new MyJDialog((Frame)null,"打开窗口",true);
dia.setLocation(300,200);
dia.pack();
dia.show(); //循环处理一些事务
for(ii=0;ii<10;ii++){
//处理一些事务
System.out.println("aaa"+ii);
sendFile();
try{
Thread.sleep(1000);
}catch(Exception ex){}
} //关闭窗口
//dia.dispose();
}
});
} //Get Applet information
public String getAppletInfo() {
return "Applet Information";
} //Get parameter info
public String[][] getParameterInfo() {
return null;
} public void sendFile(){ //在打开的窗口中显示动态更新的内容
SwingUtilities.invokeLater(new Runnable(){
public void run(){
dia.setLabel1("目录:"+ii);
dia.validateSF();
dia.validate();
dia.pack();
dia.show();
}
});
} class MyJDialog extends JDialog{
ExSendFilePanel sf=new ExSendFilePanel();
Container c;
public MyJDialog(Frame f1,String title,boolean b){
super((Frame)null,title,false);
c=getContentPane();
c.add(sf,BorderLayout.CENTER);
}
public Dimension getPreferredSize(){
return new Dimension(460,210);
}
public void setLabel1(String s){
sf.setLabel1(s);
}
public void validateSF(){
sf.validate();
}
class ExSendFilePanel extends JPanel{
JLabel lbl1=new JLabel("目录:999");
public ExSendFilePanel(){
setLayout(null);
lbl1.setBounds(5,0,400,64);
add(lbl1);
}
public Dimension getPreferredSize(){
return new Dimension(460,210);
}
public void setLabel1(String s){
lbl1.setText(s);
}
}
}
}
下面是exp2.html的内容----------------------------------------------
<html>
<head>
<title>文件上传器</title>
</head>
<body>
<OBJECT classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
WIDTH = "924" HEIGHT = "600" NAME = "TestApplet" ALIGN = "top" VSPACE = "0" HSPACE = "0" codebase="http://java.sun.com/products/plugin/1.2/jinstall-12-win32.cab#Version=1,2,0,0">
<PARAM NAME = CODE VALUE = "Exp2.class" >
<PARAM NAME = CODEBASE VALUE = "." >
<PARAM NAME = NAME VALUE = "TestApplet" ><PARAM NAME="type" VALUE="application/x-java-applet;version=1.2">
<COMMENT>
<EMBED type="application/x-java-applet;version=1.2" java_CODE = "Exp2.class" java_CODEBASE = "." NAME = "TestApplet" WIDTH = "924" HEIGHT = "600" ALIGN = "top" VSPACE = "0" HSPACE = "0" pluginspage="http://java.sun.com/products/plugin/1.2/plugin-install.html"><NOEMBED></COMMENT></NOEMBED></EMBED>
</OBJECT></body>
</html>
解决方案 »
- 脑袋昏了,这么简单的一个问题,求助。
- JAVA/Swing编程中drawImage()不能正常显示图片。
- 如何终止多个线程
- 新人虚心请教:线程处理用什么样的数据结构合适?
- 列车售票系统,打印车票应该用什么技术好呢?
- java swing 程序在宽屏和正常屏幕的显示不一样
- 如何抛出Exception?
- 什么样的人能称之为java高手?
- 在哪儿有Effective Java这本书的电子版下?中英文都可以
- 把xx.addEventListener(this);放到类的函数中,而不是构造函数,参数该是什么?谢谢!
- treeviewer如何增加子节点
- 怎么样才能是JPanel一开始在JFrame里显示出来
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import javax.swing.tree.*;
import java.net.*;
import java.io.*;
import java.util.Vector;
import javax.swing.filechooser.FileSystemView;
public class Exp3 extends JApplet {
boolean isStandalone = false;
ExSendFilePanel sf=new ExSendFilePanel();
JButton btnUp=new JButton("上传...");
JPanel pnl=new JPanel();
int ii; //Construct the applet
public Exp3() {
} //Initialize the applet
public void init() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
} //Component initialization
private void jbInit() throws Exception {
setBackground(Color.white);
Container c=getContentPane();
c.add(btnUp,BorderLayout.NORTH);
c.add(pnl,BorderLayout.CENTER); //上传
btnUp.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
(new UpdateThread()).start(); //!!!!!!!!!most important here!
}
});
} //Get Applet information
public String getAppletInfo() {
return "Applet Information";
} //Get parameter info
public String[][] getParameterInfo() {
return null;
} public void sendFile(){
System.out.println("hello"+ii);
} class UpdateThread extends Thread{
MyJDialog dia;
Runnable start,update,finish; public UpdateThread(){
//定义起始线程
start=new Runnable(){
public void run(){
//显示进度窗口
dia=new MyJDialog((Frame)null,"打开窗口",true);
dia.setLocation(300,200);
dia.pack();
dia.show();
}
};
//定义更新线程
update=new Runnable(){
public void run(){
dia.setLabel1("目录:"+ii);
}
};
//定义结束线程
finish=new Runnable(){
public void run(){
dia.dispose();
}
};
}
public void run(){
//循环处理一些事务
SwingUtilities.invokeLater(start);
for(ii=0;ii<10;ii++){
SwingUtilities.invokeLater(update);
//模拟事务处理
sendFile();
try{
Thread.sleep(1000);
}catch(Exception ex){}
}
SwingUtilities.invokeLater(finish);
}
} class MyJDialog extends JDialog{
Container c;
public MyJDialog(Frame f1,String title,boolean b){
super((Frame)null,title,false);
c=getContentPane();
c.add(sf,BorderLayout.CENTER);
}
public Dimension getPreferredSize(){
return new Dimension(460,210);
}
public void setLabel1(String s){
sf.setLabel1(s);
}
public void validateSF(){
sf.validate();
}
}
class ExSendFilePanel extends JPanel{
JLabel lbl1=new JLabel("目录:999");
public ExSendFilePanel(){
setLayout(null);
lbl1.setBounds(5,0,400,64);
add(lbl1);
}
public Dimension getPreferredSize(){
return new Dimension(460,210);
}
public void setLabel1(String s){
lbl1.setText(s);
}
}}