public class ShowImage extends JPanel implements ActionListener {
private JButton open ;
private String filePath;
private BufferedImage image;
private static ShowImage show;
public static ShowImage singleton(){
if(show==null)
show=new ShowImage();
return show;
}
private ShowImage(){
open =new JButton("open");
setLayout(new BorderLayout());
add(open,BorderLayout.SOUTH);
open.addActionListener(this);
}
private void init(String str){
FileInputStream input;
try {
input = new FileInputStream(str);
image=ImageIO.read(input);
repaint();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("JFileChooserDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Add content to the window.
frame.add(singleton());
frame.setSize(300,400);
frame.setTitle("QRCode");
//Display the window.
frame.setVisible(true);
}
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
//Turn off metal's use of bold fonts
UIManager.put("swing.boldMetal", Boolean.FALSE);
createAndShowGUI();
}
});
}
@Override
public void paint(Graphics g) {
// TODO Auto-generated method stub
g.drawImage(image,26,26,this);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==open){
JFileChooser dialog=new JFileChooser();
int result=dialog.showOpenDialog(null);
if(result==JFileChooser.APPROVE_OPTION){
File file=dialog.getSelectedFile();
filePath=file.getPath();
init(filePath);
}
}
}
}
保存与打开类似 ,得到要保存或打开的文件路径后,用其创建文件ios,进行相关操作即可。
分享到:
相关推荐
主要介绍了Java文件选择对话框JFileChooser使用详解的相关资料,需要的朋友可以参考下
JFileChooser使用详解,告诉你这个类怎样使用,包括打开文件,选择文件 啊,等等!
NULL 博文链接:https://1021443917-qq-com.iteye.com/blog/1099332
Swing中使用JFileChooser选择文件目录
JFileChooser chooser = new JFileChooser(); chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); chooser.showSaveDialog(null);
选择路径保存文件,运用JFileChooser组件生成、
JFileChooser下载文件到本地,可选择文件保存路径,可以上传文件,保存文件chooser.showSaveDialog()函数,打开文件chooser.showOpenDialog()函数
这是JDK里的JFileChooser类解压出的源代码
JFileChooser是Swing库提供的一个用于选择文件和目录的组件。它允许用户浏览文件系统并选择所需的文件或目录。
JFileChooser类简单的修改!其他的地方还没研究明白!希望大家一起研究下!
java程序设计 TCP文件传输程序 JFileChooser实现文件选择 可以设置路径
java swing小界面,文件选择JFileChooser使用
主要介绍了Java Swing组件文件选择器JFileChooser简单用法,结合实例形式分析了Swing组件中的文件选择器JFileChooser的简单使用方法,需要的朋友可以参考下
今天小编就为大家分享一篇关于实例化JFileChooser对象报空指针异常问题的解决办法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
主要介绍了Java Swing组件JFileChooser用法,结合实例形式分析了java Swing组件JFileChooser文件选择器的功能、使用方法及相关注意事项,需要的朋友可以参考下
Java中文件选择器JFileChooser.showSaveDialog实现默认文件名的解决方案
JFileChoose的使用详解,JFileChooser 用来提供一个文件对话框,可以通过其 showXxxDialog 打开一 个模态对话框,或直接实例化并加入到其他组件。
NULL 博文链接:https://happyqing.iteye.com/blog/2063990
使用JFileChooser类实现文件的打开、保存和新建功能。用户可以通过菜单栏或快捷键调用相应的方法。 ``` JFileChooser fileChooser = new JFileChooser(); // 打开文件 int result = fileChooser.showOpenDialog...
讲述了 java io流的一些基本方法。记事本的一些基本功能 介绍了jpanel,jmenubar,jfilechooser