gpt4 book ai didi

java - 转到最后滚动的位置 java swing JScrollPane

转载 作者:行者123 更新时间:2023-11-30 07:05:50 32 4
gpt4 key购买 nike

我有一个 JScrollPane,我在其中显示图像。相关的代码行是

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

此代码在单击按钮时被调用,它基本上加载相同的图像并稍作修改。

假设加载了一张图片并且用户滚动(水平和垂直)以转到某个位置。下次单击按钮并加载新图像时,滚动 Pane 会忘记先前滚动的位置并重新开始一切。

我想查看上次滚动位置的图像,直到发生另一次滚动。这可能吗?

编辑

代码如下:

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 here . Start 按钮加载新图像。在我滚动并单击“输入”按钮后,加载了另一个图像,但滚动 Pane 返回到左上角(原始位置)。

最佳答案

保存视口(viewport)的位置然后恢复它的位置:

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

编辑:

我将您的 SSCCE 更改为使用 ContainerListener 来重置视口(viewport) View 位置:

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

关于java - 转到最后滚动的位置 java swing JScrollPane,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26290852/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com