考虑以下图片:
有一个垂直滚动条和一个水平滚动条。单击确定后,框架将消失。
在完成其他一些工作之后,我想让用户保持与现在相同的位置,这可能是具有相同宽度和高度的不同图像。所以,我想跟踪滚动条中的条形。
是否有任何内置函数可以将百分比或某些内容提升到get
和set
条位置?
MultipleProcess.java 类,实际打开框架。
// imports and package description were here
public class MultipleProcess extends javax.swing.JDialog {
SinglePaperEvaluation upperClass;
BatchPaperEvaluation upperClass2;
File imageFilePath;
BufferedImage image;
public MultipleProcess(boolean modal,File imageFile,SinglePaperEvaluation upper, BatchPaperEvaluation upper2) {
super(upper==null?upper2:upper,"Co-ordinate Detector", modal);
initComponents();
upperClass = upper;
upperClass2 = upper2;
imageFilePath = imageFile;
//adjust screen and windows listener were here
loadImage();//custom function, it loads image in the jScrollPaneImageScreen
if(upperClass2!=null){
upperClass2.setVisible(false);
//scrollbars states loading part
jScrollPaneImageScreen.getVerticalScrollBar().setValue(upperClass2.verticalBar);
jScrollPaneImageScreen.getHorizontalScrollBar().setValue(upperClass2.horizontalBar);
// if(upperClass2.react!=null)jScrollPaneImageScreen.scrollRectToVisible(upperClass2.react);
}
setVisible(true);
}
// you can skip this function but have a look if you eager to see how am I loaded image in the pane
private void loadImage() {
try {
image = ImageIO.read(imageFilePath);
ImageIcon ii = new ImageIcon(imageFilePath.toString());
JLabel jLabel = new JLabel(ii);
jLabel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int x = e.getX();
int y = e.getY();
jTextFieldX.setText(String.format("%d",x));
jTextFieldY.setText(String.format("%d",y));
}
});
jScrollPaneImageScreen.setViewportView(jLabel);
} catch (IOException ex) {
Logger.getLogger(SkeletonPreparing.class.getName()).log(Level.SEVERE, null, ex);
}
}
//init Component function was here
private void jButtonOKActionPerformed(java.awt.event.ActionEvent evt) {
if(upperClass2!=null){
//other irrelevant processing deleted
//scrollbars states storing part
upperClass2.verticalBar = jScrollPaneImageScreen.getVerticalScrollBar().getValue();
upperClass2.horizontalBar = jScrollPaneImageScreen.getHorizontalScrollBar().getValue();
// upperClass2.react = jScrollPaneImageScreen.getViewport().getViewRect();
System.out.println("Stored: "+upperClass2.verticalBar+","+upperClass2.horizontalBar);
upperClass2.setVisible(true);
}
dispose();
}
//variable initializations were here
}
BatchPaperEvaluation.java 类,我从中创建了上述类的实例:
private void jButtonSubmitActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
File[] filesToBeProcessed = new File(jTextFieldOMRFolderPath.getText()).listFiles(filter);
for (File inputFile : filesToBeProcessed) {
MultipleProcess dialog = new MultipleProcess(true, inputFile, null, this);
//other processing
}
//further processing
}
int getVerticalScrollBarPolicy()
,但这并不适合我的工作。答案 0 :(得分:1)
如果您使用scrollPane.getVerticalScrollBar().getValue()
,则可以在关闭窗格时保存该值。
然后,当您重新加载窗格时,只需使用scrollPane.getVerticalScrollBar.setValue(savedValue)
。
您也可以对水平滚动条执行相同的操作。
我查看了您在评论中添加的MCVE,我强烈怀疑问题出在您给我的代码中,可能是在调用new MultipleProcess
的课程中。
答案 1 :(得分:1)
尝试使用JScrollPane.getViewport().getViewRect()
获取可见的矩形。您可以通过JScrollPane.getViewport().scrollRectToVisible(<saved rect>)
恢复此操作。