| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 656 人关注过本帖
标题:请各位大虾指点指点
只看楼主 加入收藏
磊行天下
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2008-11-9
结帖率:100%
收藏
 问题点数:0 回复次数:1 
请各位大虾指点指点
刚学过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

1.JPG (30.78 KB)
图片附件: 游客没有浏览图片的权限,请 登录注册
搜索更多相关主题的帖子: java specified private public 
2008-11-09 12:02
sxl111
Rank: 1
等 级:新手上路
威 望:1
帖 子:55
专家分:0
注 册:2008-5-7
收藏
得分:0 
72461773编程群……java   c#都可以…………
2008-11-09 13:42
快速回复:请各位大虾指点指点
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.109946 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved