導航:首頁 > 編程語言 > 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選擇文件相關的資料

熱點內容
華為手機為什麼不升級安卓11 瀏覽:42
linuxrpm卸載jdk 瀏覽:860
mysql許可權設置命令 瀏覽:618
hexophp 瀏覽:271
用什麼app買東西半價 瀏覽:62
蘋果下載的pdf文件怎麼打開 瀏覽:211
如何在伺服器上隱藏源站地址 瀏覽:645
單片機進制字母對應表 瀏覽:528
向某人下命令 瀏覽:627
編程中刪除數組中的數 瀏覽:86
aes對稱加密反編譯 瀏覽:550
java編譯成exe 瀏覽:190
gps處理演算法 瀏覽:596
什麼app可以和對象存錢 瀏覽:146
java字元串表達式計算 瀏覽:330
javacmd環境變數 瀏覽:51
電視上面找不到全民歌app怎麼辦 瀏覽:156
單片機中psw0 瀏覽:994
優酷視頻加密么 瀏覽:763
本地連接dos命令 瀏覽:206