62,614
社区成员
发帖
与我相关
我的任务
分享
public class PreviewDialog extends JDialog {
private static final long serialVersionUID = "PREVIEWDIALOG".hashCode();
private static PreviewDialog instancePreview = null;
public PreviewDialog() {
super.setBackground(Color.WHITE);
int width = Toolkit.getDefaultToolkit().getScreenSize().width - 300;
int height = Toolkit.getDefaultToolkit().getScreenSize().height - 300;
int wPoint = (Toolkit.getDefaultToolkit().getScreenSize().width - width) / 2;
int hPoint = (Toolkit.getDefaultToolkit().getScreenSize().height - height) / 2;
setSize(width, height);
setBounds(wPoint, hPoint, width, height);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setTitle("压缩文件比对");
}
/**
* 主面板
*/
public void mainPanel() {
Container contentPane = this.getContentPane();
JPanel title = new JPanel();
JLabel lblTitle = new JLabel("图片压缩前后效果比对", JLabel.CENTER);
title.setBackground(Color.WHITE);
title.add(lblTitle);
contentPane.add(title, BorderLayout.NORTH);
// 主面板
JPanel main = new JPanel();
main.setBackground(Color.WHITE);
main.setLayout(new BoxLayout(main, BoxLayout.X_AXIS));
main.setVisible(true);
contentPane.add(main);
// 左边面板
JPanel pnlLeft = new JPanel();
JScrollPane scrollLeftPane = new JScrollPane();
pnlLeft.add(scrollLeftPane, BorderLayout.CENTER);
pnlLeft.setSize(300, 200);
scrollLeftPane.setVisible(true);
main.add(pnlLeft);
// 左边原始图片
JLabel lblSource = new JLabel();
lblSource.setIcon(getIcon("F:\\MyDocument\\RainBoy\\Desktop\\122.jpg"));
pnlLeft.add(lblSource);
// 右侧面板
JPanel pnlRight = new JPanel();
JScrollPane scrollRightPane = new JScrollPane();
pnlRight.add(scrollRightPane, BorderLayout.CENTER);
main.add(pnlRight);
// 右边处理图片
JLabel lblTaget = new JLabel();
lblTaget.setIcon(getIcon("F:\\MyDocument\\RainBoy\\Desktop\\122.jpg"));
pnlRight.add(lblTaget);
}
private Icon getIcon(String url) {
ImageIcon icon = new ImageIcon(url);
return icon;
}
pnlLeft.setPreferredSize(new Dimension(300, 200));pnlRight.setPreferredSize(new Dimension(300, 200));
这样两个panel是大小控制住了,但是滚动条还是没显示出来。 /**
* 主面板
*/
public void mainPanel() {
Container contentPane = this.getContentPane();
JPanel title = new JPanel();
JLabel lblTitle = new JLabel("图片压缩前后效果比对", JLabel.CENTER);
title.setBackground(Color.WHITE);
title.add(lblTitle);
contentPane.add(title, BorderLayout.NORTH);
// 主面板
JPanel main = new JPanel();
main.setBackground(Color.WHITE);
main.setLayout(new BoxLayout(main, BoxLayout.X_AXIS));
main.setVisible(true);
contentPane.add(main);
// 左边面板
JPanel pnlLeft = new JPanel();
// 左边原始图片
JLabel lblSource = new JLabel();
lblSource.setIcon(getIcon("F:\\MyDocument\\RainBoy\\Desktop\\11.jpg"));
// 左边滚动条
final JScrollPane scrollLeftPane = new JScrollPane(lblSource);
pnlLeft.add(scrollLeftPane);
scrollLeftPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollLeftPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollLeftPane.setPreferredSize(new Dimension(WIDTH/2, HEIGHT/2));
main.add(scrollLeftPane);
// 右侧面板
JPanel pnlRight = new JPanel();
// 右边处理图片
JLabel lblTaget = new JLabel();
lblTaget.setIcon(getIcon("F:\\MyDocument\\RainBoy\\Desktop\\Thumb122.jpg"));
// 右边滚动条
final JScrollPane scrollRightPane = new JScrollPane(lblTaget);
pnlRight.add(scrollRightPane);
scrollRightPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollRightPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollRightPane.setPreferredSize(new Dimension(WIDTH/2, HEIGHT/2));
main.add(scrollRightPane);
scrollLeftPane.addAncestorListener(new AncestorListener(){
public void ancestorMoved(AncestorEvent event) {
JScrollBar jScrollBarLeft = scrollLeftPane.getHorizontalScrollBar();
int i = jScrollBarLeft.getValue();
JScrollBar jScrollBarRight = scrollRightPane.getHorizontalScrollBar();
jScrollBarRight.setValue(jScrollBarLeft.getValue());
}
public void ancestorAdded(AncestorEvent event) {
}
public void ancestorRemoved(AncestorEvent event) {
}
});
}
但现在有个问题。两边的滚动条无法同步滚动。即我拖动左边的滚动条,想让右边的也进行滚动