单击jPanel(Java)时如何调用函数?

How to call a function when I click on a jPanel (Java)?(单击jPanel(Java)时如何调用函数?)

本文介绍了单击jPanel(Java)时如何调用函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Java 中的 Netbeans IDE.

I'm working with Netbeans IDE in Java.

我有一个带有一个 JPanel 的表单.每个 JPanel 都有一个 gridLayout 3x3,每个地方都有一个代表数字 [0,1,2,3,4,5,6,7,8] 的图像(图像是使用自定义类创建的,不只是适合实验室中的图像).

I've a form with one JPanel. Each JPanel has a gridLayout 3x3 and in each place there is an image representing a number[0,1,2,3,4,5,6,7,8](the image is created used a custom class,not just fitting the image in a lab).

当用户单击它们时,我希望能够在面板中交换两个图像(第一次单击:无操作,第二次单击:切换 jPanel 组件中安装的两个图像).

I want to be able to exchange two images in the panel when the user click them (First click: no action , second click: switch the two images fitted in the jPanel Components).

我已经创建了一个函数 exchangeComponents 和一个测试代码(比如:

I already created a function exchangeComponents and with a test code (like:

exchangeComponents (0,8,jPanel1)

它正确地交换位于位置 1(第 1 行第 1 列)和位置 2(第 3 行第 3 列)的图像.

it exchanges correctly the images located in position1 (1st row,1st column) and in position2 (3rd row,3rd column).

创建的函数如下:

public void exchangeComponents(int component1,int component2,JPanel jpanel){
    try{
   Component aux1 = jpanel.getComponent(component1);
   Point aux1Loc = aux1.getLocation();
   Component aux2 = jpanel.getComponent(component2);
   Point aux2Loc = aux2.getLocation();
   aux1.setLocation(aux2Loc);
   aux2.setLocation(aux1Loc);
   }
   catch (java.lang.ArrayIndexOutOfBoundsException ex){ /* error! bad input to the function*/
       System.exit(1);
   }
}

我想当用户单击 jPanel1 上的一个图像时,我需要一个调用函数 exchangeComponents() 的事件,但我应该怎么做呢?以及如何检查用户选择了哪些组件(图像)?我只知道,当我创建一个 Button 时,如果单击它(从 IDE 中)一个类似的事件

I suppose I neeed to have an event that call the function exchangeComponents() when the user click on one of the images on the jPanel1 but how should I do it? and how to check what components (images) the user has selected? I just know that when I create a Button if a click on it (from the IDE) an event like

 private void button1ActionPerformed(java.awt.event.ActionEvent evt) {  
// some code..
}

创建并执行我填写的代码.

is created and the code I fill in is executed.

提前感谢您的任何提示.

Thank you in advance for any hint.

推荐答案

您需要将相同的鼠标侦听器添加到您的所有 JLabel 或您拥有的图像容器中,例如:

You need to add the same mouse listener to all you JLabels or whatever container you have for your images, like:

img1.addMouseListener(this);
img2.addMouseListener(this);

等,然后用 MouseEvent.getSource(); 检测你点击了哪个Jlabel,像这样

etc., then detect which Jlabel you clicked with MouseEvent.getSource(); , like this

boolean hasclicked1=false;
JLabel click1label=null;

public void mouseClicked(MouseEvent me){
  if(!hasclicked1){ //clicked first pic
    hasclicked1 = true;
    click1label = (JLabel) me.getSource();
  } else { //clicked second pic
    hasclicked1 = false;
    exchangeComponents(click1label, (JLabel) me.getSource(), /*your jpanel here*/);
  }
  //now change exchangeComponents so it uses JLabels as parameters
public void exchangeComponents(JLabel component1, JLabel component2, JPanel jpanel){
  try{
    Component aux1 = component1;
    Point aux1Loc = aux1.getLocation();
    Component aux2 = component2;
    Point aux2Loc = aux2.getLocation();
    aux1.setLocation(aux2Loc);
    aux2.setLocation(aux1Loc);
  } catch (java.lang.ArrayIndexOutOfBoundsException ex) { /* error! bad input to the function*/
   System.exit(1);
  }
}

如果您没有对图像使用 JLabel,请将代码中的 JLabel 替换为您正在使用的任何内容...

If you are not using JLabels for the images though, replace JLabel in the code with whatever you are using...

抱歉,我不认为我说得不清楚,但是您的带有方法 exchangeComponents 的类必须实现 MouseListener.然后,在 mouseClicked 事件中输入我为它提供的代码.确保在您的类中包含变量 hasclicked1click1label.让你上课是这样的

Sorry, I don't think I made this unclear, but your class with the method exchangeComponents has to implement MouseListener. Then, in the mouseClicked event put the code I gave for it. Make sure to include the variables hasclicked1 and click1label in your class. Make you class something like this

public class ComponentExchanger implements MouseListener {
boolean hasclicked1=false;
JLabel click1label=null;
JPanel mainPanel;
public ComponentExchanger(){
   //create JFrame, JPanel, etc.
   JFrame f=new JFrame();
   //etc.
   mainPanel=new JPanel();
   f.add(mainPanel);
   //set layout of panel, etc.
   for(int i=0;i<9;i++){
      JLabel l=new JLabel(/*label image here*/);
      Point loc=new Point(/*coordinates here*/);
      l.setLocation(loc);
      mainPanel.add(l);
      /*more code*/
      f.setVisible(true);
   }
}

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


public void mouseClicked(MouseEvent me){
  if(!hasclicked1){ //clicked first pic
    hasclicked1 = true;
    click1label = (JLabel) me.getSource();
  } else { //clicked second pic
    hasclicked1 = false;
    exchangeComponents(click1label, (JLabel) me.getSource(), mainPanel);
  }
  //now change exchangeComponents so it uses JLabels as parameters
public void exchangeComponents(JLabel component1, JLabel component2, JPanel jpanel){
  try{
    Component aux1 = component1;
    Point aux1Loc = aux1.getLocation();
    Component aux2 = component2;
    Point aux2Loc = aux2.getLocation();
    aux1.setLocation(aux2Loc);
    aux2.setLocation(aux1Loc);
  } catch (java.lang.ArrayIndexOutOfBoundsException ex) { /* error! bad input to the function*/
   System.exit(1);
  }
}

//Also, you will need to include the other mouselistener implemented methods, just 
//leave them empty
}

这篇关于单击jPanel(Java)时如何调用函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:单击jPanel(Java)时如何调用函数?

基础教程推荐