JAVA如何制作梯形按钮?

解决方案 »

  1.   

    import java.awt.*; 
    import java.awt.geom.*; 
    import javax.swing.*; 
    import java.awt.event.*; 
    public class RoundButton extends JButton { 
        public RoundButton(String label) { 
            super(label); 
                  //ÏÂÃæµÄÓï¾ä½²ÊöÕâ¸ö°´Å¥±äΪһ¸öÔ²Ðζø²»ÊÇÍÖÔ²ÐΠ
            Dimension size = getPreferredSize(); 
            size.width = size.height = Math.max(size.width, size.height); 
            setPreferredSize(size); 
            //²»ÈÃJButton»­±³¾°¶øÔÊÐíÎÒÃÇÈ¥»­Ò»¸öÔ²±³¾° 
            setContentAreaFilled(false); 
        } 
        // »­³öÔ²µÄ±³¾°ºÍ±êÇ© 
        protected void paintComponent(Graphics g) { 
            if (getModel().isArmed()) { 
                g.setColor(Color.lightGray); 
            } else { 
                g.setColor(getBackground()); 
            } 
            g.fillOval(0, 0, getSize().width-1, getSize().height-1); 
            // ÔÚ½¹µãÉÏ»­³öÒ»¸ö±êÇ© 
            super.paintComponent(g); 
        } 
        // »­³öÒ»¸ö±ß¿ò 
        protected void paintBorder(Graphics g) { 
            g.setColor(getForeground()); 
            g.drawOval(0, 0, getSize().width-1, getSize().height-1); 
        } 
        // Õì²ìµ¥»÷ÇøÓò 
        Shape shape; 
        public boolean contains(int x, int y) { 
            // Èç¹û°´Å¥¸Ä±äÁ˳ߴ罫ÖØд´½¨Ò»¸öShape 
            if (shape == null || !shape.getBounds().equals(getBounds())) { 
                shape = new Ellipse2D.Float(0, 0, getWidth(), getHeight()); 
                        } 
            return shape.contains(x, y); 
        } 
        // ²âÊÔ 
        public static void main(String[] args) { 
            JButton button1 = new RoundButton("http://www.cn-java.com"); 
            JButton button2 = new RoundButton("»¶Ó­Äã³£À´×ö¿Í"); 
            button1.setBackground(Color.green); 
            button2.setBackground(Color.yellow); 
            // ´´½¨Ò»¸öFramÀ´ÏÔʾÕâ¸ö°´Å¥ 
            JFrame frame = new JFrame(); 
            frame.getContentPane().add(button1); 
            frame.getContentPane().add(button2); 
            frame.getContentPane().setLayout(new FlowLayout()); 
            frame.setSize(450, 350); 
            frame.setVisible(true); 
            //²âÊÔµ¥»÷ʼþ 
            button1.addActionListener(new ActionListener(){ 
                            public void actionPerformed(ActionEvent e){ 
                                    System.out.println("you click button1!"); 
                            } 
                                            }); 
            button2.addActionListener(new ActionListener(){ 
                            public void actionPerformed(ActionEvent e){ 
                                    System.out.println("you click button2!"); 
                            } 
                    });     } 
    }
      

  2.   

    import java.awt.*; 
    import java.awt.geom.*; 
    import javax.swing.*; 
    import java.awt.event.*; 
    public class RoundButton extends JButton { 
        public RoundButton(String label) { 
            super(label); 
                  
            Dimension size = getPreferredSize(); 
            size.width = size.height = Math.max(size.width, size.height); 
            setPreferredSize(size); 
     
            setContentAreaFilled(false); 
        } 
      
        protected void paintComponent(Graphics g) { 
            if (getModel().isArmed()) { 
                g.setColor(Color.lightGray); 
            } else { 
                g.setColor(getBackground()); 
            } 
            g.fillOval(0, 0, getSize().width-1, getSize().height-1); 
      
            super.paintComponent(g); 
        }     protected void paintBorder(Graphics g) { 
            g.setColor(getForeground()); 
            g.drawOval(0, 0, getSize().width-1, getSize().height-1); 
        }     Shape shape; 
        public boolean contains(int x, int y) { 
      
            if (shape == null || !shape.getBounds().equals(getBounds())) { 
                shape = new Ellipse2D.Float(0, 0, getWidth(), getHeight()); 
                        } 
            return shape.contains(x, y); 
        }     public static void main(String[] args) { 
            JButton button1 = new RoundButton("http://www.cn-java.com"); 
            JButton button2 = new RoundButton("»¶Ó­Äã³£À´×ö¿Í"); 
            button1.setBackground(Color.green); 
            button2.setBackground(Color.yellow); 
        
            JFrame frame = new JFrame(); 
            frame.getContentPane().add(button1); 
            frame.getContentPane().add(button2); 
            frame.getContentPane().setLayout(new FlowLayout()); 
            frame.setSize(450, 350); 
            frame.setVisible(true);         button1.addActionListener(new ActionListener(){ 
                            public void actionPerformed(ActionEvent e){ 
                                    System.out.println("you click button1!"); 
                            } 
                                            }); 
            button2.addActionListener(new ActionListener(){ 
                            public void actionPerformed(ActionEvent e){ 
                                    System.out.println("you click button2!"); 
                            } 
                    });     } 
    }