大家好:我下面这段代码是一个JAVA APPLET ,点击"DownLoad"按钮后,会下载远程服务器的图片,我想在下载的时候在窗体上显示这样一个格式"1 of 3 photos downlading...",可是我这样写怎么显示不出来,是 repaint() 没起作用吗,应该怎么写啊?
package com.java.test;import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class DownPhoto3 extends Applet implements ActionListener{
String sTxt ;
String paths[];
String pathss = "";
private JFrame f;
private JPanel p,p2,p3;
private JButton bl,bd,bc;
private JTextField tx ,tx3;
File file;
/*public static void main(String args[]){
DownPhoto3 pd = new DownPhoto3();
pd.DownFrame();
System.out.print("hello");
}*/
public void init(){
this.DownFrame();
}
public void DownFrame(){
f = new JFrame("Photo DownLoad");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setBackground(Color.gray);
p = new JPanel();
f.setBounds(400, 400, 500, 200);
f.setResizable(false);
f.add(p,"North");
tx = new JTextField(30);
tx.setEnabled(false);
p.add(tx);
bl = new JButton("Browse");
bl.addActionListener(this); p.add(bl);
p2 = new JPanel();
f.add(p2,"South");
bd = new JButton("DownLoad");
bc = new JButton("Cancel");
bd.addActionListener(this);
bc.addActionListener(this);
p2.add(bd);
p2.add(bc);
p3 = new JPanel();
f.add(p3,"Center");
tx3 = new JTextField(20);
//tx3.setText(this.getSTxt());
//tx3.repaint();
tx3.setBackground(Color.pink);
p3.add(tx3);
//p3.repaint();
p3.setVisible(false);
//f.repaint();
f.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String a[] = {"http://hiphotos.baidu.com/baiweilu521/pic/item/2a8f411f84a6b3ee1ad57642.jpg","http://qhdcyts.tripc.com/thumb_scenic_images/5/200581711255331.jpg","http://www.59766.com/peruser/upic/200710/1/11610570.jpeg"};
this.setPaths(a);
//String []paths = pathss.split("|");
if(e.getSource()==bl)
{
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("C:\\Documents and Settings\\Administrator\\Desktop"));
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setAcceptAllFileFilterUsed(false);
if (chooser.showOpenDialog(f) == JFileChooser.APPROVE_OPTION) {
System.out.println("getCurrentDirectory(): "
+ chooser.getCurrentDirectory());
System.out.println("getSelectedFile() : "
+ chooser.getSelectedFile());
}
else {
System.out.println("No Selection ");
}
file = chooser.getSelectedFile();//getCurrentDirectory();//.getSelectedFile();
tx.setText(file.getPath());
}
else if(e.getSource()==bd)
{
String folderPath = tx.getText().trim();
File f3 = new File(folderPath);
if(!f3.exists())
{
JOptionPane.showMessageDialog(null, "You choose the folder does not exist", "Warning", JOptionPane.ERROR_MESSAGE );
}
else{
OutputStream out = null;
URLConnection conn = null;
InputStream in = null;
p3.setVisible(true);
//p3.repaint();
String s1 ="";
String s2 ="";
//tx3.repaint();
f.repaint();
String path ;
s2 = Integer.toString(paths.length);
String s = s1 + " of " + s2 +" photos downlading...";
//tx3.setText(s);
System.out.println(s2);
for(int i=0;i<paths.length;i++ )
{
s1 = Integer.toString(i+1);
s = s1 + " of " + s2 +" photos downlading...";
//this.setSTxt(s);
f.repaint();
System.out.println(s1) ;
System.out.println(s) ;
p3.repaint();
p3.setVisible(true);
tx3.setText(s);
//tx3.repaint();
try {
path = paths[i];
URL urlPath = new URL(path);
String filePath = urlPath.toString();
int j = filePath.lastIndexOf("/");
String fileName = filePath.substring(j+1, filePath.length());
//String photoPath = folderPath + fileName;
//File f1 = new File(folderPath);
File file = new File(folderPath ,fileName);
out = new BufferedOutputStream(
new FileOutputStream(file));
conn = urlPath.openConnection();
in = conn.getInputStream();
byte[] buffer = new byte[1024];
int numRead;
long numWritten = 0;
while ((numRead = in.read(buffer)) != -1) {
out.write(buffer, 0, numRead);
numWritten += numRead;
}
}catch (Exception e1) {
e1.printStackTrace();
} finally
{
try {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
} catch (IOException ioe) {
} }
int b;
if(i==paths.length-1)
{
JOptionPane.showMessageDialog(null, "Download images is completed!", "Message", JOptionPane.INFORMATION_MESSAGE );
//System.exit(0);
//tx3.setText("The download is completed!");
//tx3.setText(s);
}
f.repaint();
}
System.exit(0);
//JOptionPane.showMessageDialog(null, "Download images is completed!", "Message", JOptionPane.INFORMATION_MESSAGE );
}
f.repaint();
}
else if(e.getSource()==bc)
{
System.exit(0);
}
}
public String[] getPaths() {
return paths;
}
public void setPaths(String[] paths) {
this.paths = paths;
} public String getPathss() {
return pathss;
} public void setPathss(String pathss) {
this.pathss = pathss;
}
public String getSTxt() {
return sTxt;
}
public void setSTxt(String txt) {
sTxt = txt;
}
}
package com.java.test;import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class DownPhoto3 extends Applet implements ActionListener{
String sTxt ;
String paths[];
String pathss = "";
private JFrame f;
private JPanel p,p2,p3;
private JButton bl,bd,bc;
private JTextField tx ,tx3;
File file;
/*public static void main(String args[]){
DownPhoto3 pd = new DownPhoto3();
pd.DownFrame();
System.out.print("hello");
}*/
public void init(){
this.DownFrame();
}
public void DownFrame(){
f = new JFrame("Photo DownLoad");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setBackground(Color.gray);
p = new JPanel();
f.setBounds(400, 400, 500, 200);
f.setResizable(false);
f.add(p,"North");
tx = new JTextField(30);
tx.setEnabled(false);
p.add(tx);
bl = new JButton("Browse");
bl.addActionListener(this); p.add(bl);
p2 = new JPanel();
f.add(p2,"South");
bd = new JButton("DownLoad");
bc = new JButton("Cancel");
bd.addActionListener(this);
bc.addActionListener(this);
p2.add(bd);
p2.add(bc);
p3 = new JPanel();
f.add(p3,"Center");
tx3 = new JTextField(20);
//tx3.setText(this.getSTxt());
//tx3.repaint();
tx3.setBackground(Color.pink);
p3.add(tx3);
//p3.repaint();
p3.setVisible(false);
//f.repaint();
f.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String a[] = {"http://hiphotos.baidu.com/baiweilu521/pic/item/2a8f411f84a6b3ee1ad57642.jpg","http://qhdcyts.tripc.com/thumb_scenic_images/5/200581711255331.jpg","http://www.59766.com/peruser/upic/200710/1/11610570.jpeg"};
this.setPaths(a);
//String []paths = pathss.split("|");
if(e.getSource()==bl)
{
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("C:\\Documents and Settings\\Administrator\\Desktop"));
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setAcceptAllFileFilterUsed(false);
if (chooser.showOpenDialog(f) == JFileChooser.APPROVE_OPTION) {
System.out.println("getCurrentDirectory(): "
+ chooser.getCurrentDirectory());
System.out.println("getSelectedFile() : "
+ chooser.getSelectedFile());
}
else {
System.out.println("No Selection ");
}
file = chooser.getSelectedFile();//getCurrentDirectory();//.getSelectedFile();
tx.setText(file.getPath());
}
else if(e.getSource()==bd)
{
String folderPath = tx.getText().trim();
File f3 = new File(folderPath);
if(!f3.exists())
{
JOptionPane.showMessageDialog(null, "You choose the folder does not exist", "Warning", JOptionPane.ERROR_MESSAGE );
}
else{
OutputStream out = null;
URLConnection conn = null;
InputStream in = null;
p3.setVisible(true);
//p3.repaint();
String s1 ="";
String s2 ="";
//tx3.repaint();
f.repaint();
String path ;
s2 = Integer.toString(paths.length);
String s = s1 + " of " + s2 +" photos downlading...";
//tx3.setText(s);
System.out.println(s2);
for(int i=0;i<paths.length;i++ )
{
s1 = Integer.toString(i+1);
s = s1 + " of " + s2 +" photos downlading...";
//this.setSTxt(s);
f.repaint();
System.out.println(s1) ;
System.out.println(s) ;
p3.repaint();
p3.setVisible(true);
tx3.setText(s);
//tx3.repaint();
try {
path = paths[i];
URL urlPath = new URL(path);
String filePath = urlPath.toString();
int j = filePath.lastIndexOf("/");
String fileName = filePath.substring(j+1, filePath.length());
//String photoPath = folderPath + fileName;
//File f1 = new File(folderPath);
File file = new File(folderPath ,fileName);
out = new BufferedOutputStream(
new FileOutputStream(file));
conn = urlPath.openConnection();
in = conn.getInputStream();
byte[] buffer = new byte[1024];
int numRead;
long numWritten = 0;
while ((numRead = in.read(buffer)) != -1) {
out.write(buffer, 0, numRead);
numWritten += numRead;
}
}catch (Exception e1) {
e1.printStackTrace();
} finally
{
try {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
} catch (IOException ioe) {
} }
int b;
if(i==paths.length-1)
{
JOptionPane.showMessageDialog(null, "Download images is completed!", "Message", JOptionPane.INFORMATION_MESSAGE );
//System.exit(0);
//tx3.setText("The download is completed!");
//tx3.setText(s);
}
f.repaint();
}
System.exit(0);
//JOptionPane.showMessageDialog(null, "Download images is completed!", "Message", JOptionPane.INFORMATION_MESSAGE );
}
f.repaint();
}
else if(e.getSource()==bc)
{
System.exit(0);
}
}
public String[] getPaths() {
return paths;
}
public void setPaths(String[] paths) {
this.paths = paths;
} public String getPathss() {
return pathss;
} public void setPathss(String pathss) {
this.pathss = pathss;
}
public String getSTxt() {
return sTxt;
}
public void setSTxt(String txt) {
sTxt = txt;
}
}
clearRect(int x, int y, int width, int height)
方法,你可以到帮助文件里再确认下。恩我试过的,可以的。
而repaint()为什么不起作用,我也有疑问呢。
希望能帮到你。