当前位置: 技术问答>java相关
关于路径选择对话框
来源: 互联网 发布时间:2015-06-11
本文导语: 我想用在java应用程序中放置一个路径选择对话框,哪有现成的东西可以用? | JFileChooser yours=new JFileChooser(); yours.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); youes.showOpenDialog(parent); | j2sdk1....
我想用在java应用程序中放置一个路径选择对话框,哪有现成的东西可以用?
|
JFileChooser yours=new JFileChooser();
yours.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
youes.showOpenDialog(parent);
yours.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
youes.showOpenDialog(parent);
|
j2sdk1.4.0demojfcFileChooserDemo这个例子应该能满足你。
package pGComponent;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import javax.swing.*;
import java.beans.*;
/**
*
*
*
*
* @author unascribed
* @version 1.0
*/
public class ViewFileChoose extends JFileChooser
{
ImagePreviewer previewer = new ImagePreviewer();
public String c ="name";
public String name = "aaaa";
public ViewFileChoose()
{
super("FileChoose Components");
setAccessoryComponent();
this.showOpenDialog(null);
}
private void setAccessoryComponent()
{
JPanel previewPanel = new JPanel();
previewPanel.setLayout(new BorderLayout());
previewPanel.add(new JLabel("Image Previewer",
SwingConstants.CENTER),
BorderLayout.NORTH);
previewPanel.add(previewer, BorderLayout.CENTER);
previewer.setPreferredSize(new Dimension(200,0));
previewer.setBorder(BorderFactory.createEtchedBorder());
this.setAccessory(previewPanel);//设置打开文件对话框的附属组件
//new ImagePreviewerAccessoryAdapter(this, previewer);;
this.addPropertyChangeListener(new PropertyChangeListener()
{
public void propertyChange(PropertyChangeEvent e)
{
if(e.getPropertyName().equals( JFileChooser.SELECTED_FILE_CHANGED_PROPERTY))
{
previewer.update((File)e.getNewValue());
}
}
});
}
}
class ImagePreviewer extends JComponent
{
private ImageIcon icon;
public void update(File file)
{
Dimension size = getSize();
Insets insets = getInsets();
System.err.println(insets);
icon = new ImageIcon(file.getPath());
icon.setImage(icon.getImage().getScaledInstance(
size.width - insets.left - insets.right,
size.height - insets.top - insets.bottom,
Image.SCALE_SMOOTH));
if(isShowing())
{
repaint();
}
}
public void paintComponent(Graphics g)
{
Insets insets = getInsets();
super.paintComponent(g);
if(icon != null)
icon.paintIcon(this, g, insets.left, insets.top);
}
}
package pGComponent;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import javax.swing.*;
import java.beans.*;
/**
*
Title: 多文档
*
Description: 测试java多文档的实现
*
Copyright: Copyright (c) 2002
*
Company:
* @author unascribed
* @version 1.0
*/
public class ViewFileChoose extends JFileChooser
{
ImagePreviewer previewer = new ImagePreviewer();
public String c ="name";
public String name = "aaaa";
public ViewFileChoose()
{
super("FileChoose Components");
setAccessoryComponent();
this.showOpenDialog(null);
}
private void setAccessoryComponent()
{
JPanel previewPanel = new JPanel();
previewPanel.setLayout(new BorderLayout());
previewPanel.add(new JLabel("Image Previewer",
SwingConstants.CENTER),
BorderLayout.NORTH);
previewPanel.add(previewer, BorderLayout.CENTER);
previewer.setPreferredSize(new Dimension(200,0));
previewer.setBorder(BorderFactory.createEtchedBorder());
this.setAccessory(previewPanel);//设置打开文件对话框的附属组件
//new ImagePreviewerAccessoryAdapter(this, previewer);;
this.addPropertyChangeListener(new PropertyChangeListener()
{
public void propertyChange(PropertyChangeEvent e)
{
if(e.getPropertyName().equals( JFileChooser.SELECTED_FILE_CHANGED_PROPERTY))
{
previewer.update((File)e.getNewValue());
}
}
});
}
}
class ImagePreviewer extends JComponent
{
private ImageIcon icon;
public void update(File file)
{
Dimension size = getSize();
Insets insets = getInsets();
System.err.println(insets);
icon = new ImageIcon(file.getPath());
icon.setImage(icon.getImage().getScaledInstance(
size.width - insets.left - insets.right,
size.height - insets.top - insets.bottom,
Image.SCALE_SMOOTH));
if(isShowing())
{
repaint();
}
}
public void paintComponent(Graphics g)
{
Insets insets = getInsets();
super.paintComponent(g);
if(icon != null)
icon.paintIcon(this, g, insets.left, insets.top);
}
}
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。