我的水平滚动条演示有问题。我正在尝试使用滚动条移动自定义消息面板。我使用了匿名侦听器并覆盖了adjustmentValueChanged()方法,如下所示:
public void adjustmentValueChanged(AdjustmentEvent e){
System.out.println(e.getAdjustmentType());
if( e.getAdjustmentType() == AdjustmentEvent.UNIT_INCREMENT ) {
panel.moveLeft();
}
}我正在尝试使用e.getAdjustmentType()获取AdjustmentEvent,这样我就可以正确地处理消息面板的调整。然而,它并不起作用。我使用System.out.println()方法在屏幕上打印调整类型,以查看问题所在,但我不能理解的是,无论我按下滚动条的哪一部分(无论是单位增量、单位减量、块增量等)。返回值是5?我不确定是什么问题,有人能帮我吗?
public class ScrollBarDemo extends JFrame {
private JScrollBar scrollHort = new JScrollBar(JScrollBar.HORIZONTAL);
private JScrollBar scrollVert = new JScrollBar(JScrollBar.VERTICAL);
private MessagePanel panel = new MessagePanel("Welcom to Java bitch");
public static void main(String[] args) {
ScrollBarDemo frame = new ScrollBarDemo();
frame.setTitle("ScrollBarDemo");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public ScrollBarDemo() {
setLayout(new BorderLayout());
add(panel, BorderLayout.CENTER);
add(scrollHort, BorderLayout.SOUTH);
add(scrollVert, BorderLayout.EAST);
scrollHort.addAdjustmentListener(new AdjustmentListener() {
public void adjustmentValueChanged(AdjustmentEvent e) {
System.out.println(e.getAdjustmentType());
if (e.getAdjustmentType() == AdjustmentEvent.UNIT_INCREMENT) {
panel.moveLeft();
}
}
});
}
}发布于 2017-01-09 03:07:00
运行以下示例,要使用的方法是getValue()。
public class ScrollBarDemo extends JFrame {
public ScrollBarDemo() {
setLayout( new BorderLayout());
final JScrollBar scrollHort = new JScrollBar( JScrollBar.HORIZONTAL );
add( scrollHort, BorderLayout.SOUTH );
scrollHort.addAdjustmentListener( e -> System.out.println( e.getValue()));
setLocationRelativeTo(null);
setDefaultCloseOperation( EXIT_ON_CLOSE );
pack();
setVisible(true);
}
public static void main( String[] args ) {
new ScrollBarDemo();
}
}https://stackoverflow.com/questions/21489624
复制相似问题