java自定义button类控件

 

上面是做完后的效果,第一次用java来做自定义继承button类控件,如图看到的其实就只有一个经改写的button控件,style_1、2、3、4 是button内部的几个小label而已,不过中间的滑动效果做得不太好,滑动时有闪烁,还请高手们指点指点啦......

如图里的style_1、2...是可以自己在外部添加自定义  listener  ,调用如下:

myjb.addJblListener("1", new lblistener_1());  myjb.addJblListener("2", new lblistener_2());  myjb.addJblListener("3", new lblistener_3());  myjb.addJblListener("4", new lblistener_4());这样

再实现lblistener_1方法来,第一次写博文,没啥经验,如有不好的地方请大家不要太在意啦。。呵呵~

好,废话不多说了,直接上代码吧。。。

package jbutton_test;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

@SuppressWarnings("serial")
public class myJbutton extends JButton {
	static JLabel lb = null;
	static Timer t = null;
	static myJbutton jb = null;
	static boolean dire = false; // slider direction..
	static int next_slider_x; // the X of the next location where the slider will go..
	static int fir_slider_x ; // the first X of the slider..
	static int slider_width ;	//the width of the slider...
	static boolean t_isrun = false;		//is the timer running
	
	ArrayList<JLabel> Jlb = new ArrayList<JLabel>();			//JLabel  container

	public myJbutton(String labText[]) {
		Dimension size = getPreferredSize();
		size.width = get_Bwidth(labText);			//set size..
		size.height = 50;
		setBackground(Color.white);
		fir_slider_x = 35;									//get the X of the first slider ..
		slider_width = labText[0].length()*15+28;						//get the width of the first slider ..
		creatLab(labText);
		setLayout(new FlowLayout(FlowLayout.LEFT, 30, 5));
		setPreferredSize(size);
		setContentAreaFilled(false);
	}
	
//get the width of the button....
	private int get_Bwidth(String str[]) {
		int width = 0;
		for(int i = 0;i<str.length;i++){
			width += (str[i].length()*17+30);
		}
		width += 80;
		return width;
	}
//creatLab
	private void creatLab(String labText[]) {
		for (int i = 0; i < labText.length; i++) {
			lb = new JLabel(labText[i]);
			//lb.setFont(new Font("Jokerman", Font.BOLD, 20));
			lb.setFont(new Font("华文琥珀", Font.BOLD, 30));
			lb.setName(Integer.toString(i));
			add(lb);
			Jlb.add(lb);	//add to the container...
			lb.addMouseListener(new lab_lsn(lb));
			lb.setCursor(new Cursor(Cursor.HAND_CURSOR));		//set the cursor as hand when the cursor move into ...
			}
		}

//add listeners to each label  (须外部调用才能添加label事件)
	public void addJblListener(String lb_name, MouseListener l) {
		int i;
		lb_name = Integer.toString(Integer.parseInt(lb_name)-1);
		for (i = 0; i < Jlb.size(); i++) {
			JLabel lb_temp = Jlb.get(i);
			if (lb_temp.getName().equals(lb_name)) {
				lb_temp.addMouseListener(l);
				break;
			}
		}
		//当添加标签事件名字出错时执行
		if(i >= Jlb.size()){
			System.out.println("无法找到 "+lb_name+ " 标签,请核对该标签名字是否正确。。。");
			return;
		}
	}

//inner listener of the label...(let the label get two listener)
	class lab_lsn extends MouseAdapter {
		JLabel l1 = null;
		public lab_lsn(JLabel l1) {
			this.l1 = l1;
		}

		@Override
		public void mouseClicked(MouseEvent e) {
			super.mouseClicked(e);
			slider_width = l1.getSize().width + 25;		//get the next width of the next slider
			next_slider_x = l1.getX() - 13;			//get the next X of the slider
			t = new Timer();
			t_isrun = true;
			t.schedule(new ttask(), 1, 3);
		}
	}

	class ttask extends TimerTask {
		@Override
		public void run() {
			paintComponent(getGraphics());
			repaint();
		}
	}

	@Override
	public void paint(Graphics g) {
		g.setColor(getBackground());
		g.fillRoundRect(0, 0, getSize().width - 1, getSize().height - 1, 50, 50);
		super.paint(g);
	}

	protected void paintComponent(Graphics g) {
		if ((fir_slider_x == next_slider_x) && (t_isrun == true)) {
			t.cancel();
			t_isrun = false;
		}
		//g.setColor(Color.yellow);
		Color color1 = Color.white;
		Color color2 = Color.gray;
		Graphics2D g2 = (Graphics2D) g;
		g2.setPaint(new GradientPaint(getSize().width/2, 1, color1, getSize().width/2, 100, color2));
		g.fillRoundRect(fir_slider_x, 5, slider_width, 40, 50, 50);
		if (t_isrun) {
			if (fir_slider_x > next_slider_x) {
				dire = false;
			} else if (fir_slider_x < next_slider_x) {
				dire = true;
			}
			if (dire == true) {
				fir_slider_x += 1;
			} else {
				fir_slider_x -= 1;
			}
		}
//System.out.println("signal...");
	}

	protected void paintBorder(Graphics g) {
		g.setColor(getForeground());
	}
}

 

 

 

 

顺便加上main类的代码

 

package jbutton_test;

import java.awt.Color; import java.awt.FlowLayout; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent;

import javax.swing.JFrame;

 @SuppressWarnings("serial") class frame extends JFrame {  public frame() {   super("按钮");   setLayout(new FlowLayout());   /*    * 下面是不同的label个数的数组    * 中英文都试过了。。   */   //String[] labText = { "stye_1"};   //String[] labText = { "stye_1", "stye_2"};   //String[] labText = { "stye_1", "stye_2", "stye_3"};   String[] labText = { "stye_1", "stye_2", "stye_3", "stye_4"};   //String[] labText = { "stye_1", "stye_2", "stye_3", "stye_4" ,"stye_5"};   //String[] labText = { "stgfjh", "第二个标签", "stye_3", "stye","s4" ,"st", "2" ,"jgff", "3", "d","e", "g"};   myJbutton myjb = new myJbutton(labText);   add(myjb);   /*    * 下面是添加每个label的事件,都自定义的。。。格式是    * myjb.addJblListener("标签代号(第一个标签是1第二个是2以此类推,都是string型的)", new lblistener_1());    * new lblistener_1()是自己添加的mouselistener的子类,如下的lblistener_1类。。   */   myjb.addJblListener("1", new lblistener_1());   myjb.addJblListener("2", new lblistener_2());   myjb.addJblListener("3", new lblistener_3());   myjb.addJblListener("4", new lblistener_4());   //myjb.addJblListener("5", new lblistener_5());   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   pack();   setResizable(false);   this.getContentPane().setBackground(Color.DARK_GRAY);   setVisible(true);  }  class lblistener_1 extends MouseAdapter{   @Override   public void mouseClicked(MouseEvent e) {    getContentPane().setBackground(Color.DARK_GRAY);    super.mouseClicked(e);   }  }  class lblistener_2 extends MouseAdapter{   @Override   public void mouseClicked(MouseEvent e) {    getContentPane().setBackground(Color.pink);    super.mouseClicked(e);   }  }  class lblistener_3 extends MouseAdapter{   @Override   public void mouseClicked(MouseEvent e) {    getContentPane().setBackground(Color.cyan);    super.mouseClicked(e);   }  }  class lblistener_4 extends MouseAdapter{   @Override   public void mouseClicked(MouseEvent e) {    getContentPane().setBackground(Color.orange);    //System.exit(0);           //close the window...    super.mouseClicked(e);   }  }  class lblistener_5 extends MouseAdapter{   @Override   public void mouseClicked(MouseEvent e) {    getContentPane().setBackground(Color.orange);    super.mouseClicked(e);   }  } }

public class jbutt_main {  public static void main(String[] args) {   new frame();  } }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值