Design Button in Java (like in CSS)

后端 未结 2 2095
灰色年华
灰色年华 2021-01-07 03:10

For my project, I need to create customizable buttons. Except that I have a problem and I do not know how to solve it. I have a background image for my button and I would l

2条回答
  •  终归单人心
    2021-01-07 03:57

    You can scale the image and create an ImageIcon. Something like this:

    ImageIcon icon = new ImageIcon("img.png");
    Image scaledImg = icon.getImage().getScaledInstance(newWidth, newHeight,  java.awt.Image.SCALE_SMOOTH);  
    icon = new ImageIcon(scaledImg);
    JButton button = new JButton(icon);
    button.setHorizontalTextPosition(JButton.CENTER);
    button.setVerticalTextPosition(JButton.CENTER);
    

提交回复
热议问题