如何将JLabel放入JLabel或将JLabel划分为正方形

时间:2012-04-20 10:24:19

标签: java swing jlabel

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

2 个答案:

答案 0 :(得分: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();
            }
        });
    }
}

以下是相同的输出:

TWO PLAYERS

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

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 OVER JLABEL

答案 1 :(得分:3)

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