I recently read this topic ( Creating a custom button in Java ) when creating custom buttons in java, extending the JButton class, however, all solutions to this topic use graphics drawn in java.
I wanted my button to be based on the button image that I painted in Photoshop. So I tried to apply what I read in this thread with this result:
import javax.swing.*; import java.awt.*; public class nextButton extends JButton { @Override protected void paintComponent(Graphics g) { Image image = new ImageIcon("nextButton.png").getImage(); g.drawImage(image,0,0,this); } @Override public Dimension getPreferredSize() { Dimension size = super.getPreferredSize(); size.setSize(75, 150); return size; } }
When I launch the main program by adding this button to JPanel, it is not displayed. I suggest this could be one of several reasons:
a) Does the JButton size not match the image? b) I did not upload the image properly. In the notes that my lecturer gave me, he writes out the image code with the image only "imageName.png" without the path to the file, so I have no idea whether to do this or how the program will know how to load the image, c) What something else that is still known to me.
If anyone knows how to solve this, I would be very grateful.
Many thanks!
source share