swing 中如何改变按钮的形状

解决方案 »

  1.   

    没用过,记得在《core java》上看到过,你查查看吧。
      

  2.   

    public class MyButton extends JButton 

        public MyButton() { 
        }    public void paintComponent(Graphics g) { 
           super.paintComponent(g);
            g.drawArc(0,0,20,20);//可以自己绘制
        }
    }
      

  3.   

    下面是swing里面自带的改表变外观的:UIManager.setLookAndFeel(new LookAndFeel()); 
    JFrame.setDefaultLookAndFeelDecorated(true); 
    JDialog.setDefaultLookAndFeelDecorated(true); 
    还有就是一个项目做了一个提供流行的外观(look&feel)一些好看的look and feel
    .Substance 
    这个项目的目的是提供一个流行的外观(look & feel)。这个外观(look & feel)联合了Windows XP和MacOS 10.4最好的特性并且需要JDK 5.0以上 将下列jar文件拷贝到你的程序的classpath中,然后将下列代码段加入到你main函数中 
    http://l1.edn.cn/cache/http/index.php?q=http%3A%2F%2F210.42.106.102%2Fbbs%2Fviewthread.php%3Ftid%3D111%26amp%3Bextra%3Dpage%253D1 
    (注,其实我在别的文章中给出了一个例子,参见用java打造任意形状窗口一文中的的代码) 
    1.substance look and feel: 
    try { 
    UIManager.setLookAndFeel(new SubstanceLookAndFeel()); 
    UIManager.put("swing.boldMetal", false); 
    if (System.getProperty("substancelaf.useDecorations") == null) { 
    JFrame.setDefaultLookAndFeelDecorated(true); 
    JDialog.setDefaultLookAndFeelDecorated(true); 

    System.setProperty("sun.awt.noerasebackground", "true"); 
    SubstanceLookAndFeel.setCurrentTheme(new SubstanceLightAquaTheme());//设置当前的主题风格,同样我 们还可以设置当前的按钮形状,水印风格等等 
    } catch (Exception e) { 
    System.err.println("Oops! Something went wrong!"); 

    2.smooth look and feel 
    try { 
    UIManager.setLookAndFeel(new SmoothLookAndFeel()); 
    UIManager.put("swing.boldMetal", false); 
    } catch (Exception e) { 
    System.err.println("Oops! Something went wrong!"); 

    3. office/winxp/VisualStudio 2005 look and feel 
    try { 
    UIManager.setLookAndFeel("org.fife.plaf.Office2003.Office2003LookAndFeel"); 
    //UIManager.setLookAndFeel("org.fife.plaf.OfficeXP.OfficeXPLookAndFeel"); 
    //UIManager.setLookAndFeel("org.fife.plaf.VisualStudio2005.VisualStudio2005LookAndFeel"); 
    UIManager.put("swing.boldMetal", false); 
    } catch (Exception e) { 
    System.err.println("Oops! Something went wrong!"); 
    }
    参考资料:http://www.blogjava.net/dyerac/archive/2006/04/03/38986.html
      

  4.   

    Java绘制自定义按钮, 写了三种情况的, 当然还有其他的, 可以参考一下, 其实主要就是paintComponent和一些细节:import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;public class Test01 {    public static void main(String args[]) {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(300,
                    300);        JPanel panel = new JPanel();
            JButton button = new MyButton2("Button");
            button.setFocusable(false);
            button.setBorderPainted(false);
            button.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    System.out.println("Button is clicked");
                }
            });
            panel.add(button);
            frame.getContentPane().add(panel);        frame.setVisible(true);
        }
    }class MyButton2 extends JButton {
        private static final long serialVersionUID = 1965063150601339314L;    public MyButton2(String text) {
            super(text);
            setOpaque(false);
            setContentAreaFilled(false); // 这一句非常重要, 否则父类还会绘制按钮的区域.
        }    @Override
        protected void paintComponent(Graphics g) {
            int width = this.getWidth();
            int height = this.getHeight();        Graphics2D g2d = (Graphics2D) g;        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);        g2d.setColor(Color.GRAY);
            g2d.fillRoundRect(0,
                    0,
                    width,
                    height,
                    20,
                    20);        super.paintComponent(g); // 最后调用这个方法, 让父类绘制在按钮上绘制文字.
        }
    }
    Blur一个按钮:class MyButton2 extends JButton {
        private static final long serialVersionUID = 1965063150601339314L;    public MyButton2(String text) {
            super(text);
            // setOpaque(false);
            // setContentAreaFilled(false); // 这一句非常重要, 否则父类还会绘制按钮的区域.
        }    @Override
        protected void paintComponent(Graphics g) {
            BufferedImage buf = new BufferedImage(getWidth(), getHeight(),
                    BufferedImage.TYPE_INT_RGB);
            super.paintComponent(buf.getGraphics());        float[] my_kernel = { 0.10f, 0.10f, 0.10f, 0.10f, 0.20f, 0.10f, 0.10f,
                    0.10f, 0.10f };
            
            ConvolveOp op = new ConvolveOp(new Kernel(3, 3, my_kernel));
            Image img = op.filter(buf,
                    null);
            g.drawImage(img,
                    0,
                    0,
                    null);
        }
    }
    class ImageButton extends JButton {
        private static final long serialVersionUID = 1965063150601339314L;
        Image image = new ImageIcon("v4.png").getImage();    public ImageButton(String text) {
            super(text);
            setOpaque(false);
            setFocusable(false);
            setBorderPainted(false);        this.setPreferredSize(new Dimension(image.getWidth(this), image
                    .getHeight(this)));
        }    @Override
        protected void paintComponent(Graphics g) {
            g.drawImage(image,
                    0,
                    0,
                    null);
        }
    }