将JButton更改为自定义图片

时间:2014-01-04 02:53:33

标签: java swing jbutton

是否可以将JButton的外观更改为自定义图片?我想将此图片用作按钮:http://i.stack.imgur.com/JMQMX.png而不是:http://i.stack.imgur.com/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);


    }
}

我修好了。图片有问题,这就是为什么我看不到它......

3 个答案:

答案 0 :(得分:0)

(我还不能写评论所以我把它写成了一个awnser)我不确定这是否适用于此,但是当我试图在我的项目中更改Jlabel的图标时,我不得不将其设置为隐形并在更改图标后再次显示。也许如果你这样尝试就行了。

答案 1 :(得分:0)

    try {
        Playnow.setIcon(new ImageIcon(new URL("http://i.stack.imgur.com/JMQMX.png")));
    } catch (MalformedURLException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

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

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

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

希望这有助于:)

答案 2 :(得分:0)

试试这个:

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