java选择文件代码 java swing选择文件

急求一个简单的java 界面程序 实现一个选择本地电脑文件的功能import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class YFileChooser implements ActionListener{
JFrame frame=new JFrame("文件选择器实例");
JTabbedPane tabPane=new JTabbedPane();//选项卡布局
Container con=new Container();//布局1
Container con1=new Container();//布局2
JLabel label1=new JLabel("选择目录");
JLabel label2=new JLabel("选择文件");
JTextField text1=new JTextField();
JTextField text2=new JTextField();
JButton button1=new JButton("...");
JButton button2=new JButton("...");
JFileChooser jfc=new JFileChooser();//文件选择器
YFileChooser(){
jfc.setCurrentDirectory(new File("d:\\"));//文件选择器的初始目录定为d盘
//下面两行是取得屏幕的高度和宽度
double lx=Toolkit.getDefaultToolkit().getScreenSize().getWidth();
double ly=Toolkit.getDefaultToolkit().getScreenSize().getHeight();
frame.setLocation(new Point((int)(lx/2)-150,(int)(ly/2)-150));//设定窗口出现位置
frame.setSize(300,150);//设定窗口大小
frame.setContentPane(tabPane);//设置布局
//下面设定标签等的出现位置和高宽
label1.setBounds(10,10,70,20);
label2.setBounds(10,30,100,20);
text1.setBounds(80,10,120,20);
text2.setBounds(80,30,120,20);
button1.setBounds(210,10,50,20);
button2.setBounds(210,30,50,20);
button1.addActionListener(this);//添加事件处理
button2.addActionListener(this);//添加事件处理
con.add(label1);
con.add(label2);
con.add(text1);
con.add(text2);
con.add(button1);
con.add(button2);
con.add(jfc);
tabPane.add("目录/文件选择",con);//添加布局1
tabPane.add("暂无内容",con1);//添加布局2
frame.setVisible(true);//窗口可见
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//使能关闭窗口,结束程序
}
public void actionPerformed(ActionEvent e){//事件处理
if(e.getSource().equals(button1)){//判断触发方法的按钮是哪个
jfc.setFileSelectionMode(1);//设定只能选择到文件夹
int state=jfc.showOpenDialog(null);//此句是打开文件选择器界面的触发语句
if(state==1){
return;//撤销则返回
}
else{
File f=jfc.getSelectedFile();//f为选择到的目录
text1.setText(f.getAbsolutePath());
}
}
if(e.getSource().equals(button2)){
jfc.setFileSelectionMode(0);//设定只能选择到文件
【java选择文件代码 java swing选择文件】int state=jfc.showOpenDialog(null);//此句是打开文件选择器界面的触发语句
if(state==1){
return;//撤销则返回
}
else{
File f=jfc.getSelectedFile();//f为选择到的文件
text2.setText(f.getAbsolutePath());
}
}
}
public static void main(String[] args) {
new YFileChooser();
}
}
不用谢~请叫我雷锋
java图形用户界面的选择一个文件并复制(另存为)的代码,麻烦了 。import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import javax.swing.JFileChooser;
public class CopyFile {
private JFrame frame;
private JTextField textField;
private JTextField textField_1;
private JFileChooser chooser;
private String readPath;
private String writePath;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CopyFile window = new CopyFile();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public CopyFile() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 545, 277);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel label = new JLabel("文件:");
label.setFont(new Font("黑体", Font.BOLD, 18));
label.setBounds(26, 68, 57, 25);
frame.getContentPane().add(label);
JLabel lblNewLabel = new JLabel("保存目录:");
lblNewLabel.setFont(new Font("黑体", Font.BOLD, 18));
lblNewLabel.setBounds(10, 119, 95, 25);
frame.getContentPane().add(lblNewLabel);
textField = new JTextField();
textField.setBounds(105, 68, 299, 25);
frame.getContentPane().add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setBounds(105, 121, 299, 25);
frame.getContentPane().add(textField_1);
textField_1.setColumns(10);
chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);// 设置选择模式,既可以选择文件又可以选择文件夹
JButton button = new JButton("打开");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int index = chooser.showOpenDialog(null);
chooser.setDialogType(JFileChooser.OPEN_DIALOG);
chooser.setMultiSelectionEnabled(false);
chooser.setAcceptAllFileFilterUsed(false);
if (index == JFileChooser.APPROVE_OPTION) {
// 把获取到的文件的绝对路径显示在文本编辑框中
textField.setText(chooser.getSelectedFile()
.getAbsolutePath());
readPath = textField.getText();
}
}
});
button.setFont(new Font("黑体", Font.BOLD, 18));
button.setBounds(432, 67, 87, 26);
frame.getContentPane().add(button);
JButton button_1 = new JButton("浏览");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int index = chooser.showSaveDialog(null);
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setDialogType(JFileChooser.SAVE_DIALOG);
chooser.setMultiSelectionEnabled(false);
chooser.setAcceptAllFileFilterUsed(false);
if (index == JFileChooser.APPROVE_OPTION) {
// 把获取到的文件的绝对路径显示在文本编辑框中
textField_1.setText(chooser.getSelectedFile()
.getAbsolutePath());
writePath = textField_1.getText()"\\";
}
}
});
button_1.setFont(new Font("黑体", Font.BOLD, 18));
button_1.setBounds(432, 118, 87, 26);
frame.getContentPane().add(button_1);
JButton button_2 = new JButton("另存为");
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
readPath = textField.getText();
writePath = textField_1.getText()"\\";
if(copyFile(readPath, writePath)== -1){//原文件不存在
JOptionPane.showMessageDialog(null, "源文件不存在", "警告", JOptionPane.ERROR_MESSAGE);
}
}
});
button_2.setForeground(Color.RED);
button_2.setFont(new Font("黑体", Font.BOLD, 18));
button_2.setBounds(213, 180, 93, 34);
frame.getContentPane().add(button_2);
}
/*
* *
* 复制单个文件
*
* @param oldPath String 原文件路径 如:c:/fqf.txt
*
* @param newPath String 复制后路径 如:f:/fgf.txt
*
* @return int 0表示成功,-1表示原文件不存在,-2表示未知错误 。
*/
public int copyFile(String oldPath, String newPath) {
try {
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPath);
if (oldfile.exists()) { // 文件存在时
InputStream inStream = new FileInputStream(oldPath); // 读入原文件
System.out.println(newPath);
if(isExist(newPath)){
FileOutputStream fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1444];
while ((byteread = inStream.read(buffer)) != -1) {
bytesum= byteread; // 字节数 文件大小
System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
inStream.close();
fs.close();
return 0;
}else{
return -2;
}
}
return -1;
} catch (Exception e) {
System.out.println("复制单个文件操作出错");
e.printStackTrace();
return -2;
}
}
public static boolean isExist(String filePath) {
String paths[] = filePath.split("\\\\");
String dir = paths[0];
for (int i = 0; ipaths.length - 2; i) {// 注意此处循环的长度
try {
dir = dir"/"paths[i1];
File dirFile = new File(dir);
if (!dirFile.exists()) {
dirFile.mkdir();
System.out.println("创建目录为:"dir);
}
} catch (Exception err) {
System.err.println("ELS - Chart : 文件夹创建发生异常");
}
}
File fp = new File(filePath);
if (!fp.exists()) {
return true; // 文件不存在,执行下载功能
} else {
return false; // 文件存在不做处理
}
}
}
java swing编写的图片浏览器不显示图片,这是文件选择器的代码关于样式显示不出来:1.检查样式的书写格式是否正确 , 如:style/style可能无效,尝试style type="text/css"/style 另外,检查样式代码中是否缺少结束标识符 。2.如果是引用的外部样式文件,检查下路径是否正确!3.部分样式无效 , 检查选择器是否正确,如样式定义为:#box,检查标签是否为:div id="box"/div , 防止class和id混淆 。4.动态页面有时候需要多刷新几下更新缓存 。多刷新几下确保样式被重新加载 。5.样式兼容性,这个就比较特殊了,检查你的样式整体设计是否合理 。有时候浮动、高度等设置不当,导致页面无法正常显示,另外 , 标签或属性的书写错误也会导致样式执行失效 。
java代码问题求助 , 下面的代码是想要点击按钮后从本地选取文件,并将文件内容显示在文本区 。大家帮忙看看我编译时报了
p185_6.java:50: 警告: catch 子句无法访问
catch(IOException e2){System.out.println("文件读写错");}
改正后文件可以在页面内显示 , 就是位置有点问题 。
可以把add(jta,BorderLayout.SOUTH);改成add(jta,BorderLayout.CENTER);
如果想使用滚动条,可以把JTextArea放在JScrollPane里,然后再放在JFrame中 。
add(jta,BorderLayout.SOUTH);改成add(new JScrollPane(jta),BorderLayout.CENTER);
JAVA代码chooser应该是个JFileChooser对象 getSelectedFiles是该对象的一个方法表示:如果将文件选择器设置为允许选择多个文件,则返回选中文件的列表 。
chooser.getSelectedFile().getPath() 表示将选择器中选择的文件的抽象路径名转换为一个路径名字符串 。
file[0].getAbsolutePath() 表示:返回第0个文件抽象路径名的绝对路径名字符串 。
关于java选择文件代码和java swing选择文件的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

    推荐阅读