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

后端 未结 2 1783
故里飘歌
故里飘歌 2020-12-04 01:12

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

相关标签:
2条回答
  • 2020-12-04 01:33

    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 :

    TWO PLAYERS

    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

    0 讨论(0)
  • 2020-12-04 01:35

    You can also create a custom label and draw each user as square.

    0 讨论(0)
提交回复
热议问题