|
||||||||||||
|
|
| 如何在按钮上绘画图形(如何利用paintComponent) |
| 时间:22/04/2007 作者:网络 来源:网络 |
| 小提示→点这里把文章加入您的收藏夹,方便下次查看 |
| 设置文章字体大小:[大 中 小] |
|
该组件可以任意在按钮上绘画你需要的形状(Shape),具体代码如下: import javax.swing.*; import java.awt.*; public class JXButton extends JButton { final public static int UP = 0; final public static int DOWN = 1; final public static int LEFT = 2; final public static int RIGHT = 3; final private static int SPACES = 3; private Dimension size = new Dimension(20,10); private int type = 0; public JXButton(Dimension size) { super(); this.size = size; setFocusPainted(false); //setBorderPainted(false); } public JXButton(int type,int width,double height) { this(new Dimension(width,(int)height)); this.type = type; } public JXButton(int width,int height) { this(new Dimension(width,height)); } public JXButton(int type,int width,int height) { this(new Dimension(width,height)); this.type = type; } public Dimension getPreferredSize() { return size; } public void setDisplay(int type) { this.type = type; repaint(); } public void paintComponent(Graphics g) { Dimension ds = getSize(); int x0 = -1,y0 = -1; int x1 = -1,y1 = -1; int x2 = -1,y2 = -1; g.clearRect(0,0,ds.width,ds.height); switch(type) { case UP: x0 = ds.width / 2 ; y0 = SPACES; x1 = SPACES ; y1 = ds.height - SPACES; x2 = ds.width - SPACES ; y2 = ds.height - SPACES; break; case DOWN: x2 = 1; ; y2 = SPACES; x0 = ds.width / 2 ; y0 = ds.height - SPACES; x1 = ds.width - SPACES ; y1 = SPACES; break; case LEFT: x0 = SPACES ; y0 = ds.height / 2; x1 = ds.width - SPACES ; y1 = ds.height - SPACES; x2 = ds.width - SPACES ; y2 = SPACES; break; case RIGHT: x1 = SPACES ; y1 =SPACES; x2 = SPACES ; y2 = ds.height - SPACES; x0 = ds.width - SPACES ; y0 = ds.height / 2; break; } //根据三点坐标开始画三角形 if(x1==x2 && x1>0) { //横坐标相等 for(int i = Math.min(y1,y2);i<=Math.max(y1,y2);i++) { g.drawLine(x0,y0,x1,i); } } else if(y1==y2 && y1>0) { //纵坐标相等 for(int i = Math.min(x1,x2);i<=Math.max(x1,x2);i++) { g.drawLine(x0,y0,i,y1); } } g.drawLine(x1,y1,x2,y2); //super.paintComponents(g); } }; |
|
上一篇:如何实现类似于微软的IP输入框的输入框
下一篇:[Java]:创建接受格式化文本的输入域 |
| 【返回】 【顶部】 【关闭】 |
| Copyright © 2005-2010 www.594k.com All Rights Reserved. |
| 版权所有:JAVA学习网
备案序号:皖ICP备06004238号 |