2015-11-02 13 views
0

我将再次发布这个,并尝试更精确和简洁的这一次。我已经安装了WindowBuilder,并一直使用它来生成我的GUI代码。JLabel和JPanel - 动态地使用GUI来显示不同的图像

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

我编辑了很多我的代码。我想我已经把所有需要的东西都留下来,以确定我正在尝试做什么。

我不知道如果跌破作品的代码,被删节和所有,但在一般情况下,当用户点击GUI上的“ROLL”按钮,就应该执行rollDice()方法,其中周期一个骰子的一边,每个0.1秒,最后在最终值停止和着陆。

我一直在试图像疯了似的落实要做到这一点的方法,但是任何和一切我做出关于对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 

回答

0
  1. 不要将创造新的组件。如果你想改变图像,那么只需使用JLabel的setIcon()方法。

  2. 请勿使用空布局。 Swing旨在与布局经理一起使用。

  3. 请勿使用Thread.sleep()。这会导致事件分派线程进入睡眠状态,这意味着GUI无法重新绘制自己。请使用Swing Timer

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

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

+0

我会看看Swing Timer并且更多地关注setIcon,听起来像非常好的建议! 我应该怎么做,而不是在那里把null? –

+0

我已经建议你使用“布局管理器”。本教程包含工作示例。我会建议一个简单的FlowLayout是一个开始显示两个骰子的好地方。 – camickr