核心,传递参数---构造方法
? ? ? ? 不同的监听器相互间传递参数
? ? ? ? 用到的方法,SLider中的setValue,设置初始值,getValue得到Slider当前数值
? ? ? ?显现SLider用法与加监听器
class="java" name="code">package netjava; import java.awt.FlowLayout; import java.awt.Graphics; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JSlider; public class Draw02 extends JFrame { // private JSlider sl; public static void main(String[] args){ Draw02 dr=new Draw02(); dr.initUI(); } //定义初始界面方法 public void initUI(){ JFrame jf=new JFrame(); jf.setTitle("王板"); JSlider sl01=new JSlider(); sl01.setMaximum(255); sl01.setValue(0); JSlider sl02=new JSlider(); sl02.setMaximum(255); sl02.setValue(0); JSlider sl03=new JSlider(); sl03.setMaximum(255); sl03.setValue(100); jf.add(sl01); jf.add(sl02); jf.add(sl03); jf.setDefaultCloseOperation(3); jf.setLocation(100,100); jf.setSize(600,600); jf.setLayout(new FlowLayout()); JButton bu=new JButton("技术"); jf.add(bu); jf.setVisible(true); //在画板显示之后取得窗体的画布对象 Graphics g=jf.getGraphics(); //实例化处理累对象 ButtonListener dl=new ButtonListener(sl01,sl02,sl03,g); // DrawListener dl=new DrawListener(g); //加监听器 // bu.addActionListener(dl); // Recur dl=new Recur(g); SliderListener sListener = new SliderListener(sl01,sl02,sl03,dl); sl01.addChangeListener(sListener); sl02.addChangeListener(sListener); sl03.addChangeListener(sListener); jf.addMouseListener(dl); } } package netjava; import javax.swing.JSlider; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; public class SliderListener implements ChangeListener { private JSlider sl01; private JSlider sl02; private JSlider sl03; private ButtonListener dl; private int value01; private int value02; private int value03; public SliderListener(JSlider sl01, JSlider sl02, JSlider sl03,ButtonListener dl) { this.sl01= sl01; this.sl02= sl02; this.sl03= sl03; this.dl = dl; } public void stateChanged(ChangeEvent e){ // Draw02 draw=new Draw02(sl01); // int a=sl01.getValue(); // int b= if(e.getSource()==this.sl01){ value01 =this.sl01.getValue(); System.out.println("hahaa"); } if(e.getSource()==this.sl02){ value02=this.sl02.getValue(); } if(e.getSource()==this.sl03){ value03=this.sl03.getValue(); } dl.compute(value01, value02, value03); } } package netjava; import java.awt.Color; import java.awt.Graphics; //import java.awt.event.ActionEvent; //import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.JSlider; public class ButtonListener extends MouseAdapter /*implements ActionListener */{ private Graphics g; double a=1,b=4,c=60; private JSlider sl01; private JSlider sl02; private JSlider sl03; public void mouseClicked(MouseEvent e) { compute(this.sl01.getValue(),this.sl02.getValue(),this.sl03.getValue()); } // } // } public ButtonListener(JSlider sl01, JSlider sl02, JSlider sl03,Graphics g){ this.g=g; this.sl01= sl01; this.sl02= sl02; this.sl03= sl03; } public void compute(int value01,int value02,int value03){ double x=0.0; double y = 0.0; for(int i=0;i<50000;i++){ // int j=255; // while(j>0){ // Color co=new Color(j,j,j); // g.setColor(co); // double x2=d*Math.sin(a*x)-Math.sin(b*y); // double y2=c*Math.cos(a*x)+Math.cos(b*y); double x2=y-Math.signum(x)*Math.sqrt(Math.abs(b*x-c)); double y2=a-x; int x3=(int)(1*x2); int y3=(int)(1*y2); g.setColor(new Color(value01,value02,value03)); g.drawLine(x3+600, y3+400, x3+600, y3+400); x=x2; y=y2; } } }
?