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();
}
}
这是Jbutton的事件处理程序。方法pl.showHand()返回用户定义的类'Card'的ArrayList。在循环中插入println()会显示print,因此代码正在执行,但Panel panelplay不显示卡片图像。
答案 0 :(得分:2)
面板上的现有标签怎么样?你不删除它们。我猜你正在使用FlowLayout,标签只是添加到面板的末尾,所以你看不到它们。
因此,一种解决方案是在将标签添加回面板之前使用panel.removeAll()。然后我用:
panel.revalidate();
panel.repaint();
或者之前建议的更好的选择是不替换标签,而只是使用setIcon()方法替换图标。
答案 1 :(得分:1)
按照吉尔伯特的说法,看看Swing Tutorial part that concerns Labels
JLabel有以下方法......
void setIcon(Icon)
图标getIcon()
Also look at the SplitPaneDemo它完全符合您的要求,您甚至可以run it with JNLP to see。
答案 2 :(得分:0)
您不想在ActionListener中添加JLabel。
您想在ActionListener中使用已添加的JLabel setText()方法。
在创建GUI时,您可以定义一次所有Swing组件。