当前位置: 技术问答>java相关
在JAVA中如何实现WINDOWS中的打开对话框???
来源: 互联网 发布时间:2017-03-26
本文导语: 我想在程序中加上一个打开对话框,请问该如何做? 谢谢 我发现JDK中自带的实用程序中(一个用JAVA做的编辑器)的打开对话框和WINDOWS的一模一样 不是不直接用WINDOWS中的呀? | //JFileChooserTest.ja...
我想在程序中加上一个打开对话框,请问该如何做?
谢谢
我发现JDK中自带的实用程序中(一个用JAVA做的编辑器)的打开对话框和WINDOWS的一模一样 不是不直接用WINDOWS中的呀?
谢谢
我发现JDK中自带的实用程序中(一个用JAVA做的编辑器)的打开对话框和WINDOWS的一模一样 不是不直接用WINDOWS中的呀?
|
//JFileChooserTest.java
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.io.File;
import javax.swing.*;
public class JFileChooserTest extends JFrame
{
JFileChooser chooser = new JFileChooser();
JButton button = new JButton("show file chooser...");
ImagePreviewer previewer = new ImagePreviewer();
PreviewPanel previewPanel = new PreviewPanel();
class PreviewPanel extends JPanel
{
public PreviewPanel()
{
JLabel label = new JLabel("Image Previewer",SwingConstants.CENTER);
setPreferredSize(new Dimension(150,0));
setBorder(BorderFactory.createEtchedBorder());
setLayout(new BorderLayout());
label.setBorder(BorderFactory.createEtchedBorder());
add(label,BorderLayout.NORTH);
add(previewer,BorderLayout.CENTER);
}
}
public JFileChooserTest()
{
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
contentPane.add(button);
chooser.setAccessory(previewPanel);
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int state = chooser.showOpenDialog(null);
File file = chooser.getSelectedFile();
String s = "CANCELED";
if (file != null && state == JFileChooser.APPROVE_OPTION)
{
s = "File Selected:" + file.getPath();
}
JOptionPane.showMessageDialog(null,s);
}
});
chooser.addPropertyChangeListener(new PropertyChangeListener()
{
public void propertyChange(PropertyChangeEvent e)
{
if (e.getPropertyName().equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY))
{
File f = (File) e.getNewValue();
String s = f.getPath(),suffix = null;
int i = s.lastIndexOf('.');
if (i>0 && i
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.io.File;
import javax.swing.*;
public class JFileChooserTest extends JFrame
{
JFileChooser chooser = new JFileChooser();
JButton button = new JButton("show file chooser...");
ImagePreviewer previewer = new ImagePreviewer();
PreviewPanel previewPanel = new PreviewPanel();
class PreviewPanel extends JPanel
{
public PreviewPanel()
{
JLabel label = new JLabel("Image Previewer",SwingConstants.CENTER);
setPreferredSize(new Dimension(150,0));
setBorder(BorderFactory.createEtchedBorder());
setLayout(new BorderLayout());
label.setBorder(BorderFactory.createEtchedBorder());
add(label,BorderLayout.NORTH);
add(previewer,BorderLayout.CENTER);
}
}
public JFileChooserTest()
{
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
contentPane.add(button);
chooser.setAccessory(previewPanel);
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int state = chooser.showOpenDialog(null);
File file = chooser.getSelectedFile();
String s = "CANCELED";
if (file != null && state == JFileChooser.APPROVE_OPTION)
{
s = "File Selected:" + file.getPath();
}
JOptionPane.showMessageDialog(null,s);
}
});
chooser.addPropertyChangeListener(new PropertyChangeListener()
{
public void propertyChange(PropertyChangeEvent e)
{
if (e.getPropertyName().equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY))
{
File f = (File) e.getNewValue();
String s = f.getPath(),suffix = null;
int i = s.lastIndexOf('.');
if (i>0 && i