gpt4 book ai didi

java - 如何将changeHandler()添加到我的程序中

转载 作者:行者123 更新时间:2023-12-01 07:37:30 26 4
gpt4 key购买 nike

拜托,我尝试添加一个changeHandler 事件,但我认为我做得不对。我正在查看以下声明..

pickNum.addChangeListener(new ChangeHandler());

@Override
public void stateChanged(changeEvent e)
{
JSlider s = (JSlider)e.getSource();
index = s.getValue();
}

有什么事情我可以做得更好,因为它不起作用..

import javax.swing.*;

public class Slider extends JFrame{

public static int index;
JSlider pickNum = new JSlider(JSlider.HORIZONTAL,0,30,5);
public Slider()
{
super("Slider");
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

pickNum.setMajorTickSpacing(10);
pickNum.setMinorTickSpacing(1);
pickNum.setPaintTicks(true);
pickNum.setPaintLabels(true);
pickNum.addChangeListener(new ChangeHandler());

@Override
public void stateChanged(changeEvent e)
{
JSlider s = (JSlider)e.getSource();
index = s.getValue();
}

getPointedValue();
this.add(pickNum);
this.setVisible(true);

}

public final int getPointedValue()
{
int value;
value = pickNum.getValue();
return value;
}


public static void main(String[] args) {

Slider frame = new Slider();
System.out.println("value is :"+Slider.index);
}

}

[/CODE]

最佳答案

你的 ChangeHandler 是什么?您还没有发布该代码

这是使用更改监听器的示例


JSlider s = new JSlider();
s.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
System.out.println("Changed: " + e);
}
});

关于java - 如何将changeHandler()添加到我的程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9957118/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com