2

我创建了一个蛇和梯子游戏,它运行良好,但现在我想对其进行一些修改。我希望每个玩家有两个棋子/玩家而不是一个。新规则现在变成了,同一玩家的两个棋子可以占据同一个方格,因此我需要一种方法将它们显示在同一个方格中。我使用 JLable 来识别每个方格,但现在我希望每个方格可以容纳多个。也就是说,如果一个方格被多个棋子占据,则用户必须能够单击一个棋子并仅选择该棋子。有没有办法将一个正方形/JLable 分成几个正方形/JLabel?我不确定 JLabel 是否最适合在这里使用...有什么建议吗???

4

2 回答 2

5

在这里尝试这个代码示例,这是否适合您的情况:

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

这是相同的输出:

两名球员

实现这一目标的另一种方法是:

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

输出是:

JLABEL 超过 JLABEL

于 2012-04-20T11:07:39.700 回答
3

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

于 2012-04-20T10:46:37.157 回答