可点击图像按钮 [英] Clickable image buttons

查看:392
本文介绍了可点击图像按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做,需要分配信息,每一块拼图应用程序。这是一张照片。

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.

被显示之前它们的片产生的碎片在量而变化。例如我想这就是拼图块9x9的...所以我需要9x9的或81按钮...

The pieces are generated before they are displayed, the pieces vary in amount. e.g. I want a puzzle thats 9x9 pieces... So I need 9x9 or 81 buttons...

在选择按钮或片突出。如果选择与当其他按钮被点击,如指定位置比一块数据和执行/处理按钮的作用。只有一个按钮可以在一次选择..

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

中的图像出现在示例图像,code和标记问答集。作为

只有一个按钮可以在一次选择..

Only one button can be selected at a time..

使用一个 的ButtonGroup 的为。

Use a ButtonGroup for that.

的ButtonGroup 组件管理一组按钮选中/未选中状态。对于组,即的ButtonGroup 实例保证只有一个按钮,可以在选定的时间。

The ButtonGroup component manages the selected/unselected state for a set of buttons. For the group, the ButtonGroup instance guarantees that only one button can be selected at a time.

这篇关于可点击图像按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆