有没有办法在jbutton上设置一个jbutton



我很想知道这一点,因为我们正在Swing中制作游戏,并且出于任何原因,我们将地图瓦片制作成了jButtons而不是jPanels。现在我们想把单位放在它们上面,这样当单位在它们上面时,地图背景仍然显示出来。有人知道这是否可能吗?

好的,所以我不确定这真的是你想要的,也不确定你的应用程序当前是如何设置的,但这是一个将JButtons放在一起的例子(如果这不是你想要的东西,请考虑发布SSCCE并提供更多详细信息)。还有其他选择和更好的方法来处理这种事情,但由于您询问了JButton而不是JButton,这里有一个片段显示:

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

最好有一个包含按钮的面板。然后,您的面板可以使用鼠标事件侦听器在单击时执行操作。

使用jPanel的优点是可以在其中设置布局,这将使按钮在其中的定位更加容易。

好吧,可以把一个JButton放在另一个JBbutton上,但你看不到它。

CardLayout cl = new CardLayout();
JPanel jpnlMain = new JPanel(cl);
jpnlMain.add(new JButton(), "FIRST");
jpnlMain.add(new JButton(), "SECOND");

然后,也许您可以创建一个扩展Cardlayout或JPanel的类,使其同时显示这两个项。

编辑

做了一个小测试,HJere是按钮按钮概念中的一个按钮!!

主要类别:

import javax.swing.JFrame;

public class Test {
    public static void main(String[] arg0){
        SpecialButton sp = new SpecialButton();
        JFrame jf = new JFrame();
        jf.add(sp);
        jf.setVisible(true);
        jf.pack();
    }
}

特殊按钮类别:

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

    public class SpecialButton extends JButton{
        SpecialButton(){
            super();
            JButton jbtnMid = new JButton();
            JLabel jlblMid = new JLabel(new ImageIcon(this.getClass().getResource("/Images/arrowUpIcon.png")));
            jbtnMid .add(jlblMid);
            this.add(jbtnMid);
            this.setVisible(true);
        }
    }

最新更新