导航:首页 > 编程语言 > javaswing选择文件

javaswing选择文件

发布时间:2022-09-03 03:26:47

java,关于swing选择框和文件读取求助!!!

对于问题一,你要改动两个地方
1.img1 = new JLabel("test");要先赋值.
2.点击事件中加入下面的代码
this.remove(img1);
img1 = new JLabel(image);// 把图片放到标签img1里。
add(img1, BorderLayout.CENTER);
this.pack();
对于你的问题二写的不清楚,不回答了

Ⅱ 急求一个简单的java 界面程序 实现一个选择本地电脑文件的功能

importjava.io.*;
importjava.awt.*;
importjavax.swing.*;
importjava.awt.event.*;
{
JFrameframe=newJFrame("文件选择器实例");
JTabbedPanetabPane=newJTabbedPane();//选项卡布局
Containercon=newContainer();//布局1
Containercon1=newContainer();//布局2
JLabellabel1=newJLabel("选择目录");
JLabellabel2=newJLabel("选择文件");
JTextFieldtext1=newJTextField();
JTextFieldtext2=newJTextField();
JButtonbutton1=newJButton("...");
JButtonbutton2=newJButton("...");
JFileChooserjfc=newJFileChooser();//文件选择器
YFileChooser(){
jfc.setCurrentDirectory(newFile("d:\"));//文件选择器的初始目录定为d盘
//下面两行是取得屏幕的高度和宽度
doublelx=Toolkit.getDefaultToolkit().getScreenSize().getWidth();
doublely=Toolkit.getDefaultToolkit().getScreenSize().getHeight();
frame.setLocation(newPoint((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);//使能关闭窗口,结束程序
}
publicvoidactionPerformed(ActionEvente){//事件处理
if(e.getSource().equals(button1)){//判断触发方法的按钮是哪个
jfc.setFileSelectionMode(1);//设定只能选择到文件夹
intstate=jfc.showOpenDialog(null);//此句是打开文件选择器界面的触发语句
if(state==1){
return;//撤销则返回
}
else{
Filef=jfc.getSelectedFile();//f为选择到的目录
text1.setText(f.getAbsolutePath());
}
}
if(e.getSource().equals(button2)){
jfc.setFileSelectionMode(0);//设定只能选择到文件
intstate=jfc.showOpenDialog(null);//此句是打开文件选择器界面的触发语句
if(state==1){
return;//撤销则返回
}
else{
Filef=jfc.getSelectedFile();//f为选择到的文件
text2.setText(f.getAbsolutePath());
}
}
}
publicstaticvoidmain(String[]args){
newYFileChooser();
}
}

不用谢~请叫我雷锋

Ⅲ 如何在java swing界面上传图片能弹出一个窗口选择本地文件,用的access

在swing界面上实例化你要弹出的窗口,然后再写窗口里读取文件功能

Ⅳ java图形用户界面的选择一个文件并复制(另存为)的代码,麻烦了。

importjava.awt.EventQueue;

importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JOptionPane;

importjava.awt.Font;

importjavax.swing.JTextField;
importjavax.swing.JButton;

importjava.awt.Color;
importjava.awt.event.ActionListener;
importjava.awt.event.ActionEvent;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.io.InputStream;

importjavax.swing.JFileChooser;

publicclassCopyFile{

privateJFrameframe;
privateJTextFieldtextField;
privateJTextFieldtextField_1;
privateJFileChooserchooser;
privateStringreadPath;
privateStringwritePath;

/**
*Launchtheapplication.
*/
publicstaticvoidmain(String[]args){
EventQueue.invokeLater(newRunnable(){
publicvoidrun(){
try{
CopyFilewindow=newCopyFile();
window.frame.setVisible(true);
}catch(Exceptione){
e.printStackTrace();
}
}
});
}

/**
*Createtheapplication.
*/
publicCopyFile(){
initialize();
}

/**
*.
*/
privatevoidinitialize(){
frame=newJFrame();
frame.setBounds(100,100,545,277);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);

JLabellabel=newJLabel("u6587u4EF6uFF1A");
label.setFont(newFont("黑体",Font.BOLD,18));
label.setBounds(26,68,57,25);
frame.getContentPane().add(label);

JLabellblNewLabel=newJLabel("u4FDDu5B58u76EEu5F55uFF1A");
lblNewLabel.setFont(newFont("黑体",Font.BOLD,18));
lblNewLabel.setBounds(10,119,95,25);
frame.getContentPane().add(lblNewLabel);

textField=newJTextField();
textField.setBounds(105,68,299,25);
frame.getContentPane().add(textField);
textField.setColumns(10);

textField_1=newJTextField();
textField_1.setBounds(105,121,299,25);
frame.getContentPane().add(textField_1);
textField_1.setColumns(10);

chooser=newJFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);//设置选择模式,既可以选择文件又可以选择文件夹

JButtonbutton=newJButton("u6253u5F00");
button.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
intindex=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(newFont("黑体",Font.BOLD,18));
button.setBounds(432,67,87,26);
frame.getContentPane().add(button);

JButtonbutton_1=newJButton("u6D4Fu89C8");
button_1.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
intindex=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(newFont("黑体",Font.BOLD,18));
button_1.setBounds(432,118,87,26);
frame.getContentPane().add(button_1);

JButtonbutton_2=newJButton("u53E6u5B58u4E3A");
button_2.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
readPath=textField.getText();
writePath=textField_1.getText()+"\";
if(File(readPath,writePath)==-1){//原文件不存在
JOptionPane.showMessageDialog(null,"源文件不存在","警告",JOptionPane.ERROR_MESSAGE);
}
}
});
button_2.setForeground(Color.RED);
button_2.setFont(newFont("黑体",Font.BOLD,18));
button_2.setBounds(213,180,93,34);
frame.getContentPane().add(button_2);
}

/*
**
*复制单个文件
*
*@paramoldPathString原文件路径如:c:/fqf.txt
*
*@paramnewPathString复制后路径如:f:/fgf.txt
*
*@returnint0表示成功,-1表示原文件不存在,-2表示未知错误。
*/
publicintFile(StringoldPath,StringnewPath){
try{
intbytesum=0;
intbyteread=0;
Fileoldfile=newFile(oldPath);
if(oldfile.exists()){//文件存在时
InputStreaminStream=newFileInputStream(oldPath);//读入原文件
System.out.println(newPath);
if(isExist(newPath)){
FileOutputStreamfs=newFileOutputStream(newPath);
byte[]buffer=newbyte[1444];
while((byteread=inStream.read(buffer))!=-1){
bytesum+=byteread;//字节数文件大小
System.out.println(bytesum);
fs.write(buffer,0,byteread);
}
inStream.close();
fs.close();
return0;
}else{
return-2;
}
}
return-1;
}catch(Exceptione){
System.out.println("复制单个文件操作出错");
e.printStackTrace();
return-2;
}
}

publicstaticbooleanisExist(StringfilePath){
Stringpaths[]=filePath.split("\\");
Stringdir=paths[0];
for(inti=0;i<paths.length-2;i++){//注意此处循环的长度
try{
dir=dir+"/"+paths[i+1];
FiledirFile=newFile(dir);
if(!dirFile.exists()){
dirFile.mkdir();
System.out.println("创建目录为:"+dir);
}
}catch(Exceptionerr){
System.err.println("ELS-Chart:文件夹创建发生异常");
}
}
Filefp=newFile(filePath);
if(!fp.exists()){
returntrue;//文件不存在,执行下载功能
}else{
returnfalse;//文件存在不做处理
}
}

}

Ⅳ 求java源代码。使用swing或AWT。实现功能:点击按钮,选择一个txt文本文件,并将txt中

搞定了

packagecom.monubia;
importjava.awt.event.MouseAdapter;
importjava.awt.event.MouseEvent;
importjava.io.BufferedReader;
importjava.io.FileNotFoundException;
importjava.io.FileReader;
importjava.io.IOException;

importjavax.swing.JButton;

importjavax.swing.JDialog;
importjavax.swing.JFileChooser;
importjavax.swing.JTextArea;
importjavax.swing.WindowConstants;
importjavax.swing.SwingUtilities;
importjavax.swing.filechooser.FileNameExtensionFilter;


/**
*'sJigloo
*SWT/SwingGUIBuilder,whichisfreefornon-commercial
*use.(ie,byacorporation,
*)thenyou
*.
*Pleasevisitwww.cloudgarden.comfordetails.
*.
*
*THISMACHINE,
*.
*/
publicclassTxtextendsjavax.swing.JFrame{
privateJButtonjButton_open;
privateJTextAreajTextArea1;

/**
*Auto-
*/
publicstaticvoidmain(String[]args){
SwingUtilities.invokeLater(newRunnable(){
publicvoidrun(){
Txtinst=newTxt();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
}

publicTxt(){
super();
initGUI();
}

privatevoidinitGUI(){
try{
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
getContentPane().setLayout(null);
{
jButton_open=newJButton();
getContentPane().add(jButton_open);
jButton_open.setText("Open");
jButton_open.setBounds(155,114,92,49);
jButton_open.addMouseListener(newMouseAdapter(){
publicvoidmouseClicked(MouseEventevt){
jButton_openMouseClicked(evt);
}
});
}
{
jTextArea1=newJTextArea();
getContentPane().add(jTextArea1);
jTextArea1.setBounds(0,0,384,262);
}
pack();
setSize(400,300);
}catch(Exceptione){
//addyourerrorhandlingcodehere
e.printStackTrace();
}
}

privatevoidjButton_openMouseClicked(MouseEventevt){
//点击了打开
JFileChooseropen=newJFileChooser();
FileNameExtensionFiltertxt=newFileNameExtensionFilter("TxtFile","txt");
open.setFileFilter(txt);
intret=open.showOpenDialog(this);
if(ret==JFileChooser.APPROVE_OPTION)
{
jButton_open.setOpaque(false);
jButton_open.setVisible(false);
System.out.println(open.getSelectedFile().getAbsolutePath());
try{
BufferedReaderbr=newBufferedReader(newFileReader(open.getSelectedFile().getAbsolutePath()));
Stringline=null;
while((line=br.readLine())!=null)
{
jTextArea1.append(line+" ");
}
}catch(FileNotFoundExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
}

}

Ⅵ java swing中文件选择框如何实现用鼠标拖动框选 还有jbutton的enable为fals

你的java和图片放在一个目录,
我都是放在C盘根目录了,
给你稍微改了一下代码:
import java.awt.*;
import javax.swing.*;
public class TestGra extends JFrame {
 Container c = getContentPane();
 JLabel lb;
 Image image;
 public TestGra() {
  // 就改这里了
  ImageIcon img = new ImageIcon(System.getProperty("user.dir") + "\\1.jpeg");
  lb = new JLabel(img);
  
  add(lb, BorderLayout.CENTER);
  setSize(800, 600);
  setVisible(true);
  setDefaultCloseOperation(EXIT_ON_CLOSE);
 }
 public static void main(String as[]) {
  new TestGra();
 }
}

Ⅶ java swing 中的FileDialog

import java.io.File;
/**
*
* @author Administrator
*/
public class EBBFileFilter extends javax.swing.filechooser.FileFilter {
private String Filter_Type;
public EBBFileFilter(String Filter_Type){
this.Filter_Type = Filter_Type;
}
public boolean accept(File f) {
if(Filter_Type.equals("FileFilter_NB")){
if (f.isDirectory()|| f.getPath().toLowerCase().endsWith(".nb")) {
return true;
}else{
return false;
}
}
if(Filter_Type.equals("FileFilter_YB")){
if (f.isDirectory()|| f.getPath().toLowerCase().endsWith(".yb")) {
return true;
}else{
return false;
}
}
if(Filter_Type.equals("FileFilter_RB")){
if (f.isDirectory()|| f.getPath().toLowerCase().endsWith(".rb")) {
return true;
}else{
return false;
}
}
if(Filter_Type.equals("FileFilter_ETF")){
if (f.isDirectory()|| f.getPath().toLowerCase().endsWith(".etf")) {
return true;
}else{
return false;
}
}else{
return true;
}

}
public String getDescription(){
if(Filter_Type.equals("FileFilter_NB")){
return "年报";
}
if(Filter_Type.equals("FileFilter_YB")){
return "月报";
}
if(Filter_Type.equals("FileFilter_RB")){
return "日报";
}
if(Filter_Type.equals("FileFilter_ETF")){
return "模板文件";
}
return null;
}
}

/////////---

//打开报表文件
private void open_BB() {
javax.swing.JFileChooser cf = new javax.swing.JFileChooser() {

public void approveSelection() {
super.approveSelection();
File file = this.getSelectedFile();
open_BB_Data(file);
}
};
cf.setDialogTitle("选择文件");
cf.setCurrentDirectory(new java.io.File("file"));
cf.setMaximumSize(new java.awt.Dimension(425, 245));
cf.addChoosableFileFilter(new EBBFileFilter("FileFilter_NB"));
cf.addChoosableFileFilter(new EBBFileFilter("FileFilter_YB"));
cf.addChoosableFileFilter(new EBBFileFilter("FileFilter_RB"));
cf.setAcceptAllFileFilterUsed(true);
cf.showOpenDialog(this);
}

Ⅷ java swing文件选择工具JFileChooser怎么改变打开和取消按钮的名字

JFileChooser 用setApproveButtonText(String approveButtonText) 改变选定按钮的文本
取消按钮好像不能单独改变文本,可以在整个UI层面语言包层面替换取消的文本。。通常不用改取消按钮
用法
1、 fc.setApproveButtonText("打开存档");//单独指定按钮文本
2、fc.showDialog(null,"打开存档");在显示前顺便指定按钮文本

Ⅸ java Swing的导入文件问题

JFileChooserchooser=newJFileChooser();
FileNameExtensionFilterfilter=newFileNameExtensionFilter(
"xls");
chooser.setFileFilter(filter);
intreturnVal=chooser.showOpenDialog(parent);
if(returnVal==JFileChooser.APPROVE_OPTION){
System.out.println("Youchosetoopenthisfile:"+
chooser.getSelectedFile().getName());
}

Ⅹ JAVA中如何触动按钮后弹出文件浏览器和文件名选择框,选择保存目录

使用javaswing中的JFileChooser类就ok了
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.filechooser.FileNameExtensionFilter;

public class WebJianXuanZe {

public static void fileChooser() {
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"JPG & GIF Images", "jpg", "gif");
//设置文件类型
chooser.setFileFilter(filter);
//打开选择器面板
int returnVal = chooser.showOpenDialog(new JPanel());
//保存文件从这里入手,输出的是文件名
if(returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("你打开的文件是: " +
chooser.getSelectedFile().getName());
}
}
public static void main(String[] args) {
fileChooser();
}
}

建议你可以翻java API 。新童鞋的话一定要学会用API、善于用API,尽量独立解决问题,这样能够加深映像,并且对子什么也是一个激励!~

阅读全文

与javaswing选择文件相关的资料

热点内容
汉语命令形 浏览:193
ACG官网下载的游戏怎么解压 浏览:963
stata交叉项命令 浏览:470
程序员老公烫头 浏览:692
服务器文件地址格式 浏览:131
securecrtandroid 浏览:176
短字符串压缩 浏览:863
u盘插入后显示加密格式化 浏览:944
我的世界怎么用命令方块获得超级武器 浏览:382
狗语翻译器app链接怎么下 浏览:905
选择排序算法的流程图 浏览:881
如何对文件夹开启共享 浏览:527
常用的磁盘调度算法 浏览:662
怎么用返利app返利 浏览:127
java代码快速 浏览:243
单片机左移右移后是补1还是0 浏览:599
湛江一号命令 浏览:333
导出命令行 浏览:274
C和php交互 浏览:600
苹果手机里的通讯录如何导入安卓手机 浏览:170