我加的滚动条为什么不显示呢? 谢谢版主,我增加了pnlLeft.setPreferredSize(new Dimension(300, 200));pnlRight.setPreferredSize(new Dimension(300, 200));这样两个panel是大小控制住了,但是滚动条还是没显示出来。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你给JScrollPane 加上这两个方法看看setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); 调整了一下解决了 /** * 主面板 */ 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) { } }); }但现在有个问题。两边的滚动条无法同步滚动。即我拖动左边的滚动条,想让右边的也进行滚动 Switch Case 高分求助,怎样快速有效的将work文档的内容按指定格式输出 以后java搞什么方面好些???是手机软件开发还是web?? java API文档字段摘要怎么用?? Hibernate 检索时的怪问题? 我这段代码哪里错了?? 打包成JAR文件后,servlet可以用相对目录写文件吗? 急!急!!急!!! 感叹jdk各个版本 紧急求教!究竟Java里面有没有相当于C++的CPOINT的类阿? ldap分页查询,SizeLimitExceededException 在javase中的socket编程中,碰到的问题,在内容中具体描述
setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
/**
* 主面板
*/
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) {
}
}); }但现在有个问题。两边的滚动条无法同步滚动。即我拖动左边的滚动条,想让右边的也进行滚动