图像在鼠标框架中没有新鲜点击在Java中

时间:2015-04-30 18:47:56

标签: java swing jpanel mouseclick-event

第一次在三个不同阵列的Jframe上显示三个随机图像。 甚至触发了MouseClicked方法但图像不在Frame中刷新。 我想每次点击Frame时刷新三个随机图像。 请帮忙

   import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;

import javax.swing.*;

public class Cards extends JFrame implements MouseListener {

    public static void main(String[] args) {
    JFrame frame = new Cards();     
        frame.setTitle("Cards");



        frame.setSize(500, 500);

        frame.setLocationRelativeTo(null);

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


        new Cards();

    }

    public Cards() {

        this.getContentPane().addMouseListener(this);

        cards1();
        cards2();
        cards3();

    }

    public void cards1() {

        ImageIcon[] images = new ImageIcon[10];
        for (int i = 1; i < images.length; i++) {

            images[i] = new ImageIcon("Drawables//Images//" + i + ".png");

        }

        int[] threeRandoms = new int[1];
        Random ran = new Random();

        for (int i = 0; i < threeRandoms.length; i++) {

            threeRandoms[i] = ran.nextInt(10);

        }

        setLayout(new GridLayout(1, 4, 5, 5));

        add(new JLabel(images[threeRandoms[0]]));

    }


    public void cards2() {

        ImageIcon[] images = new ImageIcon[10];
        for (int i = 1; i < images.length; i++) {

            images[i] = new ImageIcon("Drawables//Images1//" + i + ".png");

        }

        int[] threeRandoms = new int[1];
        Random ran = new Random();

        for (int i = 0; i < threeRandoms.length; i++) {

            threeRandoms[i] = ran.nextInt(10);

        }

        setLayout(new GridLayout(1, 4, 5, 5));

        add(new JLabel(images[threeRandoms[0]]));

    }

    public void cards3() {
        // this.getContentPane().addMouseListener(this);
        ImageIcon[] images = new ImageIcon[10];
        for (int i = 1; i < images.length; i++) {

            images[i] = new ImageIcon("Drawables//Images2//" + i + ".png");

        }

        int[] threeRandoms = new int[1];
        Random ran = new Random();

        for (int i = 0; i < threeRandoms.length; i++) {

            threeRandoms[i] = ran.nextInt(10);

        }

        // Labels with gridLayout

        setLayout(new GridLayout(1, 4, 5, 5));

        add(new JLabel(images[threeRandoms[0]]));

    }

    public void mouseClicked(MouseEvent e) {
        System.out.println("The frame was clicked.");
        new Cards();
    }

    public void mouseEntered(MouseEvent e) {
        System.out.println("The mouse entered the frame.");
    }

    public void mouseExited(MouseEvent e) {
        System.out.println("The mouse exited the frame.");

    }

    public void mousePressed(MouseEvent e) {
        System.out.println("The left mouse button was pressed.");

    }

    public void mouseReleased(MouseEvent e) {
        System.out.println("The left mouse button was released.");

    }

}

2 个答案:

答案 0 :(得分:1)

抱歉,我对您的代码感到困惑。一方面,cards1()cards2()cards3()方法看起来完全相同,如果是这样,为什么有3种不同的方法呢?为什么不只是一种方法?在这些方法中,您似乎尝试重复添加JLabel。您是否尝试向GUI添加许多JLabel?或者您只是试图显示3个在鼠标操作中随机变化的图像?

我建议稍微改变一下结构:

  • 如果可能,请在班级构造函数中一次性阅读所有必要的图像,将图像放入ImageIcons,然后根据需要将它们添加到ArrayList或多个ArrayLists中。
  • 每次发生鼠标点击时都不要添加新的JLabel。
  • 创建一个JPanel,给它一个GridLayout,并在你的类构造函数中添加三个JLabel,它们是实例字段或数组或ArrayList。
  • 将此JPanel添加到您的JFrame。
  • 将MouseListener添加到每个JLabel
  • 在该MouseListener的mousePressed(MouseEvent e)方法(不是mouseClicked)中随机化您的号码并使用该号码在JLabel源上调用setIcon(...),通过调用{{1}获得你的MouseEvent参数。

例如:

getSource()

具体例子2:

import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.*;

@SuppressWarnings("serial")
public class RandomImages extends JPanel {
   private static final int LABEL_COUNT = 3;
   private Random random = new Random();

   public RandomImages() {
      setLayout(new GridLayout(1, 3));
      for (int i = 0; i < LABEL_COUNT; i++) {
         final List<Icon> iconList = new ArrayList<>();

         // TODO: get images for the ith list 
         // and fill iconList with ImageIcons from the first grouping

         // create JLabel and give it the first Icon from the List above
         final JLabel label = new JLabel(iconList.get(0));
         label.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
               // get random number from random object using iconList.size()
               // get random Icon from list
               // set label's icon via setIcon(...)
            }
         });
         // add to GUI
         add(label);
      }
   }

   private static void createAndShowGui() {
      JFrame frame = new JFrame("RandomImages");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(new RandomImages());
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

答案 1 :(得分:0)

我对您的代码进行了以下更改:

  • 我没有使用三种方法cards1() cards2() cards3(),而是使用了一种cards()方法。
  • 每次点击框架时,都会加载三张随机图片。
  • 我已将每张图片都设置在JLabel内,以便更新它。

以下代码可根据您的需要完美运行。

package example;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;


public class Cards extends JFrame implements MouseListener {

    JPanel subPanel1;
    JLabel label1, label2, label3;

    static ImageIcon[] images ;
    static Random ran ;
    static int[] threeRandoms;


    public Cards() {

        super("Cards");

        subPanel1 = new JPanel();

        // Set up first subpanel        
        subPanel1.setPreferredSize (new Dimension(400, 400));
        //subPanel1.setBackground (Color.cyan);
        label1 = new JLabel ("image 1",SwingConstants.CENTER);
        label2 = new JLabel ("image 2", SwingConstants.LEFT);
        label3 = new JLabel ("image 3",  SwingConstants.CENTER);

        subPanel1.add (label1);
        subPanel1.add (label2);
        subPanel1.add (label3);

        add(subPanel1);

        addMouseListener(this);
        setSize(500, 500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
        System.out.println("Success.....");
    }

    public void cards() {


        for (int i = 0; i < threeRandoms.length; i++)
            threeRandoms[i] = ran.nextInt(3);


            label1.setIcon(images[threeRandoms[0]]);
            label2.setIcon(images[threeRandoms[1]]);
            label3.setIcon(images[threeRandoms[2]]);


    }

    @Override
    public void mouseClicked(MouseEvent e) {
        System.out.println("mouseClicked");
        cards();
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        System.out.println("mouseEntered");
    }

    @Override
    public void mouseExited(MouseEvent e) {
        System.out.println("mouseExited");
    }

    @Override
    public void mousePressed(MouseEvent e) {
        System.out.println("mousePressed");
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        System.out.println("mouseReleased");
    }

    public static void loadImages(){
        images = new ImageIcon[4];
        ran = new Random();
        threeRandoms = new int[3];
        for (int i = 1; i <= images.length; i++) {

            images[i-1] = new ImageIcon("Drawables//Images//" + i + ".png");

        }
    }

    public static void main(String[] args) {
        loadImages();
        SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                 new Cards();
             }
          });       
    }

}