Javaで画像を使って何かを作るために、Java画像パネルを書いています。どうすれば物事をより良くできるかを確認するのは、最初のショットです。
そのため、これらの 3 つのクラスを作成しましたが、問題は、何も表示されていないクラスでクラスDiaporama
(以下のコードを参照)を使用する場合ですMainWindow
。しかしpublic static void main (String [] args)
、クラス Diaporama で を使用すると、すべてが機能します。
/**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 クラス:
/** 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
}
}