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

时间:2015-11-02 05:40:31

标签: java user-interface windowbuilder dice

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

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

我编写了很多代码。我想我已经留下了所需的一切,以确定我想做什么。

我不确定下面的代码是否有效,正在编辑以及所有代码,但一般来说,只要用户点击" ROLL" GUI上的按钮,它应该执行 rollDice()方法,该方法循环一个骰子的一侧,每个.1秒,最后停止并着陆最终值。

我一直在努力实现一种方法来实现这一点,但是我对 initialize()类之外的GUI所做的任何事情都不起作用 - 但是返回no我的代码中的错误。任何帮助将不胜感激!

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 个答案:

答案 0 :(得分:0)

  1. 不要继续创建新组件。如果要更改图像,只需使用JLabel的setIcon()方法。

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

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

  4. Swing Tutorial包含所有这些建议的示例,因此请阅读基础知识教程。

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