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
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 =
"" +
"" +
"" +
"
" +
"";
private static final String PLAYERONE =
"" +
"" +
"" +
"
" +
"";
private static final String PLAYERTWO =
"" +
"" +
"" +
"
" +
"";
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 :