它们实际上是两个问题:
First问题:getValueIsAdjusting()在JScrollBar和AdjustmentEvent中的区别是什么?
我用一些代码来测试它们是否有任何区别,但我没有得到任何差别!下面的代码展示了我是如何测试它们的。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ScrollTest extends JPanel
{
JPanel panel;
JScrollBar scroll;
public ScrollTest()
{
scroll = new JScrollBar(JScrollBar.HORIZONTAL, 0, 6, 0, 300);
scroll.addAdjustmentListener(ScrollListener);
panel = new JPanel(new GridLayout(1, 0));
panel.add(scroll);
this.setLayout(new BorderLayout());
this.add(panel);
}
AdjustmentListener ScrollListener = new AdjustmentListener()
{
@Override
public void adjustmentValueChanged(AdjustmentEvent e)
{
if(e.getValueIsAdjusting())
{
System.out.println("AdjustmentEvent");
}
if(scroll.getValueIsAdjusting())
{
System.out.println("JScrollBar");
}
}
};
private static void createAndShowGUI()
{
JFrame frame;
frame = new JFrame("Scroll Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(880, 100);
frame.add(new ScrollTest(), BorderLayout.CENTER);
frame.setVisible(true);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
UIManager.put("swing.boldMetal", Boolean.FALSE);
createAndShowGUI();
}
});
}
}上面的代码将按顺序打印"AdjustmentEvent“和"JScrollBar”字符串。他们之间似乎没有什么区别!
重要的一点是什么时候使用??
第二个问题:
如何收听JScrollBar按钮?如果您测试了上面的代码,当您移动旋钮或单击条带时,它会打印字符串,而不是当您单击JScrollBar的按钮时。
发布于 2013-01-26 17:53:01
在adjustmentValueChanged of AdjustmentListener类中再添加一个事件(如声明的AdjustmentListener)。
如果事件类型为AdjustmentEvent.TRACK,则还打印一条语句。
if(e.getValueIsAdjusting())
{
System.out.println("AdjustmentEvent");
}
if(scroll.getValueIsAdjusting())
{
System.out.println("JScrollBar");
}
if(e.getAdjustmentType() == AdjustmentEvent.TRACK)
{
System.out.println("The button in scrollbar clicked");
}这将捕获JScrollBar上的按钮单击操作。
https://stackoverflow.com/questions/14539583
复制相似问题