is there a way to set a jbutton on top of a jbutto

2019-08-17 19:03发布

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?

3条回答
时光不老,我们不散
2楼-- · 2019-08-17 19:24

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

}
查看更多
家丑人穷心不美
3楼-- · 2019-08-17 19:27

It might be a better idea to have a panel which contains buttons. Your panel could then use a mouse event listener to perform an action when clicked on.

The advantage of using a jPanel is that you can set a layout within it, which will make positioning your buttons in it much easier.

查看更多
够拽才男人
4楼-- · 2019-08-17 19:37

Well it is possible to put a JButton Over an other JButton you just wont be able to see it.

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

Then Maybe you could create a Class that Extends Either the Cardlayout or the JPanel to make it show both Items.

EDIT

Made a little Test and HJere is a Button in a Button Buttonception!!

Main class:

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

Special Button Class:

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);
        }
    }
查看更多
登录 后发表回答