//導入包
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import static java.awt.BorderLayout.*;
import javax.swing.JScrollPane;
import java.awt.ScrollPane;
import java.awt.*;
import java.awt.Color.*;
import javax.swing.BorderFactory;
import java.awt.Label.*;
import javax.swing.JSplitPane.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JFileChooser.*;
import javax.swing.JLabel.*;
public class JieMian
{
//定義組件
JFrame jf = new JFrame("FTP文件傳輸軟件");
JPanel jl=new JPanel();
JPanel jl1=new JPanel();
JPanel jl2=new JPanel();
JPanel jl3=new JPanel();
JPanel jl4=new JPanel();
JPanel jl5=new JPanel();
JPanel jl6=new JPanel();
JPanel jl7=new JPanel();
JPanel jl8=new JPanel();
JPanel jl9=new JPanel();
JPanel jl10=new JPanel();
JButton jb=new JButton("連接");
JButton jb1=new JButton("文件");
JButton jb2=new JButton("大小");
JButton jb3=new JButton("類型");
JButton jb4=new JButton("修改日期");
JButton jb5=new JButton("名稱");
JButton jb6=new JButton("大小");
JButton jb7=new JButton("類型");
JButton jb8=new JButton("修改日期");
JButton jb9=new JButton("權限");
JButton jb10=new JButton("所屬用戶/組");
JTextField jtf=new JTextField(20);
JTextField jtf1=new JTextField(10);
JTextField jtf2=new JTextField(10);
JTextField jtf3=new JTextField(5);
JTextField jtf4=new JTextField(31);
JTextField jtf5=new JTextField(31);
JTextArea jta =new JTextArea(20,30);
JScrollPane jsp=new JScrollPane();
public void init()
{
// 為按鈕添加背景顏色
jb.setBackground(Color.GRAY);
//設置jl面板的組建排列的方式
jl.setLayout(new BorderLayout(30,5));
//設置邊框
jl1.setBorder(BorderFactory.createLoweredBevelBorder());
jl4.setBorder(BorderFactory.createRaisedBevelBorder());
jl3.setBorder(BorderFactory.createEtchedBorder());
jl3.setPreferredSize(new Dimension(400, 500));
jl4.setPreferredSize(new Dimension(400, 500));
jf.add(jl);
jl.add(jl1,NORTH);
jl.add(jl2,SOUTH);
jl2.setLayout(new BorderLayout());
jl2.add(jl3,WEST);
jl2.add(jl4,EAST);
jl3.setLayout(new BorderLayout());
jl3.add(jl5,NORTH);
jl5.setLayout(new FlowLayout());
jl5.add(new Label("本地"));
jl5.add(jtf4);
jl4.setLayout(new BorderLayout());
jl4.add(jl6,NORTH);
jl6.setLayout(new FlowLayout());
jl6.add(new Label("遠程"));
jl6.add(jtf5);
jl3.add(jl7,SOUTH);
jl7.setBorder(BorderFactory.createRaisedBevelBorder());
jl7.setPreferredSize(new Dimension(400, 470));
jl7.setLayout(new BorderLayout());
jl7.add(jl8,NORTH);
jl8.setLayout(new FlowLayout());
jl8.add(jb1);
jl8.add(jb2);
jl8.add(jb3);
jl8.add(jb4);
jb1.setBackground(Color.lightGray);
jb2.setBackground(Color.lightGray);
jb3.setBackground(Color.lightGray);
jb4.setBackground(Color.lightGray);
jb5.setBackground(Color.lightGray);
jb6.setBackground(Color.lightGray);
jb7.setBackground(Color.lightGray);
jb8.setBackground(Color.lightGray);
jb9.setBackground(Color.lightGray);
jb10.setBackground(Color.lightGray);
jb1.setPreferredSize(new Dimension(100, 25));
jb2.setPreferredSize(new Dimension(80, 25));
jb3.setPreferredSize(new Dimension(80, 25));
jb4.setPreferredSize(new Dimension(100, 25));
jb5.setPreferredSize(new Dimension(60, 25));
jb6.setPreferredSize(new Dimension(60, 25));
jb7.setPreferredSize(new Dimension(60, 25));
jb8.setPreferredSize(new Dimension(60, 25));
jb9.setPreferredSize(new Dimension(60, 25));
jb10.setPreferredSize(new Dimension(80, 25));
jl4.add(jl9,SOUTH);
jl9.setPreferredSize(new Dimension(400, 470));
jl9.setLayout(new BorderLayout());
jl9.add(jl10,NORTH);
jl9.add(jsp);
jl9.setLayout(new FlowLayout());
jl10.add(jb5);
jl10.add(jb6);
jl10.add(jb7);
jl10.add(jb8);
jl10.add(jb9);
jl10.add(jb10);
jl1.setLayout(new FlowLayout());
//將組件添加到JL1面板上面
jl1.add(new Label("地址欄"));
jl1.add(jtf);
jl1.add(new Label("用戶名"));
jl1.add(jtf1);
jl1.add(new Label("密碼"));
jl1.add(jtf2);
jl1.add(new Label("端口"));
jl1.add(jtf3);
jl1.add(jb);
jb1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JFileChooser jfc=new JFileChooser();
jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
jfc.showDialog(new JLabel(),"選擇");
File file=jfc.getSelectedFile();
if(file.isDirectory()){
System.out.println("文件夾:"+file.getAbsolutePath());
}else if(file.isFile()){
System.out.println("文件:"+file.getAbsolutePath());
}
System.out.println(jfc.getSelectedFile().getName());
}
});
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.pack();
jf.setVisible(true);
}
public static void main(String[] args)
{
new JieMian().init();
}
}
---------- 編譯Java程序 ----------
JieMian.java:142: 錯誤: 找不到符號
JFileChooser jfc=new JFileChooser();
^
符號: 類 JFileChooser
JieMian.java:142: 錯誤: 找不到符號
JFileChooser jfc=new JFileChooser();
^
符號: 類 JFileChooser
JieMian.java:143: 錯誤: 找不到符號
jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
^
符號: 變量 JFileChooser
JieMian.java:144: 錯誤: 找不到符號
jfc.showDialog(new JLabel(),"選擇");
^
符號: 類 JLabel
4 個錯誤
輸出完成 (耗時 0 秒) - 正常終止
import javax.swing.JFileChooser.*;
這個import改成import javax.swing.JFileChooser;