我想在Java中按下按钮时显示图像

时间:2019-12-04 18:12:57

标签: java swing jframe

此代码用于黑杰克游戏。它显示了三张随机纸牌和一张纸牌的背面。我希望能够按下点击按钮,它会显示另一张随机卡片。我知道使用.setBounds并不是显示图像的最佳方法,请原谅我。

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.awt.Component;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.*;
import java.awt.*;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Frame;

public class MyGUIProgram implements  ActionListener

{
static int[] car;
static int hi=0;
static int count;
private enum Actions {
    HELLO,
    GOODBYE
}

我目前仅在尝试使它工作时才使用第一个。当我切换它们时,会显示消息,但是hi的值似乎在整个程序中都没有变化。

   public void actionPerformed(ActionEvent evt) {
    if (evt.getActionCommand() == Actions.HELLO.name()) {
        hi=1;
    } else if (evt.getActionCommand() == Actions.GOODBYE.name()) {
        JOptionPane.showMessageDialog(null, "Goodbye");
    }
    }
     public static void main(String args[])  throws IOException {
      MyGUIProgram instance = new MyGUIProgram();
    JFrame f = new JFrame("Black Jack");
    JOptionPane.showMessageDialog(f,"Welcome to Black Jack, please press 
    'ok' to begin");
    f.setSize(1000,1000);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       

    String[] files = new String[14];
    BufferedImage[] img = new BufferedImage[14];
    ImageIcon[] imageIcon = new ImageIcon[14];
    ImageIcon[] imageIcon2 = new ImageIcon[14];
    Image[] image = new Image[14];
    Image[] newimg = new Image[14];
    JLabel[] label = new JLabel[14];
    int[] cardVal = new int[14];
    car = new int[14];
    files[0] = "ace.png";
    files[1] = "two.png";
    files[2] = "three.png";
    files[3] = "four.png";
    files[4] = "five.png";
    files[5] = "six.png";
    files[6] = "seven.png";
    files[7] = "eight.png";
    files[8] = "nine.png";
    files[9] = "ten.png";
    files[10] = "jack.jpg";
    files[11] = "queen.png";
    files[12] = "king.jpg";
    files[13] = "blank.png";

    BufferedImage img13 = ImageIO.read(new File("ace.png"));
    ImageIcon imageIcon13 = new ImageIcon(img13);
    int width=imageIcon13.getIconWidth()/2;
    int height=imageIcon13.getIconHeight()/2;

    for(int i=0;i<14;i++){
        image[i] = ImageIO.read(new File(files[i]));
        imageIcon[i] = new ImageIcon(image[i]); // load the image to a 
     imageIcon

        image[i]= imageIcon[i].getImage(); // transform it

        newimg[i] = image[i].getScaledInstance(width, height,  
     java.awt.Image.SCALE_SMOOTH); // 
        scale it the smooth way 

        imageIcon2[i]= new ImageIcon(newimg[i]);  

        label[i] = new JLabel();

        car[i]=(int)(13*Math.random())+0;

        if(i<10)
            cardVal[i]=i+1;
        else
            cardVal[i]=10;
    }

    f.setLayout(null);

    JLabel label1 = new JLabel(imageIcon2[car[1]]);
    label1.setBounds(380,700,width,height);
    f.add(label1);

    JLabel label2 = new JLabel(imageIcon2[car[2]]);
    label2.setBounds(380,100,width,height);
    f.add(label2);

    JLabel label3 = new JLabel(imageIcon2[car[3]]);
    label3.setBounds(500,700,width,height);
    f.add(label3);

    JLabel label4 = new JLabel(imageIcon2[13]);
    label4.setBounds(500,100,width,height);
    f.add(label4);

    f.setVisible(true);

    JButton hello = new JButton("Hit");
    hello.setActionCommand(Actions.HELLO.name());
    hello.addActionListener(instance);
    hello.setBounds(750,500,100,70);
    f.add(hello);

    JButton goodbye = new JButton("Goodbye");
    goodbye.setActionCommand(Actions.GOODBYE.name());
    goodbye.addActionListener(instance);`

这是不起作用的部分。当我带走if语句时,它起作用。嗨,只是没有改变价值

    if(hi==1){
            count=5;
            label[count] = new JLabel(imageIcon2[car[count]]);
            label[count].setBounds(200,700,width,height);
            f.add(label[count]);
        }
  }}

0 个答案:

没有答案
相关问题