【问题标题】:Add a button over image在图像上添加按钮
【发布时间】:2015-12-08 09:00:56
【问题描述】:

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

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);

    }
}

【问题讨论】:

  • 我宁愿分割图像并将子图像用作按钮或标签的图标。然后将它们全部安排在GridBagLayout 中。这是一个使用更简单布局的example (GridLayout)。
  • 另见this question..

标签: java image swing jbutton overlay


【解决方案1】:

如何在使用 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 中。您有责任确保按钮适合图像,否则按钮将无法正确显示。

问题是您真的需要将标签添加到面板还是应该将标签添加到框架?

【讨论】:

    【解决方案2】:

    我宁愿分割图像并将子图像用作按钮或标签的图标。然后将它们全部安排在GridBagLayout 中。

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

    (它使用带有透明阴影的白色图标表示鼠标悬停,黑色表示按下。截屏时鼠标指向左前方。留作练习供读者实施在由 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);
        }
    }
    

    【讨论】:

    • 加一个??基础代数???,你的好问题和答案在这里和这里:-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多