gpt4 book ai didi

java - 将 JButton 外观更改为自定义图片

转载 作者:太空宇宙 更新时间:2023-11-04 15:15:15 25 4
gpt4 key购买 nike

是否可以将 JButton 的外观更改为自定义图片?我想用这张图片作为按钮:/image/JMQMX.png而不是:/image/MXKUF.png

我自己也尝试过,没有成功。请帮我! :)

这是我的代码:

    package launcher;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.JButton;

import java.awt.event.MouseAdapter;

public class Launcher extends JFrame {

/**
*
*/
private static final long serialVersionUID = 1L;

private JPanel contentPane;

static Point mouseDownCompCoords;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
mouseDownCompCoords = null;
final Launcher frame = new Launcher();
frame.setResizable(false);
frame.setUndecorated(true);
frame.setBackground(new Color(0, 255, 0, 0));
frame.setVisible(true);
frame.addMouseListener(new MouseListener() {
public void mouseReleased(MouseEvent e) {
mouseDownCompCoords = null;
}

public void mousePressed(MouseEvent e) {
mouseDownCompCoords = e.getPoint();
}

public void mouseExited(MouseEvent e) {
}

public void mouseEntered(MouseEvent e) {
}

public void mouseClicked(MouseEvent e) {
}
});

frame.addMouseMotionListener(new MouseMotionListener() {
public void mouseMoved(MouseEvent e) {
}

public void mouseDragged(MouseEvent e) {
Point currCoords = e.getLocationOnScreen();
frame.setLocation(currCoords.x - mouseDownCompCoords.x,
currCoords.y - mouseDownCompCoords.y);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public Launcher() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 841, 593);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);

JLabel Design = new JLabel("New label");
Design.setIcon(new ImageIcon("C:\\Users\\Daniel\\Pictures\\Launcher2.png"));
Design.setBounds(-158, -22, 1047, 592);
contentPane.add(Design);

JButton Playnow = new JButton("");
Playnow.setOpaque(false);
Playnow.setIcon(new ImageIcon("C:\\Users\\Daniel\\Pictures\\Playnow.png"));

Playnow.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
//Playnow.setIcon(new ImageIcon("C:\\Users\\Daniel\\Pictures\\PlaynowHover.png"));
}
@Override
public void mouseClicked(MouseEvent e) {
//Playnow.setIcon(new ImageIcon("C:\\Users\\Daniel\\Pictures\\PlaynowHover.png"));
}
});
Playnow.setBounds(258, 442, 301, 46);
contentPane.add(Playnow);

JButton Exit = new JButton("");
Exit.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
final Launcher frame = new Launcher();
frame.dispose();
System.exit(0);
}
});
Exit.setBounds(766, 60, 19, 17);
contentPane.add(Exit);


}
}

我修好了。图片有问题,所以我看不到它...

最佳答案

    try {
Playnow.setIcon(new ImageIcon(new URL("/image/JMQMX.png")));
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

当我尝试使用你的代码时,这对我有用。但是,如果您想在本地加载它,您应该尝试:

Playnow.setIcon(new ImageIcon(getClass().getResource("test.png")));

test.png 与此代码所在的类文件位于同一目录中。

希望这有帮助:)

关于java - 将 JButton 外观更改为自定义图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20916268/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com