2013-03-24 36 views
0

我想向我的照片查看器添加一个滚动条,但它给了我一个错误,即无法从静态上下文中引用非静态变量。'非静态变量不能是...'错误

确切地说,我试图给JPanel添加一个滚动条。此外,如果我让JScrollPane scrollBar一个静态变量,那么照片将不会出现。 TIA

import java.awt.Container; 
import java.awt.Image; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.GridBagLayout; 
import java.awt.GridLayout; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.FilenameFilter; 
import java.io.IOException; 
import javax.imageio.ImageIO; 
import javax.swing.DefaultListModel; 
import javax.swing.filechooser.FileNameExtensionFilter; 
import javax.swing.JFileChooser; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JMenu; 
import javax.swing.JMenuBar; 
import javax.swing.JMenuItem; 
import javax.swing.JOptionPane; 
import javax.swing.JPanel; 
import javax.swing.ImageIcon; 
import javax.swing.SwingUtilities; 
import javax.swing.JScrollPane; 


public class PhotoViewer 
{ 
// Instance fields. 
private FilenameFilter fileNameFilter; 
private JFileChooser fileChooser; 
private JMenuBar menuBar; 
private JPanel mainPanel; 
private static JScrollPane scrollBar; 

public PhotoViewer() // Constructor. 
{ 
    // Main JPanel with a grid style layout. 
    mainPanel = new JPanel(new GridLayout()); 

    // Jlabel to display photo on. 
    final JLabel imageView = new JLabel(); 
    // Adds the JLabel ontop of the JPanel. 
    mainPanel.add(imageView); 

    // Adds a scroll bar. 
    scrollBar = new JScrollPane(mainPanel);  
    scrollBar.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 
    scrollBar.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);  

    // Creates a file chooser to find a photo. 
    fileChooser = new JFileChooser(); 

    // Creates a new menubar at the top of the JPanel. 
    menuBar = new JMenuBar(); 
    // Adds a menu within the JMenuBar. 
    JMenu menu = new JMenu("View new photo"); 
    // Adds the additional menu ontop of the original JMenuBar. 
    menuBar.add(menu); 
    // Option to browse for a new photo. 
    JMenuItem browse = new JMenuItem("Browse"); 
    // Adds the browse option ontop of the 'View new photo' button. 
    menu.add(browse); 

    // Creates an actionlistener to follow what the user is doing. 
    browse.addActionListener(new ActionListener() 
     { 

      public void actionPerformed(ActionEvent ae) 
      { 
       int result = fileChooser.showOpenDialog(mainPanel); 
       // Displays the image if approved by JFileChooser. 
       if (result==JFileChooser.APPROVE_OPTION) 
       { 
        // Obtains the selected file by the user. 
        File singleImage = fileChooser.getSelectedFile(); 
        try 
        { 
         // Displays the image if no exception. 
         Image displayImage = ImageIO.read(singleImage); 
         imageView.setIcon(new ImageIcon(displayImage)); 
        } catch(Exception e)       
        { 
         // Displays the exception caught by the program in a JOptionPane window. 
         e.printStackTrace(); 
         JOptionPane.showMessageDialog(mainPanel, e, "Load failure!", JOptionPane.ERROR_MESSAGE); 
        } 
       } 
      } 
     }); 

} // end of constructor PhotoViewer 

public void loadImages(File directory) throws IOException 
{ 
    // Throws an exception to be caught. 
    File[] imageFiles = directory.listFiles(fileNameFilter); 
    BufferedImage[] images = new BufferedImage[imageFiles.length]; 
} // end of method loadImages(File directory) 

public Container getPanel() 
{ 
    // Hands execution back to the mainPanel function. 
    return mainPanel; 
}// end of method getPanel() 

public JMenuBar getMenuBar() 
{ 
    // Hands execution back to the menuBar function. 
    return menuBar; 
}// end of method getMenuBar() 

public JScrollPane getScrollBar() 
{ 
    // Hands execution back to the menuBar function. 
    return scrollBar; 
}// end of method getScrollBar() 

public static void main(String[] args) 
{ 
    SwingUtilities.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       // Input all the compoenents of the photo viewer to the JFrame to  display them. 
       PhotoViewer imageList = new PhotoViewer(); 

       // Creates a new JFrame to display everything. 
       JFrame mainFrame = new JFrame("Photo Viewer"); 
       // 'Throws away' the JFrame on close. 
       mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
       // Adds all the different components to the JFrame. 
       mainFrame.add(imageList.getPanel()); 
       mainFrame.add(imageList.getScrollBar()); 
       mainFrame.setJMenuBar(imageList.getMenuBar()); 
       mainFrame.setLocationByPlatform(true); 
       // Packs all the components into the JFrame. 
       mainFrame.pack(); 
       // Sets the size of the JFrame. 
       mainFrame.setSize(1500,1500); 
       // Allows us to see the JFrame. 
       mainFrame.setVisible(true); 
      } 
     }); 
} // end of method main(String[] args) 
} // end of class PhotoViewer 
+0

请包括堆栈跟踪 – 2013-03-24 21:52:51

+0

@A liAlamiri:没有编译器错误的堆栈跟踪... – jlordo 2013-03-24 21:55:29

+3

@jlordo但这段代码符合要求。 – dantuch 2013-03-24 21:57:21

回答

1

没有必要添加mainPanelscrollBar分开,如scrollBar已经包含mainPanel。请执行mainFrame.add(imageList.getScrollBar());,根本不要拨打mainFrame.add(imageList.getPanel());。一个控件只能添加到一个容器中。

JFrame的默认布局是BorderLayout。当您将控件添加到BorderLayout而未指定布局约束时,它会将控件置于BorderLayout.CENTER之内,从而有效地替换以前的任何内容。

+1

非常感谢,一直在我的脑子里呆了一个小时。 – user2205330 2013-03-24 22:07:41

+1

@ user2205330不客气,很高兴帮助你! :) – tenorsax 2013-03-24 22:11:49

0

只是一个小的改动你的代码:)

,而不是

scrollBar = new JScrollPane(mainPanel); 

使用

scrollBar = new JScrollPane(imageView); 
mainPanel.add(scrollBar); 

,也没有必要

mainFrame.add(imageList.getScrollBar()); 
+0

顺便说一句,没有必要设置scrollBar静态:) – 2013-03-24 22:27:13

相关问题