1
我試圖在JSlider上實現一個changelistener。我試過兩種單獨的方法,都沒有工作。註釋部分是第一次嘗試。現在實施的那個可能會更適合我的目的。任何人都可以點出了什麼錯誤與此:JSlider changelistener無法正常工作
public class MixWindow extends JFrame implements ChangeListener{
private JPanel contentPane;
public static int uniA [] = new int [512];
ChangeListener sizeAction;
int level = 0;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MixWindow frame = new MixWindow();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
initUni();
}
public MixWindow() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 800, 600);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JSlider slider = new JSlider(0,255);
slider.setOrientation(SwingConstants.VERTICAL);
slider.setBounds(66, 275, 72, 140);
slider.setPaintTicks(true);
slider.setMajorTickSpacing(20);
slider.setValue(uniA[0]);
//slider.addChangeListener(sizeAction);
contentPane.add(slider);
final JLabel label = new JLabel("");
label.setBounds(66, 262, 61, 16);
contentPane.add(label);
/*sizeAction=new ChangeListener()
{
public void stateChanged (ChangeEvent event)
{
System.out.println("This is getting silly");
JSlider slider=(JSlider)event.getSource();
level=slider.getValue();
uniA[0] = level;
String temp = String.valueOf(level);
label.setText(temp);
}
};*/
}
public static void initUni(){
for(int i = 0; i < uniA.length; i++){
uniA[i] = 0;
}
}
@Override
public void stateChanged(ChangeEvent arg0) {
// TODO Auto-generated method stub
System.out.println("Stuff has changed");
Object source = arg0.getSource();
System.out.println(arg0 + " has Changed");
}
}
無關:不要做手工上漿/定位*有史以來*。相反,請使用合適的LayoutManager – kleopatra