demofilechooserdialog.java
来自「java绘图 java awt 经典绘图的例子,对于初学awt模块的人非常有帮助」· Java 代码 · 共 85 行
JAVA
85 行
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package swing;import java.awt.Container;import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.File;import javax.swing.JButton;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.filechooser.FileFilter;/** * * @author zhaolin */public class DemoFileChooserDialog extends JFrame{ private JFileChooser jfc; public DemoFileChooserDialog(){ jfc = new JFileChooser(); jfc.setFileFilter(new FileFilter(){ @Override public boolean accept(File f) { if(f.isDirectory()) return true; if(f.getName().endsWith(".jpg")){ return true; } return false; } @Override public String getDescription() { return "jpg"; } }); jfc.setAcceptAllFileFilterUsed(false); Container c = this.getContentPane(); c.setLayout(new FlowLayout()); JButton open = new JButton("打开"); JButton save = new JButton("保存"); c.add(open ); c.add(save); open.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { int status = jfc.showOpenDialog(DemoFileChooserDialog.this); if(status == JFileChooser.APPROVE_OPTION){ File file = jfc.getSelectedFile(); System.out.println("Open file "+file.getName()); } } }); save.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { int status = jfc.showSaveDialog(DemoFileChooserDialog.this); if(status == JFileChooser.APPROVE_OPTION){ File file = jfc.getSelectedFile(); System.out.println("Open file "+file.getName()); } } }); this.pack(); this.setVisible(true); } public static void main(String[] args) { new DemoFileChooserDialog(); } }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?