Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我想在Java中按下按钮时显示一个图像_Java_Swing_Jframe - Fatal编程技术网

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

我想在Java中按下按钮时显示一个图像,java,swing,jframe,Java,Swing,Jframe,这是一个黑杰克游戏的代码。它显示了三张随机牌和一张扑克牌的背面。我想能够按下点击按钮,它会显示另一张随机卡。我知道使用。挫折不是最好的方式来显示图像,请原谅我 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 j

这是一个黑杰克游戏的代码。它显示了三张随机牌和一张扑克牌的背面。我想能够按下点击按钮,它会显示另一张随机卡。我知道使用。挫折不是最好的方式来显示图像,请原谅我

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);`

可以使用普通字体。彩虹般的字体样式让我很难读懂这个问题。这是我第一次使用这个工具,你如何让它更容易读懂?我用BlueJ编程了这个。这就是为什么它的颜色不同,我不认为它会太重要,我只是没有改变价值-为什么你认为它应该?代码中没有任何地方可以将其更改为除1以外的任何内容。我认为当我单击“点击”按钮时,应该将其更改为1。那就是它变为1的时候
    if(hi==1){
            count=5;
            label[count] = new JLabel(imageIcon2[car[count]]);
            label[count].setBounds(200,700,width,height);
            f.add(label[count]);
        }
  }}