Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.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 在GUI应用程序中单击时,第二个图像未显示,没有人知道原因是什么? 公共类内存扩展JFrame{ 私有静态最终长serialVersionUID=-2065145900871059367L; 私有JPanel内容窗格; 公共静态void main(字符串[]args){//从WindowBuilder自动生成 invokeLater(新的Runnable(){ 公开募捐{ 试一试{ 内存帧=新内存(); frame.setVisible(true); }捕获(例外e){ e、 printStackTrace(); } } }); } JButton[]卡=新JButton[12]; int[]flippedNum=新int[12]; 图标[]catImg=新图标[12]; { catImg[0]=新的图像图标(getClass().getResource(“2faceszd.jpg”); catImg[1]=新的图像图标(getClass().getResource(“blkctszd.jpg”); catImg[2]=新的图像图标(getClass().getResource(“ct&mszd.png”); catImg[3]=新的图像图标(getClass().getResource(“floofctszd.jpg”); catImg[4]=新的图像图标(getClass().getResource(“orngctszd.jpg”); catImg[5]=新的图像图标(getClass().getResource(“siamctszd.jpg”); catImg[6]=新的图像图标(getClass().getResource(“2faceszd.jpg”); catImg[7]=新的图像图标(getClass().getResource(“blkctszd.jpg”); catImg[8]=新的图像图标(getClass().getResource(“ct&mszd.png”); catImg[9]=新的图像图标(getClass().getResource(“floofctszd.jpg”); catImg[10]=新的图像图标(getClass().getResource(“orngctszd.jpg”); catImg[11]=新的图像图标(getClass().getResource(“siamctszd.jpg”); }//添加了两次6个图像 Icon photo=newimageicon(getClass().getResource(“BG.png”); 静态列表cats=newarraylist(){ cats.add(1);cats.add(1); cats.add(2);cats.add(2); cats.add(3);cats.add(3); cats.add(4);cats.add(4); cats.add(5);cats.add(5); cats.add(6);cats.add(6); } 静态随机基蒂=新随机(); 公共静态int getCardAssignment(){ int cat=kitty.nextInt(cats.size()); int ucat=cats.get(cat); 猫。移除(猫); 返回ucat; }返回要分配给图标的随机数 //构建框架 公共内存(){ setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 立根(100100650500); contentPane=newjpanel(); setboorder(新的EmptyBorder(5,5,5,5)); setLayout(新的BorderLayout(0,0)); setContentPane(contentPane); JPanel title=新的JPanel(); 标题.设置名称(“”); title.setFont(新字体(“Tahoma”,Font.PLAIN,16)); 标题.挫折背景(颜色.粉红色); 添加(标题,BorderLayout.NORTH); JLabel gameName=新的JLabel(“记忆游戏”); 标题。添加(游戏名称); setFont(新字体(“Arial黑色”,Font.PLAIN,16)); 游戏名称。挫折背景(颜色。粉红色); gameName.setEnabled(false); JPanel cardisplay=新的JPanel(); 卡片显示。背景(颜色。粉红色); 设置字体(新字体(“Arial黑色”,Font.PLAIN,16)); cardDisplay.Setboorder(新的空订单(10,10,10,10)); 添加(cardDisplay、BorderLayout.CENTER); 设置布局(新的网格布局(3,4,10,10)); ButtonClickEventHandler单击它=新建按钮Clickkeventhandler(); 对于(int i=0;i_Java_Swing_Event Handling_Jbutton - Fatal编程技术网

Java 在GUI应用程序中单击时,第二个图像未显示,没有人知道原因是什么? 公共类内存扩展JFrame{ 私有静态最终长serialVersionUID=-2065145900871059367L; 私有JPanel内容窗格; 公共静态void main(字符串[]args){//从WindowBuilder自动生成 invokeLater(新的Runnable(){ 公开募捐{ 试一试{ 内存帧=新内存(); frame.setVisible(true); }捕获(例外e){ e、 printStackTrace(); } } }); } JButton[]卡=新JButton[12]; int[]flippedNum=新int[12]; 图标[]catImg=新图标[12]; { catImg[0]=新的图像图标(getClass().getResource(“2faceszd.jpg”); catImg[1]=新的图像图标(getClass().getResource(“blkctszd.jpg”); catImg[2]=新的图像图标(getClass().getResource(“ct&mszd.png”); catImg[3]=新的图像图标(getClass().getResource(“floofctszd.jpg”); catImg[4]=新的图像图标(getClass().getResource(“orngctszd.jpg”); catImg[5]=新的图像图标(getClass().getResource(“siamctszd.jpg”); catImg[6]=新的图像图标(getClass().getResource(“2faceszd.jpg”); catImg[7]=新的图像图标(getClass().getResource(“blkctszd.jpg”); catImg[8]=新的图像图标(getClass().getResource(“ct&mszd.png”); catImg[9]=新的图像图标(getClass().getResource(“floofctszd.jpg”); catImg[10]=新的图像图标(getClass().getResource(“orngctszd.jpg”); catImg[11]=新的图像图标(getClass().getResource(“siamctszd.jpg”); }//添加了两次6个图像 Icon photo=newimageicon(getClass().getResource(“BG.png”); 静态列表cats=newarraylist(){ cats.add(1);cats.add(1); cats.add(2);cats.add(2); cats.add(3);cats.add(3); cats.add(4);cats.add(4); cats.add(5);cats.add(5); cats.add(6);cats.add(6); } 静态随机基蒂=新随机(); 公共静态int getCardAssignment(){ int cat=kitty.nextInt(cats.size()); int ucat=cats.get(cat); 猫。移除(猫); 返回ucat; }返回要分配给图标的随机数 //构建框架 公共内存(){ setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 立根(100100650500); contentPane=newjpanel(); setboorder(新的EmptyBorder(5,5,5,5)); setLayout(新的BorderLayout(0,0)); setContentPane(contentPane); JPanel title=新的JPanel(); 标题.设置名称(“”); title.setFont(新字体(“Tahoma”,Font.PLAIN,16)); 标题.挫折背景(颜色.粉红色); 添加(标题,BorderLayout.NORTH); JLabel gameName=新的JLabel(“记忆游戏”); 标题。添加(游戏名称); setFont(新字体(“Arial黑色”,Font.PLAIN,16)); 游戏名称。挫折背景(颜色。粉红色); gameName.setEnabled(false); JPanel cardisplay=新的JPanel(); 卡片显示。背景(颜色。粉红色); 设置字体(新字体(“Arial黑色”,Font.PLAIN,16)); cardDisplay.Setboorder(新的空订单(10,10,10,10)); 添加(cardDisplay、BorderLayout.CENTER); 设置布局(新的网格布局(3,4,10,10)); ButtonClickEventHandler单击它=新建按钮Clickkeventhandler(); 对于(int i=0;i

Java 在GUI应用程序中单击时,第二个图像未显示,没有人知道原因是什么? 公共类内存扩展JFrame{ 私有静态最终长serialVersionUID=-2065145900871059367L; 私有JPanel内容窗格; 公共静态void main(字符串[]args){//从WindowBuilder自动生成 invokeLater(新的Runnable(){ 公开募捐{ 试一试{ 内存帧=新内存(); frame.setVisible(true); }捕获(例外e){ e、 printStackTrace(); } } }); } JButton[]卡=新JButton[12]; int[]flippedNum=新int[12]; 图标[]catImg=新图标[12]; { catImg[0]=新的图像图标(getClass().getResource(“2faceszd.jpg”); catImg[1]=新的图像图标(getClass().getResource(“blkctszd.jpg”); catImg[2]=新的图像图标(getClass().getResource(“ct&mszd.png”); catImg[3]=新的图像图标(getClass().getResource(“floofctszd.jpg”); catImg[4]=新的图像图标(getClass().getResource(“orngctszd.jpg”); catImg[5]=新的图像图标(getClass().getResource(“siamctszd.jpg”); catImg[6]=新的图像图标(getClass().getResource(“2faceszd.jpg”); catImg[7]=新的图像图标(getClass().getResource(“blkctszd.jpg”); catImg[8]=新的图像图标(getClass().getResource(“ct&mszd.png”); catImg[9]=新的图像图标(getClass().getResource(“floofctszd.jpg”); catImg[10]=新的图像图标(getClass().getResource(“orngctszd.jpg”); catImg[11]=新的图像图标(getClass().getResource(“siamctszd.jpg”); }//添加了两次6个图像 Icon photo=newimageicon(getClass().getResource(“BG.png”); 静态列表cats=newarraylist(){ cats.add(1);cats.add(1); cats.add(2);cats.add(2); cats.add(3);cats.add(3); cats.add(4);cats.add(4); cats.add(5);cats.add(5); cats.add(6);cats.add(6); } 静态随机基蒂=新随机(); 公共静态int getCardAssignment(){ int cat=kitty.nextInt(cats.size()); int ucat=cats.get(cat); 猫。移除(猫); 返回ucat; }返回要分配给图标的随机数 //构建框架 公共内存(){ setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 立根(100100650500); contentPane=newjpanel(); setboorder(新的EmptyBorder(5,5,5,5)); setLayout(新的BorderLayout(0,0)); setContentPane(contentPane); JPanel title=新的JPanel(); 标题.设置名称(“”); title.setFont(新字体(“Tahoma”,Font.PLAIN,16)); 标题.挫折背景(颜色.粉红色); 添加(标题,BorderLayout.NORTH); JLabel gameName=新的JLabel(“记忆游戏”); 标题。添加(游戏名称); setFont(新字体(“Arial黑色”,Font.PLAIN,16)); 游戏名称。挫折背景(颜色。粉红色); gameName.setEnabled(false); JPanel cardisplay=新的JPanel(); 卡片显示。背景(颜色。粉红色); 设置字体(新字体(“Arial黑色”,Font.PLAIN,16)); cardDisplay.Setboorder(新的空订单(10,10,10,10)); 添加(cardDisplay、BorderLayout.CENTER); 设置布局(新的网格布局(3,4,10,10)); ButtonClickEventHandler单击它=新建按钮Clickkeventhandler(); 对于(int i=0;i,java,swing,event-handling,jbutton,Java,Swing,Event Handling,Jbutton,} } 代码应该允许用户单击其中一个 public class Memory extends JFrame { private static final long serialVersionUID = -2065145900871059367L; private JPanel contentPane; public static void main(String[] args) { //auto generated from WindowBuilder EventQueue.invoke

} }

代码应该允许用户单击其中一个
public class Memory extends JFrame {
private static final long serialVersionUID = -2065145900871059367L;
private JPanel contentPane;
public static void main(String[] args) { //auto generated from WindowBuilder
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Memory frame = new Memory();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}
JButton[] cards = new JButton[12];
int [] flippedNum = new int[12];
Icon [] catImg = new Icon[12];
{
catImg[0]= new ImageIcon(getClass().getResource("2faceszd.jpg"));
catImg[1] = new ImageIcon(getClass().getResource("blkctszd.jpg"));
catImg[2]= new ImageIcon(getClass().getResource("ct&mszd.png"));
catImg[3]= new ImageIcon(getClass().getResource("floofctszd.jpg"));
catImg[4]= new ImageIcon(getClass().getResource("orngctszd.jpg"));
catImg[5]= new ImageIcon(getClass().getResource("siamctszd.jpg"));
catImg[6]= new ImageIcon(getClass().getResource("2faceszd.jpg"));
catImg[7] = new ImageIcon(getClass().getResource("blkctszd.jpg"));
catImg[8]= new ImageIcon(getClass().getResource("ct&mszd.png"));
catImg[9]= new ImageIcon(getClass().getResource("floofctszd.jpg"));
catImg[10]= new ImageIcon(getClass().getResource("orngctszd.jpg"));
catImg[11]= new ImageIcon(getClass().getResource("siamctszd.jpg"));
} //6 images added twice
Icon photo = new ImageIcon(getClass().getResource("BG.png"));

static List<Integer> cats = new ArrayList<Integer>();{
    cats.add(1); cats.add(1);
    cats.add(2); cats.add(2);
    cats.add(3); cats.add(3);
    cats.add(4); cats.add(4);
    cats.add(5); cats.add(5);
    cats.add(6); cats.add(6);
}

static Random kitty = new Random();
public static int getCardAssignment(){
    int cat = kitty.nextInt(cats.size());
    int ucat = cats.get(cat);
    cats.remove(cat);
    return ucat;
} returns a random number to be assigned to an icon
 //building the frame
public Memory() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 650, 500);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    contentPane.setLayout(new BorderLayout(0, 0));
    setContentPane(contentPane);

    JPanel title = new JPanel();
    title.setName("");
    title.setFont(new Font("Tahoma", Font.PLAIN, 16));
    title.setBackground(Color.PINK);
    contentPane.add(title, BorderLayout.NORTH);

    JLabel gameName = new JLabel("Memory Game");
    title.add(gameName);
    gameName.setFont(new Font("Arial Black", Font.PLAIN, 16));
    gameName.setBackground(Color.PINK);
    gameName.setEnabled(false);

    JPanel cardDisplay = new JPanel();
    cardDisplay.setBackground(Color.PINK);
    cardDisplay.setFont(new Font("Arial Black", Font.PLAIN, 16));
    cardDisplay.setBorder(new EmptyBorder(10, 10, 10, 10));
    contentPane.add(cardDisplay, BorderLayout.CENTER);
    cardDisplay.setLayout(new GridLayout(3, 4, 10, 10));        

    ButtonClickEventHandler clickIt = new ButtonClickEventHandler();

    for(int i = 0; i < cards.length; i ++){
        cards[i] = new JButton(); // new button
        flippedNum[i] = getCardAssignment(); //card# assigned to cat icon
        cards[i].setIcon(photo); //set background image
        cards[i].addActionListener(clickIt); //when you click it
        cardDisplay.add(cards[i]); //show them all
        System.out.println("background displayed"); 
         //sysprint statements are for testing the output stream only   
        }
    }
    private class ButtonClickEventHandler implements ActionListener{
private int q = 0; //# of clickes, like switch statement w/o userinput
private int l; // = 0; records number location when clicked
@Override
public void actionPerformed(ActionEvent e) {
for(int i = 0; i < 12; i ++){
if (q == 0){ //at zero all enabled cards/buttons are face up
    if(cards[i] == e.getSource()){//first card clicked //start state
        System.out.println("q is 0");
        System.out.println("Selection was number " + i);
        l = i; // record the click for the next state. 
            //sets the icon to a cat image w/num [flipped num]
        cards[i].setEnabled(false); 
    // prevents user from clicking the same card twice for a match
        cards[i].setIcon(catImg[flippedNum[i]])
        cards[i].setDisabledIcon(catImg[flippedNum[i]]); 
  //prevents the icon from being grayed out when clicked
            //setting the icon and THEN the disabled icon shows 
            //non gray'd out image when clicked that cannot be clicked again
        q = 1;
        System.out.println("q is now 1");
    }
}
else if(q == 1) { //if one card is already clicked, second click
    System.out.println("q was not 0, now is 1");        
        if(cards[i] == e.getSource()){
            cards[i].setIcon(catImg[flippedNum[i]]);
        System.out.println("first was " + flippedNum[l] + " Second was " + flippedNum[i]); 
            cards[i].setIcon(catImg[flippedNum[i]]);            
            q = 2;
        }
        if(q == 2){
            System.out.println("Timer started");
            cards[l].doClick(1500); 
      //i tried moving this up to before q=2, no luck
        if(flippedNum[l] == flippedNum[i]){
            System.out.println("They Match!!");
            cards[i].setEnabled(false); 
       // permanenty sets the button as disabled
            cards[l].setEnabled(false);
            q=0;
            System.out.println("end of turn q is zero, find another match");
        }else {
            System.out.println("Dont Match");
                cards[i].setEnabled(true);
                cards[l].setEnabled(true);
                cards[i].setIcon(photo);
                cards[l].setIcon(photo);
                q = 0;
            System.out.println("end of turn, back to start, q = 0");
            }
        }
}
}
}
private class ButtonClickEventHandler implements ActionListener {

    private int q = 0; //# of clickes, like switch statement w/o userinput
    private int l; // = 0; records number location when clicked

    @Override
    public void actionPerformed(ActionEvent e) {
        for (int i = 0; i < 12; i++) {
            if (q == 0) { //at zero all enabled cards/buttons are face up
                if (cards[i] == e.getSource()) {//first card clicked //start state
                    System.out.println("q is 0");
                    System.out.println("Selection was number " + i);
                    l = i; // record the click for the next state. 
                    //sets the icon to a cat image w/num [flipped num]
                    cards[i].setEnabled(false);
                    // prevents user from clicking the same card twice for a match
                    cards[i].setIcon(catImg[flippedNum[i]]);
                    cards[i].setDisabledIcon(catImg[flippedNum[i]]);
                    //prevents the icon from being grayed out when clicked
                    //setting the icon and THEN the disabled icon shows 
                    //non gray'd out image when clicked that cannot be clicked again
                    q = 1;
                    System.out.println("q is now 1");
                    break;
                }
            } else if (q == 1) { //if one card is already clicked, second click
                System.out.println("q was not 0, now is 1");
                if (cards[i] == e.getSource()) {
                    cards[i].setIcon(catImg[flippedNum[i]]);
                    System.out.println("first was " + flippedNum[l] + " Second was " + flippedNum[i]);

                    flipFlop(l, i);
                    q = 0;
                    break;
                }
            }
        }
    }

    protected void flipFlop(int l, int i) {

        Timer timer = new Timer(1500, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("Timer started");
                //cards[l].doClick(1500);
                //i tried moving this up to before q=2, no luck
                if (flippedNum[l] == flippedNum[i]) {
                    System.out.println("They Match!!");
                    cards[i].setEnabled(false);
                    // permanenty sets the button as disabled
                    cards[l].setEnabled(false);
                    System.out.println("end of turn q is zero, find another match");
                } else {
                    System.out.println("Dont Match");
                    cards[i].setEnabled(true);
                    cards[l].setEnabled(true);
                    cards[i].setIcon(photo);
                    cards[l].setIcon(photo);
                    System.out.println("end of turn, back to start, q = 0");
                }
            }
        });
        timer.setRepeats(false);
        timer.start();
    }
}