滚动条

                           滚动条

    创建三个滚动条代表红,蓝,绿三种颜色的混合比列。用户拖拉或者单击时会显示颜色的值。

当你点击退出按钮时,窗体就会退出。当点击提交按钮时,会显示一个信息填写框提供给用户填写。

 

 java code:


import java.awt.*;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;

public class ScrollbarTest implements AdjustmentListener,ActionListener {
       
    Frame app = new Frame("滚动条");
      
     // 三个标签数组
     Label lblColor[] = {new Label("Red:"),new Label("Green:"),new Label("Blue:")};
      
        
     // 三个滚动条
     Scrollbar  scbColor[] = {
             new Scrollbar(Scrollbar.HORIZONTAL,0,20,0,255),
             new Scrollbar(Scrollbar.HORIZONTAL,0,20,0,255),
             new Scrollbar(Scrollbar.HORIZONTAL,0,20,0,255),};
        
       // Button 按钮
        Button bt1 = new Button("提交");  
        Button bt2 = new Button("退出");
           
          
         
    
       void sbinit(){
           
           app.setSize(260,300);
           app.setLayout(null);
            // 设置滚动条和标签的大小
           for(int k = 0;k<3;k++){
               lblColor[k].setBounds(110, 60+60*k, 50, 20);
               scbColor[k].setBounds(30, 85+60*k, 200, 20);
               scbColor[k].addAdjustmentListener(this);
           }
              
            //  该for循环用来设置标签和滚动条的背景颜色
             for(int i = 0;i<3;i++){
                 lblColor[0].setBackground(Color.red);
                 scbColor[0].setBackground(Color.red);
                 lblColor[1].setBackground(Color.green);
                 scbColor[1].setBackground(Color.green);
                 lblColor[2].setBackground(Color.blue);
                 scbColor[2].setBackground(Color.blue);
             }
                
             
           
             //  三个标签和三个滚动条添加到窗体中。
            for(int k = 0;k<3;k++){
                app.add(lblColor[k]);
                app.add(scbColor[k]);
            }
            
            bt1.setBounds(30, 250, 75,35);
            bt2.setBounds(160, 250, 75,35);
           bt1.addActionListener(this);
           bt2.addActionListener(this);
            
            
            
            
            app.setLocation(200, 100);
            app.setVisible(true);
              app.setResizable(false);  
             app.setBackground(Color.gray);
             app.add(bt1);
             app.add(bt2);
       }
    
    
       
       
    
    public static void main(String[] args){
        ScrollbarTest sbt = new ScrollbarTest();
        sbt.sbinit();
        
    }

    @Override
      
    public void adjustmentValueChanged(AdjustmentEvent e) {
        // TODO 自动生成的方法存根
        Object ob = e.getSource();
        if(ob == scbColor[0]){
            System.out.println(scbColor[0].getValue()+" ");
        }
        else if(ob == scbColor[1]){
            System.out.println(scbColor[1].getValue()+" ");
        }
        else if(ob == scbColor[2]){
            System.out.println(scbColor[2].getValue()+" ");
        }
        
    }
    
    
     //  ActionEvent 事件
     public void actionPerformed(ActionEvent e){
         Object ob = e.getSource();
          // 当点击退出按钮时,窗体就回退出。
         if(ob == bt2){
             System.exit(0);
         }
         else if(ob == bt1){
             Object str = "选择你喜爱的滚动条?";
             JOptionPane.showInputDialog(str);
         }
          
        
     }
    
    

}
  运行结果:

posted @ 2016-04-11 15:32  PengWenHao  阅读(245)  评论(0编辑  收藏  举报