请各位大虾指点指点
刚学过java ,写了一个小程序,但是有好多地方还是不懂,(1),怎么设置上面的圆有颜色,(2)HSlider这个拉条怎么设置老能从上向下的控制为由大到小,(3)监听器怎么写能够多次调用。谢谢各位大虾了!!
第一个java文件:
import java.awt.*;
import javax.swing.*;
public class OvalPanel extends JPanel {
private int width = 200;
private int height = 200;
private Color red,green,blue;
// draw an oval of the specified diameter
public void paintComponent( Graphics g ) {
super.paintComponent( g );
g.fillOval( 0, 0, width, height );
//g.setColor(Color.BLUE);
}
// validate and set diameter, then repaint
public void setWidth( int newWidth )
{
// if diameter invalid, default to 10
width = ( newWidth >= 0 ? newWidth : 10 );
repaint();
}
public void setHeight( int newHeight )
{
// if diameter invalid, default to 10
height = ( newHeight >= 0 ? newHeight : 10 );
repaint();
}
} // end class OvalPanel
第二个java文件:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class SliderDemo extends JFrame {
private JSlider diameterSlider,HSlider,redSlider,greenSlider,blueSlider;
private OvalPanel myPanel;
private JPanel controlSouth;
private Label redlabel,greenlabel,bluelabel,toplabel;
// set up GUI
public SliderDemo()
{
super( "Slider Demo" );
// set up OvalPanel
myPanel = new OvalPanel();
//myPanel.setBackground( Color.YELLOW );
controlSouth = new JPanel();
redlabel = new Label("Color red Component: " + 255);
greenlabel = new Label("Color green Component: " + 255);
bluelabel = new Label("Color blue Component: " + 255);
//toplabel = new Label("Applet");
// set up JSlider to control diameter value
diameterSlider =
new JSlider( SwingConstants.VERTICAL, 0,400 , 200 );
HSlider =
new JSlider( SwingConstants.HORIZONTAL, 0, 400, 200 );
redSlider =
new JSlider( SwingConstants.HORIZONTAL,0,255,255 );
greenSlider =
new JSlider( SwingConstants.HORIZONTAL,0,255,255 );
blueSlider =
new JSlider( SwingConstants.HORIZONTAL,0,255,255 );
diameterSlider.setMajorTickSpacing( 10 );
diameterSlider.setPaintTicks( true );
HSlider.setMajorTickSpacing( 10 );
HSlider.setPaintTicks( true );
redSlider.setMajorTickSpacing( 10 );
redSlider.setPaintTicks( true );
greenSlider.setMajorTickSpacing( 10 );
greenSlider.setPaintTicks( true );
blueSlider.setMajorTickSpacing( 10 );
blueSlider.setPaintTicks( true );
//set up JPanel
controlSouth.setLayout(new GridLayout(3,2,1,1));
controlSouth.add( redlabel );
controlSouth.add( redSlider );
controlSouth.add( greenlabel );
controlSouth.add( greenSlider );
controlSouth.add( bluelabel );
controlSouth.add( blueSlider );
redSlider.addChangeListener(
new ChangeListener() { // anonymous inner class
// handle change in slider value
public void stateChanged( ChangeEvent e )
{
myPanel.setBackground(new Color(redSlider.getValue(),greenSlider.getValue(),blueSlider.getValue()));
redlabel.setText("Color red Component: " + redSlider.getValue());
}
} // end anonymous inner class
); // end call to addChangeListener
greenSlider.addChangeListener(
new ChangeListener() { // anonymous inner class
// handle change in slider value
public void stateChanged( ChangeEvent e )
{
myPanel.setBackground(new Color(redSlider.getValue(),greenSlider.getValue(),blueSlider.getValue()));
greenlabel.setText("Color green Component: " + greenSlider.getValue());
}
} // end anonymous inner class
); // end call to addChangeListener
blueSlider.addChangeListener(
new ChangeListener() { // anonymous inner class
// handle change in slider value
public void stateChanged( ChangeEvent e )
{
myPanel.setBackground(new Color(redSlider.getValue(),greenSlider.getValue(),blueSlider.getValue()));
bluelabel.setText("Color blue Component: " + blueSlider.getValue());
}
} // end anonymous inner class
); // end call to addChangeListener
// attach components to content pane
// register JSlider event listener
diameterSlider.addChangeListener(
new ChangeListener() { // anonymous inner class
// handle change in slider value
public void stateChanged( ChangeEvent e )
{
myPanel.setHeight( diameterSlider.getValue());
}
} // end anonymous inner class
); // end call to addChangeListener
// attach components to content pane
HSlider.addChangeListener(
new ChangeListener() { // anonymous inner class
// handle change in slider value
public void stateChanged( ChangeEvent e )
{
myPanel.setWidth( HSlider.getValue());
}
} // end anonymous inner class
); // end call to addChangeListener
// attach components to content pane
Container container = getContentPane();
/*container.add( toplabel );
toplabel.setBounds(0,0,50,30);*/
container.add( diameterSlider, BorderLayout.WEST );
container.add( HSlider, BorderLayout.NORTH );
container.add( myPanel, BorderLayout.CENTER );
container.add( controlSouth, BorderLayout.SOUTH );
//redSlider.addadjustmentValueChanged(this);
//greenSlider.addAdjustmentListener(this);
//blueSlider.addAdjustmentListener(this);
setSize( 450, 600 );
setVisible( true );
// end constructor SliderDemo
}
/*public void adjustmentValueChanged(AdjustmentEvent e) {
int value1,value2,value3;
value1 = redSlider.getValue();
value2 = greenSlider.getValue();
value3 = blueSlider.getValue();
//color = new Color(value1,value2,value3);
myPanel.setBackground(new Color(value1,value2,value3));
}*/
public static void main( String args[] ) {
SliderDemo application = new SliderDemo();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
} // end class SliderDemo