làm một điều trong số sau đây
Để sử dụng tạo phẩm thư mục đã phát nổ được xác định trước, hãy chọn chiến tranh war:exploded phát nổ từ danh sách trên ngăn bên trái. Nếu cần, hãy thay đổi tên và thư mục đầu ra của tạo tác trong các trường tương ứng trên khung bên phải.
Để tạo một thành phần phần mềm mới, hãy nhấp vào Mới trên ngăn bên trái và chọn loại thành phần phần mềm từ New list.
Để triển khai ứng dụng dưới dạng thư mục, hãy chọn Ứng dụng web. Đã phát nổ .
Để ứng dụng được triển khai ở dạng đóng gói, hãy chọn Ứng dụng web. Lưu trữ .
Trên ngăn bên phải, chỉ định cài đặt chung của tạo phẩm, chẳng hạn như tên và thư mục đầu ra, trong các trường tương ứng
Trình chọn tệp cung cấp GUI để điều hướng hệ thống tệp, sau đó chọn tệp hoặc thư mục từ danh sách hoặc nhập tên của tệp hoặc thư mục. Để hiển thị bộ chọn tệp, bạn thường sử dụng API
public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
4 để hiển thị hộp thoại phương thức chứa bộ chọn tệp. Một cách khác để trình bày bộ chọn tệp là thêm phiên bản của public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
4 vào vùng chứa
Ghi chú. Nếu bạn định phân phối chương trình của mình dưới dạng ứng dụng Java Web Start hộp cát, thì thay vì sử dụng API
public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
4, bạn nên sử dụng các dịch vụ tệp do API JNLP cung cấp. Các dịch vụ này — public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
7 và public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
8 — không chỉ cung cấp hỗ trợ cho việc chọn tệp trong môi trường bị hạn chế mà còn đảm nhiệm việc thực sự mở và lưu chúng. Một ví dụ về việc sử dụng các dịch vụ này là trong. Tài liệu về cách sử dụng API JNLP có thể được tìm thấy trong bài học Java Web Start
Nhấp vào nút Khởi chạy để chạy JWSFileChooserDemo bằng Java™ Web Start (tải xuống JDK 7 trở lên). Ngoài ra, để tự biên dịch và chạy ví dụ, hãy tham khảo tài liệu
Khi làm việc với ví dụ
public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
9, hãy cẩn thận để không làm mất các tệp bạn cần. Bất cứ khi nào bạn nhấp vào nút lưu và chọn một tệp hiện có, bản trình diễn này sẽ hiển thị hộp thoại Tệp tồn tại với yêu cầu thay thế tệp. Chấp nhận yêu cầu ghi đè lên tập tinPhần còn lại của phần này thảo luận về cách sử dụng API
public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
4. Đối tượng public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
4 chỉ trình bày GUI để chọn tệp. Chương trình của bạn chịu trách nhiệm thực hiện điều gì đó với tệp đã chọn, chẳng hạn như mở hoặc lưu tệp đó. Tham khảo Basic I/O để biết thông tin về cách đọc và ghi tệpAPI
public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
4 giúp dễ dàng hiển thị các hộp thoại đang mở và lưu. Kiểu giao diện xác định các hộp thoại tiêu chuẩn này trông như thế nào và chúng khác nhau như thế nào. Trong giao diện Java, hộp thoại lưu trông giống như hộp thoại mở, ngoại trừ tiêu đề trên cửa sổ của hộp thoại và văn bản trên nút phê duyệt thao tác. Đây là hình ảnh của hộp thoại mở tiêu chuẩn trong giao diện JavaĐây là hình ảnh của một ứng dụng có tên
int returnVal = fc.showSaveDialog(FileChooserDemo.this);
3 hiển thị hộp thoại mở và hộp thoại lưu
Thử cái này. - Biên dịch và chạy ví dụ, tham khảo tài liệu
- Nhấp vào nút Mở tệp. Điều hướng xung quanh bộ chọn tệp, chọn một tệp và nhấp vào nút Mở của hộp thoại
- Sử dụng nút Lưu tệp để hiển thị hộp thoại lưu. Cố gắng sử dụng tất cả các điều khiển trên bộ chọn tệp
- Trong tệp nguồn
int returnVal = fc.showSaveDialog(FileChooserDemo.this);
4, thay đổi chế độ chọn tệp thành chế độ chỉ thư mục. (Tìm kiếm int returnVal = fc.showSaveDialog(FileChooserDemo.this);
5 và bỏ ghi chú dòng chứa nó. ) Sau đó biên dịch và chạy lại ví dụ. Bạn sẽ chỉ có thể xem và chọn các thư mục, không phải các tệp thông thường
Hiển thị hộp thoại mở tiêu chuẩn chỉ cần hai dòng mã
public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
2Đối số của phương thức
int returnVal = fc.showSaveDialog(FileChooserDemo.this);
6 chỉ định thành phần cha cho hộp thoại. Thành phần cha ảnh hưởng đến vị trí của hộp thoại và khung mà hộp thoại phụ thuộc vào. Ví dụ: giao diện Java đặt hộp thoại trực tiếp trên thành phần chính. Nếu thành phần chính nằm trong một khung, thì hộp thoại phụ thuộc vào khung đó. Hộp thoại này biến mất khi khung được thu nhỏ và xuất hiện lại khi khung được phóng toTheo mặc định, trình chọn tệp chưa được hiển thị trước đó sẽ hiển thị tất cả các tệp trong thư mục chính của người dùng. Bạn có thể chỉ định thư mục ban đầu của trình chọn tệp bằng cách sử dụng một trong các hàm tạo khác của
public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
4 hoặc bạn có thể đặt thư mục bằng phương thức int returnVal = fc.showSaveDialog(FileChooserDemo.this);
8Lệnh gọi tới
int returnVal = fc.showSaveDialog(FileChooserDemo.this);
6 xuất hiện trong phương thức public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
70 của trình lắng nghe hành động của nút Mở tệppublic void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
Các phương thức
public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
71 trả về một số nguyên cho biết liệu người dùng có chọn một tệp hay không. Tùy thuộc vào cách bạn sử dụng bộ chọn tệp, việc kiểm tra xem giá trị trả về có phải là public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
72 hay không và sau đó không thay đổi bất kỳ giá trị nào khác là đủ. Để lấy tệp đã chọn (hoặc thư mục, nếu bạn thiết lập trình chọn tệp để cho phép chọn thư mục), hãy gọi phương thức public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
73 trên trình chọn tệp. Phương thức này trả về một thể hiện của public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
74Ví dụ lấy tên của tệp và sử dụng nó trong thông báo tường trình. Bạn có thể gọi các phương thức khác trên đối tượng ________ 174, chẳng hạn như ________ 176, ________ 177 hoặc ________ 178 để lấy thông tin về tệp. Bạn cũng có thể gọi các phương thức khác như
public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
79 và public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
80 để thay đổi tệp theo một cách nào đó. Tất nhiên, bạn cũng có thể muốn mở hoặc lưu tệp bằng cách sử dụng một trong các lớp trình đọc hoặc trình ghi do nền tảng Java cung cấp. Xem Basic I/O để biết thông tin về cách sử dụng trình đọc và ghi để đọc và ghi dữ liệu vào hệ thống tệpChương trình ví dụ sử dụng cùng một thể hiện của lớp
public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
4 để hiển thị hộp thoại lưu tiêu chuẩn. Lần này chương trình gọi public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
82int returnVal = fc.showSaveDialog(FileChooserDemo.this);
Bằng cách sử dụng cùng một phiên bản trình chọn tệp để hiển thị các hộp thoại mở và lưu, chương trình sẽ thu được những lợi ích sau
- Bộ chọn ghi nhớ thư mục hiện tại giữa các lần sử dụng, vì vậy các phiên bản mở và lưu sẽ tự động chia sẻ cùng một thư mục hiện tại
- Bạn chỉ phải tùy chỉnh một bộ chọn tệp và các tùy chỉnh áp dụng cho cả phiên bản mở và lưu
Cuối cùng, chương trình ví dụ có các dòng mã nhận xét cho phép bạn thay đổi chế độ chọn tệp. Ví dụ: dòng mã sau đây giúp trình chọn tệp chỉ có thể chọn thư mục chứ không phải tệp
public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
7Một chế độ lựa chọn khả thi khác là
public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
83. Mặc định là public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
84. Hình ảnh sau đây hiển thị hộp thoại đang mở với chế độ chọn tệp được đặt thành int returnVal = fc.showSaveDialog(FileChooserDemo.this);
5. Lưu ý rằng, ít nhất trong giao diện Java, chỉ các thư mục được hiển thị — không phải các tệpNếu bạn muốn tạo một bộ chọn tệp cho một tác vụ khác ngoài việc mở hoặc lưu hoặc nếu bạn muốn tùy chỉnh bộ chọn tệp, hãy tiếp tục đọc. Chúng ta sẽ thảo luận về các chủ đề sau
Chúng ta hãy xem ví dụ, một phiên bản sửa đổi của chương trình demo trước đó sử dụng nhiều API
public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
4 hơn. Ví dụ này sử dụng bộ chọn tệp đã được tùy chỉnh theo nhiều cách. Giống như ví dụ ban đầu, người dùng gọi trình chọn tệp bằng cách nhấn nút. Đây là hình ảnh của trình chọn tệpNhư hình minh họa, trình chọn tệp này đã được tùy chỉnh cho một tác vụ đặc biệt (Đính kèm), cung cấp bộ lọc tệp mà người dùng có thể chọn (Chỉ Hình ảnh), sử dụng chế độ xem tệp đặc biệt cho các tệp hình ảnh và có một thành phần phụ kiện hiển thị bản phác thảo hình thu nhỏ
Phần còn lại của phần này hiển thị cho bạn mã tạo và tùy chỉnh trình chọn tệp này. Xem các liên kết cho tất cả các tệp theo yêu cầu của ví dụ này
Như bạn đã thấy, lớp
public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
4 cung cấp phương thức int returnVal = fc.showSaveDialog(FileChooserDemo.this);
6 để hiển thị hộp thoại mở và phương thức public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
82 để hiển thị hộp thoại lưuLớp này có một phương thức khác,
public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
01, để hiển thị bộ chọn tệp cho tác vụ tùy chỉnh trong hộp thoại. Trong giao diện Java, sự khác biệt duy nhất giữa hộp thoại này và các hộp thoại chọn tệp khác là tiêu đề trên cửa sổ hộp thoại và nhãn trên nút phê duyệt. Đây là mã từ public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
86 hiển thị hộp thoại chọn tệp cho tác vụ Đính kèmpublic void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
8Đối số đầu tiên của phương thức
public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
01 là thành phần chính của hộp thoại. Đối số thứ hai là đối tượng public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
04 cung cấp cả tiêu đề cho cửa sổ hộp thoại và nhãn cho nút phê duyệtMột lần nữa, trình chọn tệp không làm gì với tệp đã chọn. Chương trình chịu trách nhiệm thực hiện tác vụ tùy chỉnh mà bộ chọn tệp đã được tạo
Theo mặc định, trình chọn tệp hiển thị tất cả các tệp và thư mục mà nó phát hiện, ngoại trừ các tệp ẩn. Một chương trình có thể áp dụng một hoặc nhiều bộ lọc tệp cho bộ chọn tệp để bộ chọn chỉ hiển thị một số tệp. Trình chọn tệp gọi phương thức
public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
05 của bộ lọc cho mỗi tệp để xác định xem nó có được hiển thị hay không. Bộ lọc tệp chấp nhận hoặc từ chối tệp dựa trên các tiêu chí như loại tệp, kích thước, quyền sở hữu, v.v. Bộ lọc ảnh hưởng đến danh sách tệp được hiển thị bởi trình chọn tệp. Người dùng có thể nhập tên của bất kỳ tệp nào ngay cả khi nó không được hiển thịpublic void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
4 hỗ trợ ba loại lọc khác nhau. Các bộ lọc được kiểm tra theo thứ tự được liệt kê ở đây. Ví dụ: bộ lọc do ứng dụng kiểm soát chỉ xem những tệp được bộ lọc tích hợp sẵn chấp nhậnBộ lọc tích hợp Bộ lọc được thiết lập thông qua các lệnh gọi phương thức cụ thể trên bộ chọn tệp. Hiện tại, bộ lọc tích hợp sẵn duy nhất dành cho các tệp ẩn, chẳng hạn như những tệp có tên bắt đầu bằng dấu chấm (. ) trên hệ thống UNIX. Theo mặc định, các tệp ẩn không được hiển thị. Gọi public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
07 để hiển thị các tệp ẩn. Lọc do ứng dụng kiểm soátỨng dụng xác định tệp nào được hiển thị. Tạo một lớp con tùy chỉnh của public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
08, khởi tạo nó và sử dụng thể hiện đó làm đối số cho phương thức public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
09. Bộ lọc đã cài đặt được hiển thị trên danh sách các bộ lọc do người dùng lựa chọn. Bộ chọn tệp chỉ hiển thị những tệp mà bộ lọc chấp nhận. Lọc do người dùng lựa chọnGUI của trình chọn tệp cung cấp danh sách các bộ lọc mà người dùng có thể chọn. Khi người dùng chọn một bộ lọc, bộ chọn tệp chỉ hiển thị những tệp được bộ lọc đó chấp nhận. public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
86 thêm bộ lọc tệp tùy chỉnh vào danh sách bộ lọc người dùng có thể chọnpublic void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
0Theo mặc định, danh sách các bộ lọc người dùng có thể chọn bao gồm bộ lọc Chấp nhận tất cả, cho phép người dùng xem tất cả các tệp không bị ẩn. Ví dụ này sử dụng đoạn mã sau để tắt bộ lọc Chấp nhận tất cảpublic void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
2Bộ lọc tệp tùy chỉnh của chúng tôi được triển khai trong public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
21 và là lớp con của public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
08. Lớp public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
23 triển khai phương thức public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
24 để trả về "Chỉ hình ảnh" — một chuỗi để đưa vào danh sách bộ lọc do người dùng lựa chọn. public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
23 cũng triển khai phương pháp public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
05 để nó chấp nhận tất cả các thư mục và bất kỳ tệp nào có phần mở rộng tên tệp public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
27, public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
28, public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
29, public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
20, public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
21 hoặc public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
22public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
2Bằng cách chấp nhận tất cả các thư mục, bộ lọc này cho phép người dùng điều hướng xung quanh hệ thống tệp. Nếu các dòng in đậm bị bỏ qua trong phương thức này, người dùng sẽ bị giới hạn trong thư mục mà bộ chọn được khởi tạoMẫu mã trước sử dụng phương thức
public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
23 và một số hằng chuỗi từ public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
24, được hiển thị tại đâypublic void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
8Trong giao diện Java, danh sách của bộ chọn hiển thị tên của từng tệp và hiển thị một biểu tượng nhỏ cho biết tệp đó là tệp thực hay thư mục. Bạn có thể tùy chỉnh chế độ xem tệp này bằng cách tạo một lớp con tùy chỉnh của
public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
25 và sử dụng một thể hiện của lớp làm đối số cho phương thức public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
26. Ví dụ này sử dụng một thể hiện của lớp tùy chỉnh, được triển khai trong public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
27, làm chế độ xem tệp của trình chọn tệppublic void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
2Lớp
public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
28 hiển thị một biểu tượng khác nhau cho từng loại hình ảnh được chấp nhận bởi bộ lọc hình ảnh được mô tả trước đóLớp
public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
28 ghi đè năm phương thức trừu tượng được định nghĩa trong public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
25 như saupublic void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
81Trả về mô tả của loại tệp. Đây là triển khai của phương pháp này của public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
28public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
0public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
83Trả về một biểu tượng đại diện cho tệp hoặc loại tệp. Đây là triển khai của phương pháp này của public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
28public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
1public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
85Trả về tên của tệp. Hầu hết các triển khai của phương pháp này sẽ trả về public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
86 để chỉ ra rằng giao diện sẽ tìm ra nó. Một triển khai phổ biến khác trả về public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
87. public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
88Trả về mô tả của tệp. Mục đích là để mô tả các tệp riêng lẻ cụ thể hơn. Một triển khai phổ biến của phương pháp này trả về public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
86 để cho biết rằng giao diện sẽ tìm ra nó. public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
20Trả về liệu một thư mục có thể duyệt được hay không. Hầu hết các triển khai của phương pháp này sẽ trả về public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
86 để chỉ ra rằng giao diện sẽ tìm ra nó. Một số ứng dụng có thể muốn ngăn người dùng truy cập vào một loại thư mục nhất định vì nó đại diện cho một tài liệu phức hợp. Phương thức public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
22 sẽ không bao giờ trả về public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
23 cho một thư mục khôngTrình chọn tệp tùy chỉnh trong
public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
86 có một thành phần phụ kiện. Nếu mục hiện được chọn là hình ảnh PNG, JPEG, TIFF hoặc GIF, thành phần phụ kiện sẽ hiển thị bản phác thảo hình thu nhỏ của hình ảnh. Nếu không, thành phần phụ kiện trống. Ngoài trình xem trước, có lẽ cách sử dụng phổ biến nhất cho thành phần phụ kiện là một bảng điều khiển có nhiều điều khiển hơn, chẳng hạn như các hộp kiểm chuyển đổi giữa các tính năngVí dụ này gọi phương thức
public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
25 để thiết lập một thể hiện của lớp public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
26, được triển khai trong public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
27, làm thành phần phụ kiện của bộ chọnpublic void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
2Bất kỳ đối tượng nào kế thừa từ lớp
public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
28 đều có thể là một thành phần phụ kiện. Thành phần phải có kích thước ưa thích trông đẹp mắt trong bộ chọn tệpTrình chọn tệp kích hoạt sự kiện thay đổi thuộc tính khi người dùng chọn một mục trong danh sách. Một chương trình có thành phần phụ kiện phải đăng ký để nhận các sự kiện này nhằm cập nhật thành phần phụ kiện bất cứ khi nào lựa chọn thay đổi. Trong ví dụ, đối tượng
public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
26 tự đăng ký các sự kiện này. Điều này giữ tất cả các mã liên quan đến thành phần phụ kiện cùng nhau trong một lớpDưới đây là ví dụ triển khai phương thức
public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
00, đây là phương thức được gọi khi một sự kiện thay đổi thuộc tính được kích hoạtpublic void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
3Nếu
public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
01 là thuộc tính đã thay đổi, phương thức này lấy đối tượng public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
74 từ bộ chọn tệp. Các phương thức public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
03 và public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
04 sử dụng đối tượng public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
74 để tải hình ảnh và sơn lại thành phần phụ kiệnAPI để sử dụng trình chọn tệp thuộc các danh mục này
Bảng này hiển thị các ví dụ sử dụng bộ chọn tệp và chỉ ra nơi các ví dụ đó được mô tả