Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.BorderLayout;
- import java.awt.FlowLayout;
- import java.awt.Font;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.File;
- import java.io.IOException;
- import java.util.ArrayList;
- import javax.annotation.processing.FilerException;
- import javax.swing.ImageIcon;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- public class DemoFile extends JFrame implements ActionListener {
- String path = getClass().getProtectionDomain().getCodeSource()
- .getLocation().getPath();
- // when use jar file
- String absolutePathJarFile = path.substring(0, path.lastIndexOf("/"))
- + "/duoi hinh bat chu";
- // when run in project
- String absolutePath = "duoi hinh bat chu";
- private File file = new File(absolutePathJarFile);
- private JLabel label, dapan;
- private JButton btNext, btPrev, btDapAn;
- private ArrayList<String> list = new ArrayList<String>();
- private ArrayList<String> listName = new ArrayList<String>();
- private int count = -1;
- public DemoFile() {
- btNext = createButton("Next");
- btPrev = createButton("Prev");
- btDapAn = createButton("Result");
- dapan = new JLabel("Dap An");
- dapan.setFont(new Font("arial", Font.BOLD, 30));
- label = new JLabel("");
- JPanel panel = new JPanel(new FlowLayout());
- JPanel panel1 = new JPanel(new FlowLayout());
- panel1.add(label);
- panel.add(btPrev);
- panel.add(btNext);
- panel.add(btDapAn);
- this.setLayout(new BorderLayout());
- this.add(dapan, BorderLayout.NORTH);
- this.add(panel1, BorderLayout.CENTER);
- this.add(panel, BorderLayout.SOUTH);
- this.setSize(600, 500);
- this.setVisible(true);
- }
- private JButton createButton(String s) {
- JButton bt = new JButton(s);
- bt.addActionListener(this);
- return bt;
- }
- private ImageIcon createIcon(String name) {
- String pathImage = file.getAbsolutePath() + File.separator + name;
- System.out.println(pathImage);
- return new ImageIcon(file.getAbsolutePath() + File.separator + name);
- }
- public void listDirectory(File file) throws FilerException {
- if (!file.exists()) { // kiểm tra xem đường dẫn có tồn tại hay không
- System.out.print("Duong dan khong ton tai");
- return;
- }
- if (file.isDirectory()) {
- String[] listDirectoryChild = file.list();
- for (int i = 0; i < listDirectoryChild.length; i++) {
- listDirectory(new File(file.getPath(), listDirectoryChild[i]));
- listName.add(listDirectoryChild[i]);
- }
- return;
- }
- if (file.isFile()) {
- try {
- list.add(file.getCanonicalPath());
- } catch (IOException e) {
- e.printStackTrace();
- }
- return;
- }
- return;
- }
- public static void main(String args[]) {
- DemoFile demoFile = new DemoFile();
- System.out.println(demoFile.file.getAbsolutePath());
- try {
- demoFile.listDirectory(demoFile.file);
- } catch (FilerException e) {
- e.printStackTrace();
- }
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- if (e.getSource() == btNext) {
- if (++count < list.size()) {
- label.setIcon(createIcon(listName.get(count)));
- dapan.setText("");
- }
- }
- if (e.getSource() == btPrev) {
- if (count > 0) {
- label.setIcon(createIcon(listName.get(--count)));
- dapan.setText("");
- }
- }
- if (e.getSource() == btDapAn) {
- String s = listName.get(count).substring(0,
- listName.get(count).indexOf("."));
- dapan.setText(s);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement