gpt4 book ai didi

java - 如何将 JLabel 放入 JLabel 中或将 JLabel 分成正方形

转载 作者:行者123 更新时间:2023-12-02 13:15:41 26 4
gpt4 key购买 nike

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

最佳答案

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

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

关于java - 如何将 JLabel 放入 JLabel 中或将 JLabel 分成正方形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10244428/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com