在图像上添加一个按钮

时间:2015-09-11 18:04:35

标签: java image swing jbutton overlay

如何在使用JLabel创建的图像上添加按钮? 即使我在“.setbounds”上放置相同的坐标,图像通常也会出现在按钮后面。

soccer field

public class Tatica extends JFrame {
    JPanel jl = new JPanel();
    JLabel jp = new JLabel();

public Tatica(){
jl.setLayout(null);
    jp.setIcon(new ImageIcon("C:\\Users\\LG\\workspace\\Teste\\src\\snippet\\asdiuashd.Jpg"));
    jl.add(jp);
    add(jl);
    jp.setBounds(100, 0, 1000, 1000);

      validate();
 JButton team2 = new JButton("Gk");
     jl.add(team2);
     team2.setBounds(400, 0, 100, 20);
     team2.setVisible(true);
     team2.setLayout(null);


      JButton dc = new JButton("Dc");
      jl.add(dc);
      dc.setBounds(300, 200, 100, 20);
      dc.setVisible(true);

      JButton dc2 = new JButton("Dc");
      jl.add(dc2);
      dc2.setBounds(500, 200, 100, 20);
      dc2.setVisible(true);

      JButton dl = new JButton("Dl");
      jl.add(dl);
      dl.setBounds(100, 200, 100, 20);
      dl.setVisible(true);

      JButton dr = new JButton("Dr");
      jl.add(dr);
      dr.setBounds(700, 200, 100, 20);
      dr.setVisible(true);

    }
}

2 个答案:

答案 0 :(得分:2)

  

如何在使用JLabel创建的图像上添加按钮?

将按钮添加到标签,而不是面板。

所以基本代码是:

JPanel panel = new JPanel();
JLabel label = new JLabel( new ImageIcon(...) );
label.setLayout( new FlowLayout() );
JButton button1 = new JButton("Button1");
label.add(button1);
JButton button2 = new JButton("Button1");
label.add(button2);

所以现在标签将是图像的大小。这些按钮将显示在图像上的FlowLayout中。您有责任确保按钮适合图像,否则按钮将无法正确显示。

问题是你真的需要在面板上添加标签,还是只需将标签添加到框架中?

答案 1 :(得分:2)

我改为将图像切片并将子图像用作按钮或标签的图标。然后将它们全部安排在GridBagLayout中。

这是我的意思的一个例子(特别感谢@camickr解决我的布局代码中的错误!):

enter image description here

(它使用一个带有透明阴影的图标用于鼠标悬停,黑色用于按下。当拍摄屏幕截图时,鼠标指向左中心。它留给练习者以便读者实施在由81个子图像组成的GUI中,根据需要在标签和按钮之间切换所需的逻辑。)

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

import java.net.URL;
import javax.imageio.ImageIO;

public class SoccerField {

    private JPanel ui = null;
    int[] x = {0, 35, 70, 107, 142, 177, 212, 247, 282, 315};
    int[] y = {0, 45, 85, 140, 180, 225, 265, 280, 320, 345};
    Color brighter = new Color(255,255,255,92);
    Color darker = new Color(0,0,0,92);

    SoccerField() {
        initUI();
    }

    public void initUI() {
        if (ui != null) {
            return;
        }

        ui = new JPanel(new GridBagLayout());
        ui.setBackground(Color.RED);

        try {
            URL url = new URL("http://i.stack.imgur.com/9E5ky.jpg");
            BufferedImage img = ImageIO.read(url);
            BufferedImage field = img.getSubimage(100, 350, 315, 345);

            BufferedImage[] bi = subSampleImageColumns(field);
            BufferedImage[][] fieldParts = new BufferedImage[bi.length][];
            for (int ii=0; ii<bi.length; ii++) {
                fieldParts[ii] = subSampleImageRows(bi[ii]);
            }
            for (int ii=0; ii<fieldParts[0].length; ii++) {
                for (int jj=0; jj<fieldParts.length; jj++) {
                    addImageToPanel(ui, fieldParts[ii][jj], ii, jj);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private void addImageToPanel(
            JPanel panel, BufferedImage img, int row, int col) {
        Insets insets = new Insets(0,0,0,0);
        double weighty = img.getHeight()==40 ? .5 : .1;
        GridBagConstraints gbc = new GridBagConstraints(
                row, col, 
                1, 1, 
                .5, weighty, 
                GridBagConstraints.CENTER, 
                GridBagConstraints.BOTH, 
                insets, 0, 0);
        ImageIcon ii = new ImageIcon(img);
        JButton b = new JButton(ii);
        b.setRolloverIcon(new ImageIcon(getFadeImage(img, brighter)));
        b.setPressedIcon(new ImageIcon(getFadeImage(img, darker)));

        b.setBorder(null);
        b.setBorder(new EmptyBorder(0, 0, 0, 0));
        b.setBorderPainted(false);
        b.setContentAreaFilled(false);
        b.setFocusPainted(false);
        panel.add(b, gbc);
    }

    private BufferedImage[] subSampleImageColumns(BufferedImage img) {
        BufferedImage[] imageRows = new BufferedImage[x.length - 1];
        for (int ii = 0; ii < x.length - 1; ii++) {
            BufferedImage bi = img.getSubimage(
                    x[ii], 0, x[ii + 1] - x[ii], img.getHeight());
            imageRows[ii] = bi;
        }

        return imageRows;
    }

    private BufferedImage[] subSampleImageRows(BufferedImage img) {
        BufferedImage[] imageRows = new BufferedImage[y.length - 1];
        for (int ii = 0; ii < y.length - 1; ii++) {
            BufferedImage bi = img.getSubimage(
                    0, y[ii], img.getWidth(), y[ii + 1] - y[ii]);
            imageRows[ii] = bi;
        }

        return imageRows;
    }

    private BufferedImage getFadeImage(BufferedImage img, Color clr) {
        BufferedImage bi = new BufferedImage(
                img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics g = bi.getGraphics();

        g.drawImage(img, 0, 0, ui);
        g.setColor(clr);
        g.fillRect(0, 0, img.getWidth(), img.getHeight());
        g.dispose();

        return bi;
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                SoccerField o = new SoccerField();

                JFrame f = new JFrame(o.getClass().getSimpleName());
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}