java 能否实现图形按钮学习了java 已经有几个月了,想做几个例子出来,说明一下自已的实力!
看了一下API文档,但没有找到合适的类实现自已个性化的按钮!!!(不是WEB方面的)请高手帮忙!
提醒一下!!

解决方案 »

  1.   

    >>java 能否实现图形按钮 
        肯定的回答,能
       >>学习了java 已经有几个月了,想做几个例子出来,说明一下自已的实力! 
        不知道你这几个月都学的什么。 想MM 呢?!>>看了一下API文档,但没有找到合适的类实现自已个性化的按钮!!!(不是WEB方面的)请高手帮忙! 
    >>提醒一下!!
       
       javax.swing.JButton 
       public JButton(Icon icon) //------------------------------
       如果你还不懂, 改行吧。 
      

  2.   

    对啊!个性化!当然有形状各异的按钮了!!图形化的按钮了!!
    awt包里能否实现!不喜欢用swing因为它的在API中老说什么线程不安全的问题!
      

  3.   

    这个Java事情我不懂,但是我以前搞过别的开发——如果swing/SWT支持onmousedown/onmouseup,而且能够返回坐标的话,就没什么困难的。显示背景透明的按钮,onmouseup的时候,确定是不是在非透明区,如果不是的话直接忽略,如果是的话模拟按钮按下的效果(我当年用的是异或一个同样形状的图片),onmouseup的时候恢复原状(知道为什么用异或了吧?),然后执行。Java不是号称面向对象做得很好么,打个包就能复用了。
      

  4.   

    这个有什么难的
    swing用的是UI来显示组件,写个JButton的UI
    重写其中的某些方法,便可以实现一些特别的功能
    如:
        boolean  contains(JComponent c, int x, int y) 
    如果指定 x,y 位置包含在指定组件外观的已定义形状中,则返回 true。
    重写这个方法以绘制特殊形状的按钮.    void  paint(Graphics g, JComponent c) 
    绘制指定组件,使其适合外观。
    重写这个方法以画出自己想要按钮.
    还有些其他的方法,根据需要来决定是否重写
      

  5.   

    建议你看一下http://www.ibm.com/developerworks/cn/java/j-synth/
      

  6.   

    你在Google里面搜索一下Swing按钮会出来好多的!
      

  7.   


    那是因为你还没有弄明白它为什么要做成不是线程安全的,还有怎么去避开线程不安全的方法。:)请别介意我这么说,因为我觉得Swing确实不错……关于本帖的问题,我想你应该是想些自己的LOOKANDFEEL吧?
    java里面有两种实现方式 一种是用java写的,另一种是用XML文件中定义的 其实自己写这种东西没有太大必要。毕竟很漂亮的界面有专门的UI设计师去做。
      

  8.   


    /*
     * Created on Oct 16,2008
     * 
     * Copyright by 布谷鸟
     */
    package gui;
    import org.eclipse.swt.SWT;
    import org.eclipse.swt.graphics.Image;
    import org.eclipse.swt.graphics.ImageData;
    import org.eclipse.swt.graphics.Point;
    import org.eclipse.swt.graphics.Region;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.Event;
    import org.eclipse.swt.widgets.Listener;
    import org.eclipse.swt.widgets.Shell;/**
     * 
     * @Author cuckoo
     * @MSN [email protected]
     * @Time 14:09
     * @Description 
     *        draw an anomaly image in this application 
     */
    public class AnomalyImage {
    private Shell shell;    private Display display = Display.getDefault();    private Image image = null;    private ImageData imageData = null;    public static void main(String[] args) {
            try {
             AnomalyImage window = new AnomalyImage();
                window.open();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }    public void open() {
            createUI();
            shell.open();
            shell.layout();
            while (!shell.isDisposed()) {
                if (!display.readAndDispatch())
                    display.sleep();
            }
        }    protected void createUI() {
            shell = new Shell(display, SWT.NO_TRIM);
            shell.setSize(500, 375);
            shell.setText("SWT实现不规则窗体");
            createShell();
            Listener listener = new Listener() {
                int startX, startY;            public void handleEvent(Event e) {
                    // 注入鼠标事件
                    if (e.type == SWT.MouseDown && e.button == 1) {
                        startX = e.x;
                        startY = e.y;
                    }
                    if (e.type == SWT.MouseMove && (e.stateMask & SWT.BUTTON1) != 0) {
                        Point p = shell.toDisplay(e.x, e.y);
                        p.x -= startX;
                        p.y -= startY;
                        shell.setLocation(p);
                    }
                    if (e.type == SWT.Paint) {
                     e.gc.drawImage(image, imageData.x, imageData.y);
                    }
                }
            };        shell.addListener(SWT.KeyDown, listener);
            shell.addListener(SWT.MouseDown, listener);
            shell.addListener(SWT.MouseMove, listener);
            shell.addListener(SWT.Paint, listener);    }    protected void createShell() {     String path ="pan1.gif";
            image = new Image(display, new ImageData(path));
            Region region = new Region();
            imageData = image.getImageData();
            // 将255(白色)定为透明区域,镂空
            if (imageData.alphaData != null) {
                for (int y = 0; y < imageData.height; y++) {
                    for (int x = 0; x < imageData.width; x++) {
                        if (imageData.getAlpha(x, y) == 255) {
                            region.add(imageData.x + x, imageData.y + y, 1, 1);
                        }
                    }
                }
            }
            else {
                ImageData mask = imageData.getTransparencyMask();
                for (int y = 0; y < mask.height; y++) {
                    for (int x = 0; x < mask.width; x++) {
                        if (mask.getPixel(x, y) != 0) {
                            region.add(imageData.x + x, imageData.y + y, 1, 1);
                        }
                    }
                }
            }
            shell.setRegion(region);
            shell.setSize(imageData.x + imageData.width, imageData.y
                    + imageData.height);    }
    }注意,作为背景图片不能看见部分必须是透明的
    ----------------------------------------------------------------
    Quietly through ........
      

  9.   

    ImageIcon rolloverIcon=new ImageIcon("res/ActionIcon/操作员管理_roll.png");
    ImageIcon pressedIcon=new ImageIcon("res/ActionIcon/操作员管理_down.png");
    JButton button = new JButton(icon);
    button.setRolloverIcon(rolloverIcon);
    button.setPressedIcon(pressedIcon);基本上可以了
      

  10.   

    如果细看一下API你会发现,所以按钮或类似按钮都继承自AbstractButton
    所以要想实现自己的按钮,只要继承这个类,在绘制方法里画出自己想要的开关就OK了
    绘制按钮形状可要求对绘图类库的掌握,数学计算,及事件的处理
    基础库的开发就是这么类,什么特殊需求,非得开发这样的东东吗?
    另外一种方法就是利用第三方了,不推荐
      

  11.   

    我设计GUI程序时,老是没什么好界面,能否推荐几本电子书讲解这方面的,最好有一些可以用来模仿的例子程序!谢!
    新手上路,请多多观照!