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

dra*_*mnl 5 java events components jpanel

我正在使用Java中的Netbeans IDE.

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

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

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

exchangeComponents (0,8,jPanel1)
Run Code Online (Sandbox Code Playgroud)

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

creted的功能如下:

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);
   }
}
Run Code Online (Sandbox Code Playgroud)

我想我需要有一个事件,当用户点击jPanel1上的一个图像时调用函数exchangeComponents()但是我应该怎么做?以及如何检查用户选择的组件(图像)?我只知道当我创建一个Button时,如果点击它(来自IDE)就像一个事件

 private void button1ActionPerformed(java.awt.event.ActionEvent evt) {  
// some code..
}
Run Code Online (Sandbox Code Playgroud)

已创建,我执行的代码已执行.

提前感谢您的任何提示.

Dan*_*mes 5

您需要为所有JLabel或图像的任何容器添加相同的鼠标侦听器,例如:

img1.addMouseListener(this);
img2.addMouseListener(this);
Run Code Online (Sandbox Code Playgroud)

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

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);
  }
}
Run Code Online (Sandbox Code Playgroud)

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

编辑:对不起,我不认为我不清楚,但是带有方法exchangeComponents的类必须实现MouseListener。然后,在mouseClicked事件中放入我为其提供的代码。确保包括变量hasclicked1,并click1label在您的类。让你上课像这样

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
}
Run Code Online (Sandbox Code Playgroud)