编写一个Applet程序,在程序中包含一个滚动条,在Applet程序中绘制一个圆,用滚动条滑块显示的数字表示该圆的直径,当用户拖动滑块时,圆的大小随之改变。
------解决方案--------------------
作业题
------解决方案--------------------
这个还不简单?
------解决方案--------------------
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JApplet;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Applet1 extends JApplet {
private JPanel jpanel;
private JSlider jslider;
private Container contentPane = getContentPane();
public void init() {
jpanel = new JPanel();
jslider = new JSlider(50, 100);
jslider.putClientProperty( "JSlider.isFilled ", Boolean.TRUE);
jslider.setPaintLabels(true);
jslider.setMajorTickSpacing(10);
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
jpanel.setLayout(gbl);
constraints.gridx = 4;
constraints.gridy = 0;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.weightx = 4;
constraints.weighty = 1;
constraints.fill = GridBagConstraints.NONE;
gbl.setConstraints(jslider, constraints);
jpanel.add(jslider);
contentPane.setLayout(new BorderLayout());
contentPane.add( "Center ", jpanel);
final CircleCanvas kc = new CircleCanvas(jslider);
contentPane.add( "South ", kc);
jslider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
kc.setRadius(jslider.getValue());
kc.repaint();
}
});
}
}
class CircleCanvas extends Canvas {
private JSlider js;
private int diameter;
public CircleCanvas(JSlider js) {
this.js = js;
setSize(170, 120);
}
public void paint(Graphics g) {
diameter = js.getValue();
g.setColor(Color.red);
g.drawOval(40, 10, diameter, diameter);
}
public void setRadius(int radius){
this.diameter = radius;
}
}