在 ActionListener 中调用对象不起作用,但在对象类中工作 main



我正在编写一个java图像面板,用java中的图像制作一些东西。这是第一枪,看看我怎样才能把事情做得更好。

所以我写了这三个类,问题是当我在类中使用类Diaporama(您可以看到下面的代码)时MainWindow我什么都没有显示。但是,如果我在 Diaporama 类中使用public static void main (String [] args),一切都在工作。

/**RechercheFichier.java*/
//this class while return an ArrayList of String which contain all file path selected
public class RechercheFichier {
    private JFileChooser myFileChooser;
    private ArrayList<String> FileList;
    public RechercheFichier() {
        myFileChooser = new JFileChooser();
        myFileChooser.setCurrentDirectory(new File("."));
        myFileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        try {
            int value = myFileChooser.showOpenDialog(null);
            if (value == JFileChooser.APPROVE_OPTION) {
                File SelectedFile = myFileChooser.getSelectedFile();
                String Filename = SelectedFile.getPath();
                FileList = new ArrayList<String>();
                if (SelectedFile.isDirectory()) {
                    String[] myFile = SelectedFile.list();
                    for (int i = 0; i < myFile.length; i++) {
                        if (myFile[i].endsWith(".png") == true
                                || myFile[i].endsWith(".jpg") == true
                                || myFile[i].endsWith(".jpeg") == true) {
                            FileList.add(Filename + "/" + myFile[i]);
                        }
                    }
                } else
                    FileList.add(Filename);
            } else
                JOptionPane.showMessageDialog(null,
                        "User did not choose a file.");
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.getMessage(), "Error",
                    JOptionPane.ERROR_MESSAGE);
        }
    }
    //getter and setter
    public ArrayList<String> getFileList() {
        return FileList;
    }
    public void setFileList(ArrayList<String> fileList) {
        FileList = fileList;
    }

透析类:

/**Diaporama.java*/ 
public class Diaporama extends JFrame {
    private JFrame frame;
    private JPanel panel;
    private JLabel lab;
    public Diaporama() {
        frame =  new JFrame();
        panel = new JPanel();
        lab = null;
        frame.setTitle("Diaporama");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        RechercheFichier file = new RechercheFichier();
        ArrayList<String> toto = file.getFileList(); //toto contain all file path
        //Collections.sort(toto);
        for (int i = 0; i < toto.size(); i++) {
            panel = new JPanel();
            frame.setLayout(new GridLayout(1,1));
            frame.setVisible(true);
            frame.setSize(600, 500);
            frame.setLocation(200, 200);
            // this.image = getToolkit().getImage(toto.get(i));
            System.out.println(toto.get(i));
            this.lab = new JLabel(new ImageIcon(toto.get(i)));
            panel.add(lab);
            frame.setContentPane(panel);
            try {
                Thread.sleep(2000L);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            // imagePanel.remove(imagePanel);
            panel.remove(lab);
            panel.revalidate();
            frame.pack();
            frame.repaint();
            panel = null;
        }
        frame.dispose();
    }
    //public static void main(String [] args)
    //{
    //  new Diaporama();
    //}

主窗口类:

/** MainWindow.java**/
public class MainWindow extends JFrame implements ActionListener{
    private JFrame frame;
    private JButton buttonDiapo, buttonOpen, buttonSearch, buttonTag, buttonAlbum, buttonQuit;
    private JPanel panel;
    public JFrame getFrame()
    {
        return frame;
    }
    public MainWindow()
    {
            frame = new JFrame("The best Jphoto");
            frame.setSize(300, 400);
            frame.setLocation(200, 200);
            buttonDiapo = new JButton("Diaporama");
            buttonOpen = new JButton("Ouvrir Photo");
            buttonQuit = new JButton ("Quitter");
            buttonSearch = new JButton("Recherche par tag");
            buttonTag = new JButton("Add tag");
            buttonAlbum = new JButton("Creer un album");
            panel = new JPanel();
            panel.setLayout(new FlowLayout());
            panel.add(buttonOpen);
            panel.add(buttonDiapo);
            panel.add(buttonAlbum);
            panel.add(buttonSearch);
            panel.add(buttonTag);
            panel.add(buttonQuit);
            frame.getContentPane().add(panel, BorderLayout.CENTER);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setVisible(true);
            //ajout du action Listener pour le bouton Open
            buttonOpen.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    // new OpenImg();
                }
            });
            //ajout du action Listener pour le bouton Diapo
            buttonDiapo.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                              //the problem is here
                     new Diaporama();
                }
            });
            buttonQuit.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }
            });
            buttonAlbum.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    // new Album();
                }
            });
            buttonSearch.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    // new Search();
                }
            });
            buttonTag.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    // new Tag();
                }
            });
        }

        public static void main(String[] args) {
            // Set the look and feel to Java Swing Look
            new MainWindow();
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
        }
}
 public static void main (String [] args) 

提供编程的入口点。没有它,什么都跑不了。

在 Java 语言中,当您使用 Java 解释器执行类时,运行时系统首先调用该类的 main() 方法。然后,main() 方法调用运行应用程序所需的所有其他方法。

有关更多信息,请点击此链接 http://wiki.answers.com/Q/What_is_the_use_of_main_method_in_java

每次

单击按钮时弹出JFrame s的效率有点低。您是否考虑过使用 JDialogs?

但是,如果您真的想要解决问题,您可以尝试:

 Diaporama diaporamaFrame = new Diaporama();
 diaporamaFrame.setVisible(true);

把它贴在你的听众身上。祝你好运。

我终于改变了 Diaporama 类,我把它实现为

线程的实例
。奇怪的是,将
Diaporama实现为Runnable并将其作为线程进行午餐,使其可以工作
。但是,我仍然不明白为什么第一个版本不起作用。我的一些老师告诉我java图形线程,不会刷新框架,或者类似的东西。

谢谢你的帮助

我在这里粘贴我的新代码。

import java.util.ArrayList;
@SuppressWarnings("serial")
public class DiapoThread implements Runnable {
    Thread thread;
    ArrayList<String> toto = null;
    public void init() {
        RechercheFichier file = new RechercheFichier();
        file.RechercheFichierFileOrDirectory();
        toto = file.getFileList();
    }
    public void start() {
        (thread = new Thread(this)).start();
    }
    public void stop() {
        thread = null;
    }
    public void run() {
        this.init();
        new DiaporamaView(toto);
        this.stop();
    }
}

透视镜.java
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class DiaporamaView extends JFrame{
    private int iframe = 0;
    private JFrame frame = null;
    private JPanel panel = null;
    private JLabel lab = null;
    private ArrayList<String> toto = null;
    public DiaporamaView(ArrayList<String> list)
    {
        ArrayList<String> toto = list;
        long delay = 2500L;
        frame = new JFrame();
        panel = new JPanel();
        JPanel panBoutton = new JPanel();
        frame.setTitle("Diaporama");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setLayout(new BorderLayout());
        frame.add(panel, BorderLayout.CENTER);
        panBoutton.setLayout(new GridLayout(1,1));
        JButton fermer = new JButton("fermer fenetre");
        fermer.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                frame.dispose();
            }
        });
        panBoutton.add(fermer);
        this.frame.setVisible(true);
        this.panel.setBackground(Color.GRAY);
        frame.setLocation(100, 200);
        try {
            while (iframe < toto.size()) {
                if (iframe == 0)
                frame.setSize(400, 450);
                else if (iframe % 2 == 0)
                    frame.setSize(401, 451);
                else
                    frame.setSize(399, 449);
                Image img = ImageIO.read(new File(toto.get(iframe)));
//              redimensionner les images, 
                int width = img.getWidth(null);
                int height = img.getHeight(null);
                int new_height = 448;
                int new_width = (width / height) * new_height; 
                ImageIcon ii = new ImageIcon(img.getScaledInstance(new_width, new_height, img.SCALE_FAST));
                this.lab = new JLabel(ii);
                panel.add(lab);
                frame.add(panBoutton, BorderLayout.SOUTH);
                //repaint();
                Thread.sleep(delay);
                panel.remove(lab);
                panel.revalidate();
                iframe++;
//              frame.pack();
                 frame.repaint();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

最新更新