Java JLabel和JPanel-动态使用GUI显示不同的图像

Java JLabel和JPanel-动态使用GUI显示不同的图像,java,user-interface,windowbuilder,dice,Java,User Interface,Windowbuilder,Dice,我将再次发布这篇文章,这次尽量做到更精确和简洁。我已经安装了WindowBuilder,并一直在使用它生成GUI代码 所以我有我的图形用户界面设置和一切进行。WindowBuilder会自动生成一个名为initialize的方法,这就是我所有GUI代码所在的位置 我已经修改了很多代码。我想我已经留下了我想要做的一切 我不确定下面的代码是否有效,是否经过了编辑,但一般来说,每当用户单击GUI上的ROLL按钮时,它都应该执行rollDice方法,该方法在骰子的一侧循环0.1秒,最后停止并到达最终值

我将再次发布这篇文章,这次尽量做到更精确和简洁。我已经安装了WindowBuilder,并一直在使用它生成GUI代码

所以我有我的图形用户界面设置和一切进行。WindowBuilder会自动生成一个名为initialize的方法,这就是我所有GUI代码所在的位置

我已经修改了很多代码。我想我已经留下了我想要做的一切

我不确定下面的代码是否有效,是否经过了编辑,但一般来说,每当用户单击GUI上的ROLL按钮时,它都应该执行rollDice方法,该方法在骰子的一侧循环0.1秒,最后停止并到达最终值

我一直在疯狂地尝试实现一个方法来实现这一点,但是我在initialize类之外对GUI所做的任何事情都不起作用——但在我的代码中没有返回任何错误。任何帮助都将不胜感激

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;
import java.awt.Image;
import java.awt.Label;

import javax.swing.JScrollPane;
import javax.swing.JPanel;
import javax.swing.JOptionPane;
import java.util.*;
import javax.swing.JComboBox;
import java.awt.Color;
import javax.swing.SwingConstants;

public class PigDice {
public JFrame frame;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                PigDice window = new PigDice();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}//Main Method

static void diceTumble(){

}

static void pausee(int x){
    try {
        Thread.sleep(x);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}//endsPause

/**
 * Create the application.
 */
public PigDice() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {

    frame = new JFrame();
    frame.setBounds(100, 100, 1038, 892);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JPanel panel = new JPanel();
    panel.setBounds(0, 0, 1016, 830);
    frame.getContentPane().add(panel);
    panel.setLayout(null);

    JButton btnRoll = new JButton("Roll");
    btnRoll.setFont(new Font("Tahoma", Font.BOLD, 24));
    btnRoll.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            tumbleDice();
        }
    });
    btnRoll.setBounds(292, 639, 135, 52);
    panel.add(btnRoll);

}

static void tumbleDice(){
    for(int i = 0; i < 25; i++){
        sleep(100);

        JPanel panel_1 = new JPanel();//panel for dice1
        panel_1.setBounds(277, 393, 150, 150);
        panel.add(panel_1);
        panel_1.setLayout(null);

        JPanel panel_2 = new JPanel();//panel for dice2
        panel_2.setBounds(564, 393, 150, 150);
        panel.add(panel_2);
        panel_2.setLayout(null);

        JLabel dice1 = new JLabel("dice1");
        dice1.setHorizontalAlignment(SwingConstants.CENTER);
        dice1.setBounds(0, 0, 150, 150);
        Image die1 = new ImageIcon(this.getClass().getResource("" + tumble())).getImage();
        dice1.setIcon(new ImageIcon(die1));
        panel_1.add(dice1);

        JLabel dice2 = new JLabel("dice2");
        dice2.setHorizontalAlignment(SwingConstants.CENTER);
        dice2.setBounds(0, 0, 150, 150);
        Image die2 = new ImageIcon(this.getClass().getResource("" + tumble())).getImage();
        dice2.setIcon(new ImageIcon(die2));
        panel_2.add(dice2); 
    }//for loop
}//tumbleDice method

String tumble(){
    int random = (int) (Math.random() * 6) + 1;
    if(random == 1)
        return "/side1.png";
    if(random == 2)
        return "/side2.png";
    if(random == 3)
        return "/side3.png";
    if(random == 4)
        return "/side4.png";
    if(random == 5)
        return "/side5.png";
    return "/side6.png";
}
}//end PigDice 
不要继续创建新组件。如果要更改图像,则只需使用JLabel的setIcon方法

不要使用空布局。Swing设计用于布局管理器

不要使用线程。睡眠。这会导致事件调度线程休眠,这意味着GUI无法重新绘制自身。而是使用一个摆动计时器

本教程提供了所有这些建议的示例,因此请阅读教程了解基本知识


另外,不要持续读取图像文件。如果要循环25次,这不是很有效。相反,图像应该加载到类的构造函数中。然后可能将它们存储在ArrayList中,然后返回要在标签中显示的图标的随机索引。

我将查看Swing Timer和setIcon更多信息,听起来是非常好的建议!我应该怎么做,而不是把空里面呢?我已经建议你使用布局管理器。本教程包含工作示例。我建议一个简单的FlowLayout是开始显示两个骰子的好地方。