Cześć
oto mój program:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class ARTlogo
{
public static void main(String[] args)
{
JFrame frame = new ImageViewerFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class ImageViewerFrame extends JFrame
{
public ImageViewerFrame()
{
setTitle("ARTlogo");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
// use a label to display the images
label = new JLabel();
add(label);
// set up the file chooser
chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
choser = new JFileChooser();
choser.setCurrentDirectory(new File("."));
// set up the menu bar
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu menu = new JMenu("Plik");
menuBar.add(menu);
JMenu me = new JMenu("Edycja");
menuBar.add(me);
JMenuItem openItem = new JMenuItem("Otwórz");
menu.add(openItem);
openItem.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
// show file chooser dialog
int result = chooser.showOpenDialog(null);
// if file selected, set it as icon of the label
if (result == JFileChooser.APPROVE_OPTION)
{
String name = chooser.getSelectedFile().getPath();
label.setIcon(new ImageIcon(name));
}
}
});
JMenuItem exitItem = new JMenuItem("Wyjście");
menu.add(exitItem);
exitItem.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
});
JMenuItem penItem = new JMenuItem("Zmiana postaci");
me.add(penItem);
openItem.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
// show file chooser dialog
int result = choser.showOpenDialog(null);
// if file selected, set it as icon of the label
if (result == JFileChooser.APPROVE_OPTION)
{
String name = choser.getSelectedFile().getPath();
label.setIcon(new ImageIcon(name));
}
}
});
}
private JLabel label;
private JFileChooser chooser;
private JFileChooser choser;
private static final int DEFAULT_WIDTH = 700;
private static final int DEFAULT_HEIGHT = 500;
}
Błąd polega na tym, że gdy kliknę przycisk "zmiana postaci" nic się nie dzieje, a ma się włączyć okno wyboru pliku.
Z kolei gdy kliknę przycisk "Otwórz" otwiera mi się okno i wszystko działa.
Jak to naprawić?