Java 在web浏览器上运行小程序时JFilechooser不工作
当小程序在浏览器上运行时,JFileChooser无法正常工作。GUI出现,但单击浏览按钮时,不会发生任何操作,并显示空白文件系统。从Eclipse测试小程序时,在web页面之外,JFileChooser工作正常。如何让JFileChooser访问本地文件Java 在web浏览器上运行小程序时JFilechooser不工作,java,swing,applet,jfilechooser,Java,Swing,Applet,Jfilechooser,当小程序在浏览器上运行时,JFileChooser无法正常工作。GUI出现,但单击浏览按钮时,不会发生任何操作,并显示空白文件系统。从Eclipse测试小程序时,在web页面之外,JFileChooser工作正常。如何让JFileChooser访问本地文件 import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.Image; import java.awt.Rendering
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();
}
}
导入java.awt.Color;
导入java.awt.Font;
导入java.awt.Graphics2D;
导入java.awt.Image;
导入java.awt.RenderingHints;
导入java.awt.SystemColor;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.awt.image.buffereImage;
导入java.io.File;
导入java.io.IOException;
导入java.nio.file.Path;
导入java.util.Scanner;
导入javax.imageio.imageio;
导入javax.print.DocFlavor.BYTE_数组;
导入javax.swing.ImageIcon;
导入javax.swing.JButton;
导入javax.swing.JFileChooser;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JTextField;
公共类测试扩展了javax.swing.JApplet{
JFrame frame=新JFrame();//帧
JTextField textField_path=new JTextField();//浏览路径
JButton btnBrowse=新JButton(“浏览”);
JLabel JLabel_image1=新的JLabel(“”);
JLabel JLabel_image2=新的JLabel(“”);
JButton btnSave=新JButton(“保存”);
BuffereImage img=null;
BuffereImage image1=null;
BuffereImage capture=null;
//函数重新缩放图像以适应帧
私有映像缩放映像(缓冲映像映像,int w,int h){
BuffereImage resizeimage=新的BuffereImage(w,h,BuffereImage.TYPE_INT_BGR);
Graphics2D g2=resizeimage.createGraphics();
g2.setRenderingHint(RenderingHits.KEY\u插值,RenderingHits.VALUE\u插值\u双线性);
g2.绘图图像(图像,0,0,w,h,空);
g2.dispose();
返回大小图像;
}
公开考试(){
//设置帧大小
frame.getContentPane().setBackground(新颜色(169169169169));
frame.setSize(900700);//2
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//3
frame.setLocationRelativeTo(空);
textField_path.set前台(新颜色(0,0,0));
textField_path.setBackground(新颜色(245245220));
//设置用于浏览的文本路径
textField_path.setBounds(114626507,23);
frame.getContentPane().setLayout(null);//初始设置路径字段为空
frame.getContentPane().add(textField_路径);
Jlabel_image1.设置背景(SystemColor.inactiveCaptionBorder);
//设置原始图像的标签
Jlabel_图像1.立根(142,29429267);
frame.getContentPane().add(Jlabel_image1);
//设置灰色图像的标签
Jlabel_图像2.立根(142345429267);
frame.getContentPane().add(Jlabel_image2);
setFont(新字体(“Cambria Math”,Font.PLAIN,13));
//设置浏览按钮
btnBrowse.setBounds(714621,89,30);
frame.getContentPane().add(btnBrowse);
setFont(新字体(“Cambria Math”,Font.PLAIN,13));
//设置保存按钮
btnSave.setBounds(714580,89,30);
frame.getContentPane().add(btnSave);
//保存按钮的actionlistener
添加ActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
JFileChooser jFile=newjfilechooser();
jFile.showsavedilog(空);
路径pth=jFile.getSelectedFile().toPath();
//showMessageDialog(null,pth.toString());
//Graphics2D Graphics2D=image1.createGraphics();
试试{
write(img,“png”,新文件(pth.toString());
}捕获(IOOX){
//TODO:处理异常
ox.printStackTrace();
}
}
});
//浏览按钮的actonlistener
btnBrowse.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
System.out.println(“浏览按钮”);
JFileChooser chooser=新的JFileChooser();
int figure=chooser.showOpenDialog(选择器);
if(图==JFileChooser.APPROVE\u选项){
File File=chooser.getSelectedFile();
如果(!file.exists()){
回来
}
试一试{
字符串filedetails[]=新字符串[2];
filedetails[0]=file.getName();
filedetails[1]=file.getAbsolutePath();
textField_path.setText(filedetails[1]);//显示浏览图像的路径
img=ImageIO.read(文件);
image1=ImageIO.read(文件);
对于(inti=0;i