package practice15;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
class MyGuiEvent extends JFrame implements ChangeListener{
private JPanel jpn = new JPanel();
private JSlider jsl = new JSlider(0,765);
public MyGuiEvent() {
this.add(jpn);
jpn.add(jsl);
jsl.setMajorTickSpacing(255);//主刻度
jsl.setMinorTickSpacing(15);//副刻度
jsl.addChangeListener(this);
jsl.setPaintTicks(true);
jsl.setPaintLabels(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(800,800);
this.setVisible(true);
}
@Override
public void stateChanged(ChangeEvent e) {
if(jsl.getValue()<=255) {
jpn.setBackground(Color.red);
}
else if(jsl.getValue()<=255*2) {
jpn.setBackground(Color.green);
}
else {
jpn.setBackground(Color.blue);
}
}
public static void main(String[] args){
MyGuiEvent mge = new MyGuiEvent();
}
}