2014-10-10 46 views
1

我有一個JScrollPane我正在顯示圖像。相關的代碼行是轉到最後滾動的位置java swing JScrollPane

ImageIcon ii = new ImageIcon(in); 
scrollPane.setViewportView(new JLabel(ii)); 

這個代碼在單擊按鈕時被調用,它基本上只是稍加修改就加載相同的圖像。

假設圖像已加載,並且用戶滾動(水平和垂直)以轉到某個位置。下次單擊該按鈕並加載新圖像時,滾動窗格會忘記以前滾動的位置並重新開始一切。

我想查看上一個滾動位置的圖像,直到發生另一個滾動。那可能嗎?

EDIT

下面是代碼:

import java.awt.EventQueue; 
import java.awt.Font; 
import java.awt.Point; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 

import javax.imageio.ImageIO; 
import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JViewport; 
import javax.swing.SwingConstants; 
import javax.swing.border.EmptyBorder; 
import javax.swing.event.ChangeEvent; 
import javax.swing.event.ChangeListener; 

public class TestScrollPane extends JFrame { 
    /** 
    * 
    */ 
    static BufferedImage in = null; 
    private JPanel contentPane; 
    static String imageLoc=null; 
    static Point p= new Point(2,2); 
    /** 
    * Launch the application. 
    * @throws IOException 
    */ 
    public static void main(String[] args) throws IOException { 
     EventQueue.invokeLater(new Runnable() { 

      public void run() { 
       try { 
        TestScrollPane frame = new TestScrollPane(); 
        frame.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    /** 
    * Create the frame. 
    */ 

    private String getAnewImage() throws IOException { 
     return "/home/abc/test.jpg"; //change this 
    } 
    public TestScrollPane() { 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setBounds(0, 0, 1300, 1000); 
     contentPane = new JPanel(); 
     contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
     setContentPane(contentPane); 
     contentPane.setLayout(null); 

     //all declarations go here 
     final JScrollPane scrollPane = new JScrollPane(); 
     final JButton btnInput = new JButton("Test"); 
     btnInput.setVerticalAlignment(SwingConstants.TOP); 
     btnInput.setFont(new Font("Dialog", Font.BOLD, 12)); 
     btnInput.setHorizontalAlignment(SwingConstants.LEFT); 
     final JButton btnStart = new JButton("start"); 
     final JViewport jv= scrollPane.getViewport(); 

     // all dimensions go here 
     scrollPane.setBounds(2, 2, 1150, 950); 
     btnInput.setBounds(1160, 100, 90, 50); 
     btnStart.setBounds(1160, 300, 90, 25); 

     contentPane.add(scrollPane); 


     jv.addChangeListener(new ChangeListener() { 
      @Override 
      public void stateChanged(ChangeEvent e) { 
       p=jv.getViewPosition(); 
      } 
     }); 



     btnInput.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       try { 
        in = ImageIO.read(new File(imageLoc)); 
       } catch (IOException e1) { 
        e1.printStackTrace(); 
       } 
       ImageIcon ii = new ImageIcon(in); 
       System.out.println("setting viewport to "+p); 
       scrollPane.setViewportView(new JLabel(ii)); 
       scrollPane.getViewport().setViewPosition(p); 
      } 
     }); 

     contentPane.add(btnInput); 


     btnStart.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       try { 
        imageLoc=getAnewImage(); 
       } catch (IOException e1) { 
        e1.printStackTrace(); 
       } 
       if (imageLoc==null) 
        return; 
       else { 

        try { 
         in = ImageIO.read(new File(imageLoc)); 
        } catch (IOException e1) { 
         e1.printStackTrace(); 
        } 
        ImageIcon ii = new ImageIcon(in); 
        scrollPane.setViewportView(new JLabel(ii)); 
       } 

      } 


     }); 


     contentPane.add(btnStart); 


     } 
    } 

這裏是我用於測試的圖像::enter image description hereStart按鈕加載新圖像。在滾動並單擊「輸入」按鈕後,將加載另一個圖像,但滾動窗格會返回到左上角(原始位置)。

回答

3

保存視口的位置,然後恢復它的位置:

Point p = scrollPane.getViewport().getViewPostion(); 
scrollPane.setViewportView(new JLabel(ii)); 
scrollPane.getViewport.setViewPosition(p); 

編輯:

我改變了你的SSCCE使用ContainerListener復位口的視圖位置:

jv.addContainerListener(new ContainerAdapter() 
{ 
    @Override 
    public void componentAdded(ContainerEvent e) 
    { 
     scrollPane.getViewport().setViewPosition(p); 
    } 
}); 
+0

這似乎不起作用。我有點滾動,但'setViewPosition'不設置的位置。 – rivu 2014-10-10 02:43:33

+2

@rivu,在Windows 7上使用JDK7可以正常工作。發佈你的[SSCCE](http://sscce.org/)來證明問題。只有其他建議是嘗試在SwingUtilities.invokeLater()中包裝setViewPosition(...)語句,以確保在使用新組件重新驗證滾動窗格後執行代碼。 – camickr 2014-10-10 04:01:43

+0

可能與flush()問題,這是JLabel的常見問題 - IMageIcon - (較慢的資源)與較大的文件 – mKorbel 2014-10-10 07:13:45

1

爲標籤創建一個displayImage。加載新圖像時,將其繪製到顯示圖像並重新繪製標籤。

相關問題