简体   繁体   中英

how to put a JLabel inside a JLabel or divide a JLabel into squares

I have created a snake and ladder game and it works well, but now I want to add a little bit modification to it. I want each player to have two pieces/players instead of one. The new rule now becomes, two pieces of the same player can occupy the same square, hence I need a way to show them in the same square. I used JLable to identify each square, but now I want each square to hold more than one piece. That is, if a square is occupied by more than one piece the user must be able to click on a piece and select that piece only. Is there a way to divide a square/JLable into several squares/JLabels? I am not sure if JLabel is the best to use here... Any suggestions please???

Here try this code example, will this do for your case :

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class LabelOverLabel
{
    public static final String HTML =
        "<html>" +
        "<style type'text/css'>" +
        "body, html { padding: 0px; margin: 0px; }" +
        "</style>" +
        "<body>" +
        "<img src='http://pscode.org/media/starzoom-thumb.gif'" +
        " width=320 height=240>" +
        "";

    private static final String PLAYERONE = 
        "<html>" +
        "<style type'text/css'>" +
        "body, html { padding: 0px; margin: 0px; }" +
        "</style>" +
        "<body>" +
        "<img src='http://pscode.org/media/citymorn2.jpg'" +
        " width=160 height=120>" +
        "";

    private static final String PLAYERTWO = 
        "<html>" +
        "<style type'text/css'>" +
        "body, html { padding: 0px; margin: 0px; }" +
        "</style>" +
        "<body>" +
        "<img src='http://pscode.org/media/citymorn1.jpg'" +
        " width=160 height=120>" +
        ""; 

    private JLabel playerOneLabel;
    private JLabel playerTwoLabel;

    private void createAndDisplayGUI()
    {
        JFrame frame = new JFrame("JLabel Over JLabel");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        final JLabel bottomLabel = new JLabel(HTML);
        bottomLabel.setLayout(new FlowLayout(FlowLayout.LEFT, 2, 2));

        playerOneLabel = new JLabel(PLAYERONE);
        bottomLabel.add(playerOneLabel);

        JButton insertPlayer = new JButton("INSERT");
        insertPlayer.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                playerTwoLabel = new JLabel(PLAYERTWO);
                bottomLabel.add(playerTwoLabel);
                bottomLabel.revalidate();
                bottomLabel.repaint();
            }
        });

        frame.getContentPane().add(bottomLabel, BorderLayout.CENTER);
        frame.getContentPane().add(insertPlayer, BorderLayout.PAGE_END);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new LabelOverLabel().createAndDisplayGUI();
            }
        });
    }
}

Here is the output of the same :

两个球员

Another way to achieve this is :

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class LabelOverLabel
{   
    private JLabel playerOneLabel;
    private JLabel playerTwoLabel;  
    private Icon[] icons = {UIManager.getIcon("OptionPane.informationIcon"),
                            UIManager.getIcon("OptionPane.errorIcon"),
                            UIManager.getIcon("OptionPane.warningIcon")};

    private void createAndDisplayGUI()
    {
        JFrame frame = new JFrame("JLabel Over JLabel");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        final JLabel bottomLabel = new JLabel("I am a JLabel");
        bottomLabel.setBorder(BorderFactory.createLineBorder(Color.BLUE.darker(), 5));
        //bottomLabel.setIcon(icons[0]);
        bottomLabel.setLayout(new FlowLayout(FlowLayout.LEFT, 2, 2));

        playerOneLabel = new JLabel();
        playerOneLabel.setIcon(icons[1]);
        bottomLabel.add(playerOneLabel);

        JLabel secondLabel = new JLabel("1");
        bottomLabel.add(secondLabel);

        JButton insertPlayer = new JButton("INSERT");
        insertPlayer.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                playerTwoLabel = new JLabel();
                playerTwoLabel.setIcon(icons[2]);
                bottomLabel.add(playerTwoLabel);
                bottomLabel.revalidate();
                bottomLabel.repaint();
            }
        });

        frame.getContentPane().add(bottomLabel, BorderLayout.CENTER);
        frame.getContentPane().add(insertPlayer, BorderLayout.PAGE_END);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new LabelOverLabel().createAndDisplayGUI();
            }
        });
    }
}

Output is :

JLABEL OVER JLABEL

您还可以创建自定义标签并将每个用户绘制为方形。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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