简体   繁体   English

可点击的图像按钮

[英]Clickable image buttons

I'm making a puzzle app that needs to assign information to each piece. 我正在制作一个拼图应用程序,需要为每个作品分配信息。 Here is a photo. 这是一张照片。

In the photo I've just printed each puzzle piece to the screen. 在照片中,我刚刚将每个拼图打印到屏幕上。

The pieces are generated before they are displayed, the pieces vary in amount. 碎片是在显示之前生成的,碎片的数量不同。 eg I want a puzzle thats 9x9 pieces... So I need 9x9 or 81 buttons... 例如,我想要一个9x9的拼图...所以我需要9x9或81个按钮...

When selected the button or piece is highlighted. 选中后,按钮或片段会突出显示。 When selected and when other buttons are clicked such as Assign Location than the piece data and the action of the button are executed/processed. 当选择并单击其他按钮(例如“分配位置”)时,将执行/处理件数据和按钮的动作。 Only one button can be selected at a time.. 一次只能选择一个按钮。

I've got every working as far as data and functions. 就数据和功能而言,我已经做了所有工作。

I just need to figure out how to make the buttons and make they selectable. 我只需要弄清楚如何制作按钮并使它们可选即可。

See if this example gives you some ideas: 看看这个例子是否给你一些想法:

在此处输入图片说明

import java.awt.*;
import java.awt.image.BufferedImage;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;

class ImageButtonFun {

    public static void main(String[] args) throws Exception {
        URL url1 = new URL("http://i.stack.imgur.com/XZ4V5.jpg");
        final BufferedImage img1 = ImageIO.read(url1);
        URL url2 = new URL("http://i.stack.imgur.com/7bI1Y.jpg");
        final BufferedImage img2 = ImageIO.read(url2);
        final int sW = img2.getWidth();
        final int sH = img2.getHeight();
        final int tileW = 40;
        final int tileH = 40;
        Runnable r = new Runnable() {
        @Override
            public void run() {
                JPanel gui = new JPanel(new GridLayout(8,12));
                ButtonGroup bg = new ButtonGroup();
                for (int jj=0; jj<sH/tileH; jj++) {
                    for (int ii=0; ii<sW/tileW; ii++) {
                        Image im1 = img1.getSubimage(ii*tileW, jj*tileH, tileW, tileH);
                        Image im2 = img2.getSubimage(ii*tileW, jj*tileH, tileW, tileH);
                        JToggleButton button = new JToggleButton(new ImageIcon(im1));
                        button.setSelected( (ii%2==0 && jj%2==0) || (ii%2==1 && jj%2==1));
                        button.setSelectedIcon(new ImageIcon(im2));
                        button.setContentAreaFilled(false);
                        button.setBorderPainted(false);
                        button.setBorder(null);
                        bg.add(button);  // ensure only one button is selected at a time
                        gui.add(button);
                    }
                }

                JOptionPane.showMessageDialog(null, gui);
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency
        SwingUtilities.invokeLater(r);
    }
}

The images are seen in Example images for code and mark-up Q&As . 可以在示例图像的代码和标记Q&A中看到这些图像。

Edit 编辑

Only one button can be selected at a time.. 一次只能选择一个按钮。

Use a ButtonGroup for that. 为此使用ButtonGroup

The ButtonGroup component manages the selected/unselected state for a set of buttons. ButtonGroup组件管理一组按钮的选定/未选定状态。 For the group, the ButtonGroup instance guarantees that only one button can be selected at a time. 对于该组, ButtonGroup实例保证一次只能选择一个按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM