如何使用java swing将图像随机显示到JButton上? [英] how to display an image randomly on to a JButton using java swing?

查看:31
本文介绍了如何使用java swing将图像随机显示到JButton上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,所以基本上我已经创建了 5 个 jbuttons,当按下投掷按钮时,每个 jbuttons 都应该随机显示一个图像..下面的代码显示了我相信我的 6 个带有随机图像生成器代码的模具类...

import javax.swing.*;导入 java.util.*;公共类骰子{Die die1 = new Die();模具 die2 = new Die();模具 die3 = new Die();死 die4 = new Die();死 die5 = new Die();死 die6 = new Die();公共无效 someFunc() {die1.setImage("1.png");die2.setImage("2.png");die3.setImage("3.png");die4.setImage("4.png");die5.setImage("5.png");die6.setImage("6.png");}公共无效滚动(){ArrayList list = new ArrayList();随机 posGen = new Random(5);for (int i = 0; i <5; i++)开关 (1 + posGen.nextInt()) {情况1:list.add(die1);休息;案例2:list.add(die2);休息;案例3:list.add(die3);休息;案例4:list.add(die4);休息;案例5:list.add(die5);休息;案例6:list.add(die6);休息;}}}

让我再解释一下,上面的类创建了 6 个 die 对象的实例.然后我将每个骰子分配给一个图像.然后 roll 方法应该将随机图像添加到数组列表中..

现在以下是我的 GUI 代码:

import javax.swing.*;导入 java.awt.*;导入 java.awt.event.*;导入静态 javax.swing.JFrame.EXIT_ON_CLOSE;公开课 Coursework2 {骰子 mydice = new Dice();类 MyActionListener 实现 ActionListener {JFrame 框架;JButton die1;MyActionListener(JFrame f) {框架 = f;}public void actionPerformed(ActionEvent e) {//die1.setIcon(null);}}公共无效gui(){JFrame frame = new JFrame("SimpleSwingExample");JPanel 按钮 = new JPanel();button.setLayout(null);JButton button = new JButton("扔他妈的死");frame.setContentPane(按钮);frame.getContentPane().add(button);frame.setDefaultCloseOperation(EXIT_ON_CLOSE);JButton die1 = new JButton("die 1");JButton die2 = new JButton("die 2");JButton die3 = new JButton("die 3");JButton die4 = new JButton("die 4");JButton die5 = new JButton("die 5");button.addActionListener(new MyActionListener(frame));frame.getContentPane().add(die1);frame.getContentPane().add(die2);frame.getContentPane().add(die3);frame.getContentPane().add(die4);frame.getContentPane().add(die5);button.setBackground(Color.blue);die1.setBounds(200, 50, 200, 200);die2.setBounds(400, 50, 200, 200);die3.setBounds(600, 50, 200, 200);die4.setBounds(800, 50, 200, 200);die5.setBounds(1000, 50, 200, 200);button.setBounds(10, 300, 200, 100);frame.setSize(1500, 800);frame.setVisible(true);}}

好的,现在我想要实现的是,当单击标记为throw"的 jbutton 时,我创建的每个标记为 die 1-6 的 jbutton 都应该随机显示一个图像.明白了吗?

我该怎么做,我需要用代码解释...

谢谢.

解决方案

您不应该使用空布局.相反,您可以使用

需要考虑的事项:

  • 此程序不验证随机数是否相同
  • 图片必须全部为png格式
  • 启动时的程序没有图像,但您可以自行更改.

产生上述输出的代码是:

import javax.swing.*;导入 java.awt.*;导入 java.awt.event.*;导入 javax.imageio.ImageIO;导入 java.io.*;导入 java.util.Random;公开课 CourseWork2 {JFrame 框架;JButton b1, b2, b3, b4, b5, randomButton;JPanel buttonPane、randomPane;public void generateRandomImages() {尝试 {随机 rand = new Random();int randomNum = 0;int numbers[] = new int[5];for (int i = 0; i <5; i++) {randomNum = rand.nextInt((5 - 1) + 1) + 1;数字[i] = randomNum;}图像 img = ImageIO.read(getClass().getResource(numbers[0] + ".png"));b1.setIcon(new ImageIcon(img));img = ImageIO.read(getClass().getResource(numbers[1] + ".png"));b2.setIcon(new ImageIcon(img));img = ImageIO.read(getClass().getResource(numbers[2] + ".png"));b3.setIcon(new ImageIcon(img));img = ImageIO.read(getClass().getResource(numbers[3] + ".png"));b4.setIcon(new ImageIcon(img));img = ImageIO.read(getClass().getResource(numbers[4] + ".png"));b5.setIcon(new ImageIcon(img));框架.pack();} catch (IOException ex) {ex.printStackTrace();}}CourseWork2() {frame = new JFrame("随机图片");b1 = new JButton();b2 = new JButton();b3 = new JButton();b4 = 新 JButton();b5 = 新 JButton();randomButton = new JButton("点击随机图片");buttonPane = new JPanel();randomPane = new JPanel();buttonPane.setLayout(new FlowLayout());randomPane.setLayout(new FlowLayout());buttonPane.add(b1);buttonPane.add(b2);buttonPane.add(b3);buttonPane.add(b4);buttonPane.add(b5);randomPane.add(randomButton);randomButton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {generateRandomImages();}});frame.add(buttonPane, BorderLayout.PAGE_START);frame.add(randomPane, BorderLayout.CENTER);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);框架.pack();frame.setVisible(true);}公共静态无效主(字符串参数[]){新课程工作2();}}

alright, so basically i have created 5 jbuttons, each of these should display an image randomly when the throw button is pressed.. the following code below shows what i beleive are my 6 die classes with the random image generator code...

import javax.swing.*;
import java.util.*;
public class Dice {

    Die die1 = new Die();
    Die die2 = new Die();
    Die die3 = new Die();
    Die die4 = new Die();
    Die die5 = new Die();
    Die die6 = new Die();

    public void someFunc() {
        die1.setImage("1.png");
        die2.setImage("2.png");
        die3.setImage("3.png");
        die4.setImage("4.png");
        die5.setImage("5.png");
        die6.setImage("6.png");
    }

    public void roll() {
        ArrayList list = new ArrayList();
        Random posGen = new Random(5);

        for (int i = 0; i < 5; i++)
        switch (1 + posGen.nextInt()) {
            case 1:
                list.add(die1);
                break;
            case 2:
                list.add(die2);
                break;
            case 3:
                list.add(die3);
                break;
            case 4:
                list.add(die4);
                break;
            case 5:
                list.add(die5);
                break;
            case 6:
                list.add(die6);
                break;
        }

    }

}

so let me explain again, the above class created 6 instances of die objects. i then assigned each die to an image. then the roll method should add a random image into the array list..

now the following is my GUI code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import static javax.swing.JFrame.EXIT_ON_CLOSE;

public class Coursework2 {

    Dice mydice = new Dice();

    class MyActionListener implements ActionListener {

        JFrame frame;
        JButton die1;

        MyActionListener(JFrame f) {
            frame = f;

        }
        public void actionPerformed(ActionEvent e) {


            //die1.setIcon(null);

        }
    }
    public void gui() {

        JFrame frame = new JFrame("SimpleSwingExample");
        JPanel buttons = new JPanel();
        buttons.setLayout(null);
        JButton button = new JButton("throw the fucking die");
        frame.setContentPane(buttons);
        frame.getContentPane().add(button);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);

        JButton die1 = new JButton("die 1");
        JButton die2 = new JButton("die 2");
        JButton die3 = new JButton("die 3");
        JButton die4 = new JButton("die 4");
        JButton die5 = new JButton("die 5");

        button.addActionListener(new MyActionListener(frame));

        frame.getContentPane().add(die1);
        frame.getContentPane().add(die2);
        frame.getContentPane().add(die3);
        frame.getContentPane().add(die4);
        frame.getContentPane().add(die5);

        buttons.setBackground(Color.blue);

        die1.setBounds(200, 50, 200, 200);
        die2.setBounds(400, 50, 200, 200);
        die3.setBounds(600, 50, 200, 200);
        die4.setBounds(800, 50, 200, 200);
        die5.setBounds(1000, 50, 200, 200);

        button.setBounds(10, 300, 200, 100);

        frame.setSize(1500, 800);
        frame.setVisible(true);

    }


}

alright, now what i want to achieve is, when the jbutton labeled "throw" is clicked, each of the jbuttons labeled die 1-6 that i have created should then display an image at random. get it?

how can i do this, i need explanation with code...

thanks .

解决方案

You shouldn't use a null layout. Instead you can get a nice view with Layout Managers. You can read more about why this isn't encouraged here and here

Things to consider:

  • This program doesn't validate if random numbers are the same
  • The images must be all png format
  • The program at start has no images, but is up to you to change it.

The code that produces the above output is:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.imageio.ImageIO;
import java.io.*;
import java.util.Random;

public class CourseWork2 {
    JFrame frame;
    JButton b1, b2, b3, b4, b5, randomButton;
    JPanel buttonPane, randomPane;

    public void generateRandomImages() {
        try {
            Random rand = new Random();
            int randomNum = 0;
            int numbers[] = new int[5];
            for (int i = 0; i < 5; i++) {
                randomNum = rand.nextInt((5 - 1) + 1) + 1;
                numbers[i] = randomNum;
            }

            Image img = ImageIO.read(getClass().getResource(numbers[0] + ".png"));
            b1.setIcon(new ImageIcon(img));

            img = ImageIO.read(getClass().getResource(numbers[1] + ".png"));
            b2.setIcon(new ImageIcon(img));

            img = ImageIO.read(getClass().getResource(numbers[2] + ".png"));
            b3.setIcon(new ImageIcon(img));

            img = ImageIO.read(getClass().getResource(numbers[3] + ".png"));
            b4.setIcon(new ImageIcon(img));

            img = ImageIO.read(getClass().getResource(numbers[4] + ".png"));
            b5.setIcon(new ImageIcon(img));

            frame.pack();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    CourseWork2() {
        frame = new JFrame("Random Images");
        b1 = new JButton();
        b2 = new JButton();
        b3 = new JButton();
        b4 = new JButton();
        b5 = new JButton();
        randomButton = new JButton("Clic for random images");

        buttonPane = new JPanel();
        randomPane = new JPanel();

        buttonPane.setLayout(new FlowLayout());
        randomPane.setLayout(new FlowLayout());

        buttonPane.add(b1);
        buttonPane.add(b2);
        buttonPane.add(b3);
        buttonPane.add(b4);
        buttonPane.add(b5);
        randomPane.add(randomButton);

        randomButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                generateRandomImages();
            }
        });

        frame.add(buttonPane, BorderLayout.PAGE_START);
        frame.add(randomPane, BorderLayout.CENTER);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String args[]) {
        new CourseWork2();
    }
}

这篇关于如何使用java swing将图像随机显示到JButton上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆