- Java code
JButton loggin = new JButton("登入");; loggin.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ Icon icon1 = new ImageIcon(getClass().getResource("player1.png")); JLabel labelPlayer1 = new JLabel(icon1); labelPlayer1.setBounds(450,10,icon1.getIconWidth()+200,icon1.getIconHeight()); gamePanel.add(labelPlayer1); gamePanel.setBackground(Color.black); } });
开始gamePanel是一个JPanel类,开始的背景颜色是蓝色,然后添加一个label,开始时不会显示,可以改变了背景色之后又显示了,,为什么啊
------解决方案--------------------
刷新你jlabel的父容器,父容器.repaint();