【发布时间】:2016-02-01 23:15:22
【问题描述】:
我将再次发布此内容,并尝试更准确和简洁。我已经安装了 WindowBuilder 并一直在使用它来生成我的 GUI 代码。
所以我已经设置好了我的 GUI,一切顺利。 WindowBuilder 会自动创建一个名为 initialize() 的方法,这就是我所有的 GUI 代码所在的位置。
我已经修改了很多代码。我想我已经留下了确定我正在尝试做什么所需的一切。
我不确定下面的代码是否有效,被编辑和所有,但一般来说,每当用户点击 GUI 上的“滚动”按钮时,它应该执行 rollDice()方法,每个骰子的一边循环 0.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
【问题讨论】:
标签: java user-interface windowbuilder dice