Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 更新JLabel图标_Java_Swing_Icons_Actionlistener_Jlabel - Fatal编程技术网

Java 更新JLabel图标

Java 更新JLabel图标,java,swing,icons,actionlistener,jlabel,Java,Swing,Icons,Actionlistener,Jlabel,我不熟悉创建GUI,在刷新JLabel上的图像时遇到问题。我也读过有同样问题的人提出的其他问题,但没有一个答案对我有帮助。我有一个程序,每次单击JButton时都会滚动一个骰子,如果结果是一个,我希望JLabel的图像改变。以下是我的GUI类的构造函数: private Panel panel; private Label label; private TextField text; private JButton roll; private ArrayList<ImageIcon>

我不熟悉创建GUI,在刷新JLabel上的图像时遇到问题。我也读过有同样问题的人提出的其他问题,但没有一个答案对我有帮助。我有一个程序,每次单击JButton时都会滚动一个骰子,如果结果是一个,我希望JLabel的图像改变。以下是我的GUI类的构造函数:

private Panel panel;
private Label label;
private TextField text;
private JButton roll;
private ArrayList<ImageIcon> deck;
private ArrayList<ImageIcon> discard;
private JLabel pic;
private JFrame f;
private ImageIcon now;
public Planechase()
{
    f = new JFrame("Planechase");
    deck = new ArrayList<ImageIcon>();
    populate();
    Collections.shuffle(deck);
    discard = new ArrayList<ImageIcon>();
    label = new Label("Planechase");
    text = new TextField(":)",8);
    text.setEditable(false);
    roll = new JButton("Roll");
    roll.addActionListener(
        new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
                int i = (int)(Math.random()*6)+1;
                System.out.println(i);
                if(i==1)
                {
                    text.setText("Planeswalk");
                    discard.add(now);
                    now = deck.remove(0);
                    pic = new JLabel(now);
                    pic.updateUI();
                }
                else if(i==6)
                {
                    text.setText("Chaos");
                }
                else
                {
                    text.setText("Blank");
                }
            }
        });
    now = deck.remove(0);
    pic = new JLabel(now);
    f.getContentPane().setLayout(new FlowLayout());
    f.getContentPane().add(pic);
    f.getContentPane().add(text);
    f.getContentPane().add(roll);
}
专用面板;
自有品牌;
私有文本字段文本;
私用钮扣卷;
私人露天甲板;
私有数组列表丢弃;
私人JLabel公司;
私有jf框架;
私人图像图标现在;
公共飞机追逐()
{
f=新JFrame(“Planechase”);
deck=newarraylist();
填充();
收藏。洗牌(牌组);
discard=新的ArrayList();
标签=新标签(“Planechase”);
text=新文本字段(“:”,8);
text.setEditable(false);
滚动=新的按钮(“滚动”);
roll.addActionListener(
新建ActionListener(){
已执行的公共无效操作(操作事件e)
{
int i=(int)(Math.random()*6)+1;
系统输出打印LN(i);
如果(i==1)
{
text.setText(“刨花”);
放弃。添加(现在);
现在=甲板。移除(0);
pic=新的JLabel(现在);
pic.updateUI();
}
else如果(i==6)
{
text.setText(“混乱”);
}
其他的
{
text.setText(“空白”);
}
}
});
现在=甲板。移除(0);
pic=新的JLabel(现在);
f、 getContentPane().setLayout(新的FlowLayout());
f、 getContentPane().add(pic);
f、 getContentPane()。添加(文本);
f、 getContentPane()。添加(滚动);
}

正如在类似问题中所建议的那样,我尝试使用上面的updateUI(),但图片没有改变。我还应该尝试什么?

您正在
ActionListener
中创建一个新的
Jlabel
,但没有将其添加到容器中。您可以使用更新
图标


一些旁注:

  • 当您删除
    图标
    以在
    JLabel
    中使用时,如果不重新添加,最终将用尽可用的图标,从而导致
    索引自动边界异常
  • 通常,混合AWT和Swing组件不是一个好主意。较旧的AWT组件通常不尊重组件的z顺序,并且通常隐藏其轻量级邻居。看

+1,周日早上挑剔:从技术上讲,如今混合很好——但大多数时候并不是有意的:——)
pic.setIcon(now);