Java 如何准确地制作带有图像的JButton?

Java 如何准确地制作带有图像的JButton?,java,swing,jbutton,embedded-resource,Java,Swing,Jbutton,Embedded Resource,我是java新手 我想让我的图像在我的项目中充当按钮。就像windows phone互动程序一样。我的框架背景是黑色的。我安排了6个按钮。我还添加了一个图像,但在图像周围有一些空白作为边框。以下是我使用的代码: ImageIcon image = new ImageIcon(getClass().getResource("yellowimage.png"); JButton button = new JButton(image); 我不要那些空位。我只想按钮是完全的形象,并希望键入“点击我”的

我是java新手

我想让我的图像在我的项目中充当按钮。就像windows phone互动程序一样。我的框架背景是黑色的。我安排了6个按钮。我还添加了一个图像,但在图像周围有一些空白作为边框。以下是我使用的代码:

ImageIcon image = new ImageIcon(getClass().getResource("yellowimage.png");
JButton button = new JButton(image);
我不要那些空位。我只想按钮是完全的形象,并希望键入“点击我”的按钮。我该怎么做?

试试这个:

JButton button = new JButton(image);
button.setContentAreaFilled(false);
button.setBorder(null);
您还可以在按下按钮时更改按钮图像:

button.setPressedIcon(pressedIcon);
试试这个:

JButton button = new JButton(image);
button.setContentAreaFilled(false);
button.setBorder(null);
您还可以在按下按钮时更改按钮图像:

button.setPressedIcon(pressedIcon);

您可以尝试使用图像制作JButton

Icon yourIcon = new ImageIcon("yourFile.gif");
JButton button2 = new JButton(yourIcon);

您可以尝试使用图像制作JButton

Icon yourIcon = new ImageIcon("yourFile.gif");
JButton button2 = new JButton(yourIcon);

您可以使用setMargin()最小化图像周围的边框


您可以使用setMargin()最小化图像周围的边框

试试这个

JButton button = new JButton();
    ImageIcon icon = new ImageIcon(
            ClassName.class.getResource("image.png"));
    button.setIcon(icon);
    button.setBackground(new Color(0,0,0,0));
    button.setBorderPainted(false);
试试这个

JButton button = new JButton();
    ImageIcon icon = new ImageIcon(
            ClassName.class.getResource("image.png"));
    button.setIcon(icon);
    button.setBackground(new Color(0,0,0,0));
    button.setBorderPainted(false);

根据您试图实现的目标,您还可以使用
setboordpainted
setFocusPainted
setMargins
根据您试图实现的目标,您还可以使用
setboordpainted
setFocusPainted
setMargins