怎样回到得到JFileChooser选择文件的路径??

pandyman2000 2003-05-04 12:50:41
在弹出的JFileChooser选择文件后点击确定按钮,怎么才能把选择文件的路径赋给一个原始的JFrame上的JTextField
下面是“确定”按钮的代码:
void JBfilepath1_actionPerformed(ActionEvent e) {
if(e.getActionCommand()=="确定"){
frame.dispose();
strSelectedPreAddr=chooser.getSelectedFile().getPath();
Frame1 frame1 = new Frame1();
frame1.jTextField1.setText(strSelectedPreAddr);
}
}
Frame1为原始窗口,无法为jTextField1赋值呀???
...全文
485 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
pandyman2000 2003-05-04
  • 打赏
  • 举报
回复
父窗体的代码:
package untitled3;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import com.borland.jbcl.layout.*;

public class Frame1 extends JFrame {
private JPanel contentPane;
private XYLayout xYLayout1 = new XYLayout();
private JButton jButton1 = new JButton();
public JTextField jTextField1 = new JTextField();
private JButton jButton2 = new JButton();
private JLabel jLabel1 = new JLabel();
private String DelPath;
//Construct the frame
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
//setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));
contentPane = (JPanel) this.getContentPane();
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
contentPane.setLayout(xYLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("DeleteFile");
jButton1.setText("选择目录");
jButton2.setText("保存路径");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton2_actionPerformed(e);
}
});
jLabel1.setText("目录路径:");
contentPane.add(jLabel1, new XYConstraints(6, 34, 66, 23));
contentPane.add(jTextField1, new XYConstraints(74, 31, 309, 27));
contentPane.add(jButton1, new XYConstraints(192, 79, 87, 30));
contentPane.add(jButton2, new XYConstraints(295, 79, -1, 30));
a("dfd");

}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}

//点击按钮弹出选择目录的文件对话框
void jButton1_actionPerformed(ActionEvent e) {
FileChooser chooser = new FileChooser();
}

void jButton2_actionPerformed(ActionEvent e) {
DelPath=jTextField1.getText();
}
void a(String aa){
jTextField1.setText(aa);
System.out.println(aa);
}
}

pandyman2000 2003-05-04
  • 打赏
  • 举报
回复
不行,楼上的方法我试过了,还是无法赋值!
trust_me 2003-05-04
  • 打赏
  • 举报
回复
你是否应该在Frame1这个类里面加一个方法:
public void setJTextFieldText(String str){
jTextField1.setText(str);
}

然后,

void JBfilepath1_actionPerformed(ActionEvent e) {
if(e.getActionCommand()=="确定"){
frame.dispose();
strSelectedPreAddr=chooser.getSelectedFile().getPath();
Frame1 frame1 = new Frame1();
frame1.setJTextFieldText(strSelectedPreAddr);
}
}
pandyman2000 2003-05-04
  • 打赏
  • 举报
回复
我有两个JFrame,当点击Frame1上的按钮后弹出另一个文件选择窗口,选择文件目录后点击确定的代就是我贴的代码:

frame.dispose();//关闭文件选择窗体
strSelectedPreAddr=chooser.getSelectedFile().getPath();//得到文件的目录
Frame1 frame1 = new Frame1();//创建第一个窗体的实例
frame1.jTextField1.setText(strSelectedPreAddr); //将得到的地址赋值给第一个窗体上的jTextField
在赋值的时候失败,没有得到值?????
thebear810611 2003-05-04
  • 打赏
  • 举报
回复
楼主的那段代码存在两个问题
1 判断两个字符串是否相同,需要用str1.equals(str2)方法
2 frame.dispose()后,你又新建了一个Frame1(),这个Frame1是你自己写的Frame的子类吗?frame也是Frame1的实例吗?不管是不是,你的程序都达不到你的效果,因为frame1和frame是两个不同的实例!
thebear810611 2003-05-04
  • 打赏
  • 举报
回复
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class test extends JFrame implements ActionListener{
JTextField jtf = new JTextField(40);
JButton jb = new JButton("打开");
JFileChooser chooser = new JFileChooser();
public test(){
this.getContentPane().add(jtf,BorderLayout.NORTH);
this.getContentPane().add(jb,BorderLayout.CENTER);
jb.addActionListener(this);
this.pack();
this.show();
}

public static void main(String a[]){
new test();
}

public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("打开")){
if(JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)){
String strSelectedPreAddr=chooser.getSelectedFile().getPath();
jtf.setText(strSelectedPreAddr);
}
}
}
}
pandyman2000 2003-05-04
  • 打赏
  • 举报
回复
高手们帮帮忙吧!
Java仿Windows 窗口化复制文件功能,   class SelectFileListener implements ActionListener { //取得目录内容的事件处理    public void actionPerformed(ActionEvent event) {    if (fileChooser.showOpenDialog(CopyFileDemo.this)==JFileChooser.APPROVE_OPTION){ //弹出文件选择器,并判断是否点击了打开按钮    String fileName=fileChooser.getSelectedFile().getAbsolutePath(); //得到选择文件的绝对路径    if (event.getSource().equals(selectFile1)){ //判断事件来自于哪个按钮    jtfSourceFile.setText(fileName); //设置源文件路径    }    else{    jtfTargetFile.setText(fileName); //设置目标文件路径    }    }    }   }   public boolean copy(String file1,String file2){ //拷贝文件方法    try{    java.io.File fileIn=new java.io.File(file1); //用路径名生成源文件    java.io.File fileOut=new java.io.File(file2); //用路径名生成目标文件    FileInputStream fin=new FileInputStream(fileIn); //得到文件输入流    FileOutputStream fout=new FileOutputStream(fileOut); //得到文件输出流    byte[] bytes=new byte[1024]; //初始化字节数组,用于缓冲    int c;    while((c=fin.read(bytes))!=-1){ //如果文件未读完    fout.write(bytes,0,c); //将读取的字节数组写入目标文件输出流中    }    fin.close(); //关闭输入流    fout.close(); //关闭输出流

62,629

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧