如何在鼠标单击Java上调用方法?

时间:2015-04-30 13:57:04

标签: java jframe mouseclick-event

每次我们运行此代码时,它会显示54个随机卡中的3个。 我想打电话给cards1();鼠标单击时显示3个随机卡的方法。 每次我点击第3帧时,都应显示随机卡片。 请问有什么身体帮忙吗?

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

import javax.swing.*;



public class Cards extends JFrame  {

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

        frame.setTitle("Cards");

        frame.setSize(300, 200);

        frame.setLocationRelativeTo(null);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setVisible(true);

    }

    public Cards() {

        // Create array for cards

        cards1();
    }

    public void cards1() {
        ImageIcon[] images = new ImageIcon[54];
        for (int i = 1; i < images.length; i++) {

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

        }

        // Get random number between 1 & 54... three times

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

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

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

        }

        // Labels with gridLayout
        setLayout(new GridLayout(1, 4, 5, 5));

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

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

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

    }


}

4 个答案:

答案 0 :(得分:0)

将MouseListener实现到您的类:

import java.awt.*;
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(300, 200);

    frame.setLocationRelativeTo(null);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setVisible(true);

}

public Cards() {

    // Create array for cards

    cards1();
}
public void mouseClicked(MouseEvent arg0) {
 // HERE YOU CALL YOUR  cards1 to generate random cards: 
 cards1();
}

public void mouseExited(MouseEvent arg0) {

}

public void mouseEntered(MouseEvent arg0) {

}
.
.
. // TO THE END OF YOUR CLASS

答案 1 :(得分:0)

这样做

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

public class Cards implements MouseListener {

    public Cards() {
        JFrame frame = new JFrame("Cards");

         frame.setTitle("Cards");
         frame.setSize(300, 200);
         frame.setLocationRelativeTo(null);
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

         frame.addMouseListener(this);

         frame.setVisible(true);

    }

    public void cards1() {
        ImageIcon[] images = new ImageIcon[54];
        for (int i = 1; i < images.length; i++) {

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

         }

        // Get random number between 1 & 54... three times

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

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

             threeRandoms[i] = ran.nextInt(54);
        }

    }


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

    }

    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.");

    }

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

}

答案 2 :(得分:0)

 public static void main(String[] args) {
  Cards frame = new Cards();
  frame.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseClicked(MouseEvent e) {
       frame.cards1();
    }
  });
...
} // end of main

将您的帧类型更改为Cards,以便从mouseClicked方法内部调用cards1方法。

Additionals:

  • 只要点击cards1方法,就会初始化GridLayout和Label - 应该执行一次。 cards1方法只应更改内容。
  • 改进:在阵列中只读取一次图像。让cards1方法只访问数组中随机选择的三个图像

答案 3 :(得分:0)

在初始化main变量后,在frame方法内添加代码:

frame.addMouseListener(new MouseAdapter(){

   public void mouseClicked(MouseEvent e) {
      (Cards)frame.cards1();
   }

});

这基本上就是你添加鼠标监听器的方式。

但我看到另一个问题。这是方法card1 此方法会将JLabel的新实例添加到JFrame卡中。因此,只要将frame设置为可见,它就会显示。但再次单击它将添加3张新卡等等。