我的水平滚动条演示有问题。我正在尝试使用滚动条移动自定义消息面板。我使用了一个匿名监听器并覆盖了adjustValueChanged()方法,如此,
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();
}
}
});
}
}
答案 0 :(得分:0)
运行以下示例,要使用的方法是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();
}
}