我的图像查看器出现问题,它要求我的文件选择器上有一个返回方法。该程序应该为猫打开一个图像。
我不知道如何修复语法错误。
我只是想通过图像查看器方法中的filechooser来解决问题,继续查看我是否有更多问题,我是GUI新手 ---- jGRASP exec:javac -g ImageViewerr.java
ImageViewerr.java:32:错误:方法声明无效;需要返回类型 private fileChooser() ^ 1错误
---- jGRASP wedge2:进程的退出代码为1。 ---- jGRASP:操作完成。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.*;
import java.io.File;
public class ImageViewerr extends JFrame
{
private JPanel imagePanel;
private JPanel buttonPanel;
private JLabel imageLabel;
private JButton button;
private JFileChooser fileChooser;
public ImageViewerr()
{
setTitle( "Image Viewer" );
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
buildImagePanel();
buildButtonPanel();
add(imagePanel, BorderLayout.CENTER);
add(buttonPanel,BorderLayout.SOUTH);
pack();
setLocationRelativeTo( null );
setVisible( true );
private fileChooser()
{
fileChooser = new JFileChooser( "." );
fileNameExtensionFilter defaultFilter = new FileNameExtensionFilter( "Image Files", "jpg"
,"gif", "png" );
fileChooser.addChoosableFileFilter( defaultFilter );
fileChooser.addChoosableFileFilter(
new FileNameExtensionFilter( "JPEG Files", "jpg" ) );
fileChooser.addChoosableFileFilter(
new FileNameExtensionFilter( "GIF Files", "gif" ) );
fileChooser.addChoosableFileFilter(
new FileNameExtensionFilter( "PNG Files", "png" ) );
fileChooser.setFileFilter( defaultFilter );
fileChooser.setDialogTitle( "Select Image File" );
}
}
private void buildImagePanel()
{
imagePanel = new JPanel();
imageLabel = new JLabel("Click here to " +
"get an image");
imagePanel.add(imageLabel);
}
private void buildButtonPanel()
{
buttonPanel = new JPanel();
menuItem.setMnemonic(KeyEvent.VK_G);
button = new JButton("Get Image");
button.addActionListener(new ButtonListener());
buttonPanel.add(button);
}
private class ButtonListener implements ActionListener
{
File selectedFile;
ImageIcon image;
String filename;
int fileChooserStatus;
public void actionPerformed( ActionEvent e )
{
fileChooserStatus = fileChooser.showOpenDialog( ImageViewer.this );
if( fileChooserStatus == JFileChooser.APPROVE_OPTION )
{
selectedFile = fileChooser.getSelectedFile();
filename = selectedFile.getPath();
image = new ImageIcon( filename );
imageLabel.setIcon( image );
imageLabel.setText( null );
pack();
}
}
}
public static void main( String[] args )
{
new ImageViewerr();
}
}