Image not displayed on JPanel

 class Deal implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
             dl.setDeck();
             dl.shuffle();
             dl.firstDraw(pl);

             for(Card c:pl.showHand())
             panelplay.add(new JLabel(c.getImageIcon()));

             panelplay.validate();
        }
    }

This is an event handler for Jbutton. The pl.showHand () method returns an ArrayList for the user-defined Map class. Inserting println () inside the loop shows printing, so the code is executed, but the Panelplay panel does not display the map.

+3
source share
3 answers

You do not want to add JLabel to the ActionListener.

You want to use the already added setText () method of JLabel in the ActionListener.

You define all Swing components once when you create the GUI.

0
source

? . , FlowLayout, , .

, - panel.removeAll() . :

panel.revalidate();
panel.repaint();

, , - , setIcon().

+2

, , Swing Tutorial, .
JLabel ... void setIcon (Icon)
getIcon()

SplitPaneDemo , , JNLP, .

+1

Source: https://habr.com/ru/post/1750556/


All Articles