有没有一种方法可以将jbutton设置在jbutton的顶部? [英] is there a way to set a jbutton on top of a jbutton?

查看:168
本文介绍了有没有一种方法可以将jbutton设置在jbutton的顶部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道这一点,因为我们在Swing中制作了一个游戏,无论出于何种原因,我们都将地图图块做成了jButtons而不是jPanels.现在,我们想将单位放在它们的顶部,这样当单位位于它们的顶部时,地图背景仍会显示出来.有人知道这是否可能吗?

I am wondering about this since we are making a game in Swing and we made our map tiles into jButtons instead of jPanels for whatever reason. Now we want to put units on top of them so the map background is still shown when the unit is on top of them. Anyone know if this is possible?

推荐答案

好,所以我不确定这真的是您要查找的内容以及您的应用程序当前的设置方式,但这是一个启用JButton的示例彼此顶部(如果这不是您想要的,请考虑发布 SSCCE 并提供更多详细信息).还有其他替代方法和更好的方法来处理此类问题,但是由于您通过JButton询问了JButton,因此以下代码段显示了这一点:

OK, so I am not sure this is really what you are looking for and how your application is currently set up, but this is an example to have JButtons on top of each other (if this is not what you are looking for, consider posting an SSCCE and give more details). There are other alternatives and better way to handle such thing, but since you asked JButton over a JButton, here is a snippet showing that:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.plaf.ButtonUI;
import javax.swing.plaf.basic.BasicButtonUI;

public class TestButtonGrid {

    protected int x;
    protected int y;

    protected void initUI() throws MalformedURLException {
        final JFrame frame = new JFrame();
        frame.setTitle(TestButtonGrid.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final JPanel panel = new JPanel(new GridBagLayout());
        ImageIcon icon = new ImageIcon(new URL("http://manhack-arcade.net/pivot/isdrawing/tile_bluerock.png"));
        ImageIcon unit = new ImageIcon(new URL("http://static.spore.com/static/image/500/642/783/500642783372_lrg.png"));
        ButtonUI ui = new BasicButtonUI();
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                gbc.gridx = i;
                gbc.gridy = j;
                JButton button = new JButton(icon);
                button.setBorderPainted(false);
                button.setPreferredSize(new Dimension(icon.getIconWidth(), icon.getIconHeight()));
                button.setUI(ui);
                button.setOpaque(false);
                panel.add(button, gbc);
                if (i == j) {
                    // Choose a layout that will center the icon by default
                    button.setLayout(new BorderLayout());
                    JButton player = new JButton(unit);
                    player.setPreferredSize(new Dimension(unit.getIconWidth(), unit.getIconHeight()));
                    player.setBorderPainted(false);
                    player.setUI(ui);
                    player.setOpaque(false);
                    button.add(player);
                }
            }
        }
        frame.add(panel);
        frame.setResizable(false);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    new TestButtonGrid().initUI();
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }
            }
        });
    }

}

这篇关于有没有一种方法可以将jbutton设置在jbutton的顶部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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