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

JLabel and JPanel - Dynamically using a GUI to display different images

我将再次 post 并尝试更加精确和简洁。我已经安装了 WindowBuilder 并一直使用它来生成我的 GUI 代码。

所以我设置了 GUI,一切正常。 WindowBuilder 自动生成一个名为 initialize() 的方法,这就是我所有 GUI 代码所在的地方。

我修改了很多代码。我想我已经留下了确定我正在尝试做的事情所需的一切。

我不确定下面的代码是否有效,正在被编辑和所有,但一般来说,每当用户单击 GUI 上的 "ROLL" 按钮时,它应该执行 rollDice () 方法,每个骰子的一面循环 0.1 秒,最后停止并落在最终值上。

我一直在疯狂地尝试实现一种方法来执行此操作,但是我在 initialize() class 之外所做的与 GUI 相关的所有事情根本不起作用 - 但 returns 我的代码中没有错误。如有任何帮助,我们将不胜感激!

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 
  1. 不要继续创建新组件。如果要更改图像,只需使用 JLabel 的 setIcon() 方法即可。

  2. 不要使用空布局。 Swing 旨在与布局管理器一起使用。

  3. 不要使用 Thread.sleep()。这会导致事件调度线程休眠,这意味着 GUI 无法自行重绘。而是使用 Swing Timer.

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

还有,不要一直看图片文件。如果要循环 25 次,这不是很有效。相反,图像应该加载到 class 的构造函数中。然后可能将它们存储在一个 ArrayList 中,只是 return 您想要在标签中显示的图标的随机索引。