请知道的帮忙解答下,我想从fc中传过来选定的文件路径和名称,并显示在文本框中,请大家帮忙看看为什么显示不出来啊。
package zuoye;
import javax.swing.*;import java.io.*;import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class FeiGe implements ActionListener{
JFrame jf=new JFrame("文件分割器");
JTextField jtf=new JTextField(30);
JFileChooser fc = new JFileChooser();
public FeiGe(){
JButton jb=new JButton("分割");
JButton jb1=new JButton("合并");
JButton jb2=new JButton("open");
jf.setLayout(new GridLayout(2,2));
JTextField jtf=new JTextField(30);
jb2.addActionListener(this);
jf.add(jtf);
jf.add(jb2);
jf.add(jb);
jf.add(jb1);
jf.setSize(400, 100);
jf.setVisible(true);
jf.setLocation(400, 200);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new FeiGe();
}
public void actionPerformed(ActionEvent e) {
String comm = e.getActionCommand();
File f1 = null;
if (comm.equals("open")) {
fc.showOpenDialog(fc);
fc.setVisible(true);
}
f1=fc.getSelectedFile();
jtf.setText(f1.getPath());
}}
你将这句代码改成jtf.setText("Test");
那个文本内容都不会改变。、我也不知道为什么、、
JTextField jtf=new JTextField(30);
去掉就可以咯、、
package zuoye;
import javax.swing.*; import java.io.*; import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class FeiGe implements ActionListener{
JFrame jf=new JFrame("文件分割器");
JTextField jtf=new JTextField(30); //这里已经声明,所以下面的去掉就可以了
JFileChooser fc = new JFileChooser();
public FeiGe(){
JButton jb=new JButton("分割");
JButton jb1=new JButton("合并");
JButton jb2=new JButton("open");
jf.setLayout(new GridLayout(2,2));
JTextField jtf=new JTextField(30); //上面已经声明,所以这里去掉就可以了
jb2.addActionListener(this);
jf.add(jtf);
jf.add(jb2);
jf.add(jb);
jf.add(jb1);
jf.setSize(400, 100);
jf.setVisible(true);
jf.setLocation(400, 200);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new FeiGe(); }
public void actionPerformed(ActionEvent e) {
String comm = e.getActionCommand();
File f1 = null;
if (comm.equals("open")) {
fc.showOpenDialog(fc);
fc.setVisible(true); }
f1=fc.getSelectedFile();
jtf.setText(f1.getPath());
}}
package zuoye;
import javax.swing.*; import java.io.*; import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class FeiGe implements ActionListener{
JFrame jf=new JFrame("文件分割器");
JTextField jtf=new JTextField(30); //这里已经声明,所以下面的去掉就可以了
JFileChooser fc = new JFileChooser();
public FeiGe(){
JButton jb=new JButton("分割");
JButton jb1=new JButton("合并");
JButton jb2=new JButton("open");
jf.setLayout(new GridLayout(2,2));
JTextField jtf=new JTextField(30); //上面已经声明,所以这里去掉就可以了
jb2.addActionListener(this);
jf.add(jtf);
jf.add(jb2);
jf.add(jb);
jf.add(jb1);
jf.setSize(400, 100);
jf.setVisible(true);
jf.setLocation(400, 200);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new FeiGe(); }
public void actionPerformed(ActionEvent e) {
String comm = e.getActionCommand();
File f1 = null;
if (comm.equals("open")) {
fc.showOpenDialog(fc);
fc.setVisible(true); }
f1=fc.getSelectedFile();
jtf.setText(f1.getPath());
}}
不好意思没发好
}}[/code]