当applet在浏览器上运行时,JFileChooser无法正常工作。将出现GUI,但单击“浏览”按钮时不会执行任何操作,并显示空白文件系统。当从Eclipse测试applet时,在网页之外JFileChooser工作正常。如何让JFileChooser访问本地文件?
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Scanner;
import javax.imageio.ImageIO;
import javax.print.DocFlavor.BYTE_ARRAY;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Test extends javax.swing.JApplet{
JFrame frame = new JFrame();// frame
JTextField textField_path = new JTextField();// browse path
JButton btnBrowse = new JButton("Browse");
JLabel Jlabel_image1 = new JLabel(" ");
JLabel Jlabel_image2 = new JLabel(" ");
JButton btnSave = new JButton("Save");
BufferedImage img = null;
BufferedImage image1 =null;
BufferedImage capture = null;
// function to rescale the image to fit in the frame
private Image ScaledImage(BufferedImage image, int w,int h){
BufferedImage resizeimage = new BufferedImage(w,h,BufferedImage.TYPE_INT_BGR);
Graphics2D g2= resizeimage.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(image,0,0,w,h,null);
g2.dispose();
return resizeimage;
}
public Test() {
// set the framesize
frame.getContentPane().setBackground(new Color(169, 169, 169));
frame.setSize(900,700);//2
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//3
frame.setLocationRelativeTo(null);
textField_path.setForeground(new Color(0, 0, 0));
textField_path.setBackground(new Color(245, 245, 220));
//set the text path for browsing
textField_path.setBounds(114, 626, 507, 23);
frame.getContentPane().setLayout(null);//intially set the path field empty
frame.getContentPane().add(textField_path);
Jlabel_image1.setBackground(SystemColor.inactiveCaptionBorder);
//set the label for original image
Jlabel_image1.setBounds(142, 29, 429, 267);
frame.getContentPane().add(Jlabel_image1);
//set the label for gray image
Jlabel_image2.setBounds(142, 345, 429, 267);
frame.getContentPane().add(Jlabel_image2);
btnBrowse.setFont(new Font("Cambria Math", Font.PLAIN, 13));
//set the BROWSE button
btnBrowse.setBounds(714, 621, 89, 30);
frame.getContentPane().add(btnBrowse);
btnSave.setFont(new Font("Cambria Math", Font.PLAIN, 13));
//set the save button
btnSave.setBounds(714, 580, 89, 30);
frame.getContentPane().add(btnSave);
// actionlistener for save button
btnSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser jFile = new JFileChooser();
jFile.showSaveDialog(null);
Path pth = jFile.getSelectedFile().toPath();
//JOptionPane.showMessageDialog(null, pth.toString());
//Graphics2D graphics2D = image1.createGraphics();
try {
ImageIO.write(img, "png", new File(pth.toString()));
} catch (IOException ox) {
// TODO: handle exception
ox.printStackTrace();
}
}
});
// actonlistener for browse button
btnBrowse.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("browse button");
JFileChooser chooser = new JFileChooser();
int figure = chooser.showOpenDialog(chooser);
if (figure == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
if(!file.exists()){
return;
}
try {
String filedetails[]= new String[2];
filedetails[0]=file.getName();
filedetails[1]=file.getAbsolutePath();
textField_path.setText(filedetails[1]);// display the path of the image browsed
img=ImageIO.read(file);
image1 =ImageIO.read(file);
for (int i = 0; i < image1.getWidth(); i++) {
for (int j = 0; j < image1.getHeight(); j++) {
Color color = new Color(image1.getRGB(i,j));
int red = color.getRed();
int green = color.getGreen();
int blue = color.getBlue();
int gray = (int)(red * 0.299 + green * 0.587 + blue * 0.114);
color = new Color(gray, gray, gray);
int rgb = color.getRGB();
image1.setRGB(i, j, rgb);
}
}
//*********** display image on jframe*********
ImageIcon grayimage = new ImageIcon(ScaledImage(image1, Jlabel_image1.getWidth(), Jlabel_image1.getHeight()));
Jlabel_image1.setIcon(grayimage);
frame.getContentPane().add(Jlabel_image1);
ImageIcon originalimage = new ImageIcon(ScaledImage(img, Jlabel_image2.getWidth(), Jlabel_image2.getHeight()));
Jlabel_image2.setIcon(originalimage);
frame.getContentPane().add(Jlabel_image2);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
});
frame.setVisible(true);
}
public static void main(String[] args) {
Test jb = new Test();
}
}