【问题标题】:JLabel and JPanel - Dynamically using a GUI to display different imagesJLabel 和 JPanel - 动态使用 GUI 显示不同的图像
【发布时间】: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


    【解决方案1】:
    1. 不要继续创建新组件。如果你想改变图像,那么只需使用 JLabel 的setIcon() 方法即可。

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

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

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

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

    【讨论】:

    • 我将研究 Swing Timer 并更多地研究 setIcon,听起来是非常好的建议!那我应该怎么做而不是把 null 放在里面呢?
    • 我已经建议你使用“布局管理器”。本教程包含工作示例。我建议一个简单的 FlowLayout 是开始显示两个骰子的好地方。
    猜你喜欢
    • 2021-06-07
    • 2019-07-08
    • 2022-06-19
    • 2012-11-11
    • 2014-01-20
    • 2013-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多