The metalButton that swing comes with is very ugly and cannot meet our actual needs, so we need to customize our favorite buttons, such as a picture button, etc. As shown in the figure below.
Then explain how to make it.
(1) Find some good button pictures, but the buttons may be inside the picture, so we need to use Meitu Xiuxiu or PS to pick out the buttons. As shown in the figure below:
(2) Just save it as a transparent background.
(3) Then write a my button class:
import javax.imageio.ImageIO; import javax.swing.*; import java.awt.*; import java.awt.image.BufferedImage; public class newButton extends JButton{ ImageIcon img; public newButton(String icon){ super(); this.img = new ImageIcon(Demo.class.getResource(icon)); setBorderPainted(false); setContentAreaFilled(false); setOpaque(false); setSize(img.getIconWidth(),img.getIconHeight()); try{ bi = ImageIO.read(Demo.class.getResource(icon)); }catch(Exception e){ JOptionPane.showMessageDialog(this,"Maybe the image file does not exist", "ImageIO exception",JOptionPane.ERROR_MESSAGE); System.exit(0); } } @Override public void paintComponent(Graphics g){ if(this.getModel().isPressed()){ g.drawImage(img.getImage(),1,1,this); }else{ g.drawImage(img.getImage(),0,0,this); } super.paintComponent(g); } BufferedImage bi ; int rgb,alpha; /** * Set the button click range only in the non-transparent area of the picture. */ @Override public boolean contains(int x,int y){ try{ rgb = bi.getRGB(x,y); alpha = (rgb>>24)&0xFF; if(alpha==0){ return false; }else{ return true; } }catch(ArrayIndexOutOfBoundsException e){ //When a transparent area is searched, getRGB throws the following table out of bounds exception return false; } } } The above program rewritten the contains function to ensure that the party mouse click area is limited to the valid area of the picture.
(4) Write a Demo class test:
import javax.swing.*; import java.awt.*; import java.net.URL; public class Demo { public Demo(){ JFrame jf=new JFrame("Arbitrary shape picture button test"); jf.setBounds(500,200,700,500); myJPanel jp = new myJPanel(Demo.class.getResource("bg.jpg")); jp.setLayout(null); newButton jb1 = new newButton("bt1.png"); jb1.setLocation(44,44); jp.add(jb1); jb1 = new newButton("snowFlower.png"); jb1.setLocation(200,44); jp.add(jb1); jb1 = new newButton("bt2.png"); jb1.setLocation(350,64); jp.add(jb1); jb1 = new newButton("bt3.png"); jb1.setLocation(450,64); jp.add(jp); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setVisible(true); } public static void main(String[] args){ new Demo(); } private class myJPanel extends JPanel{ ImageIcon bg; public myJPanel(URL bg) { this.setOpaque(false);//To be set to transparent. this.bg = new ImageIcon(bg); } // Used to set background image @Override public void paintComponent(Graphics g){ g.drawImage(bg.getImage(),0,0,this.getWidth(),this.getHeight(),this); super.paintComponent(g); } } }The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.