当我点击jPanel(Java)时如何调用函数? [英] How to call a function when I click on a jPanel (Java)?

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

问题描述

我正在使用Java中的Netbeans IDE。



我有一个带有一个JPanel的表单。
每个JPanel都有一个gridLayout 3x3,每个地方都有一个代表数字[0,1,2,3,4,5,6,7,8]的图像(创建的图像使用一个自定义的类,



当用户点击它们时,我想要在面板中交换两个图像(首先点击:没有动作,第二个我已经创建了一个函数exchangeComponents和一个测试代码(如:

  exchangeComponents(0,8,jPanel1)

它正确地交换位于位置1(第一行,第一列)和位置2(第三行,第三列)中的图像。



函数a creted是以下:

  public void exchangeComponents(int component1,int component2,JPanel jpanel){
try {
组件aux1 = jpanel.getComponent(component1);
Point aux1Loc = aux1.getLocation();
组件aux2 = jpanel。 getComponent(COMPONENT2);
Point aux2Loc = aux2.getLocation();
aux1.setLocation(aux2Loc);
aux2.setLocation(aux1Loc);
}
catch(java.lang.ArrayIndexOutOfBoundsException ex){/ *错误!函数的输入不正确* /
System.exit(1);
}
}

我想我有一个事件调用函数exchangeComponents()当用户点击jPanel1上的一个图像,但该怎么办?以及如何检查用户选择的组件(图像)?
我只知道当我创建一个按钮,如果点击它(从IDE)一个事件,如

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

被创建,我填写的代码被执行。



提前谢谢你的任何提示。

解决方案

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

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

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

  boolean hasclicked1 = false; 
JLabel click1label = null;

public void mouseClicked(MouseEvent me){
if(!hasclicked1){// clicked first pic
haveclicked1 = true;
click1label =(JLabel)me.getSource();
} else {// clicked second pic
haveclicked1 = false;
exchangeComponents(click1label,(JLabel)me.getSource(),/ *你的jpanel这里* /);
}
//现在更改exchangeComponents,因此它使用JLabels作为参数
public void exchangeComponents(JLabel component1,JLabel component2,JPanel jpanel){
try {
Component aux1 = component1;
Point aux1Loc = aux1.getLocation();
组件aux2 = component2;
Point aux2Loc = aux2.getLocation();
aux1.setLocation(aux2Loc);
aux2.setLocation(aux1Loc);
} catch(java.lang.ArrayIndexOutOfBoundsException ex){/ *错误!函数的输入不正确* /
System.exit(1);
}
}

如果您不使用JLabels的图像,将代码中的JLabel替换为正在使用的代码...



编辑:对不起,我不认为我不清楚,你的类与exchangeComponents的方法必须实现MouseListener。然后,在mouseClicked事件中放上我给的代码。确保在您的课堂中包含变量 hasclicked1 click1label 。让你类似这样的东西

  public class ComponentExchanger implements MouseListener {
boolean hasclicked1 = false;
JLabel click1label = null;
JPanel mainPanel;
public ComponentExchanger(){
//创建JFrame,JPanel等
JFrame f = new JFrame();
//等。
mainPanel = new JPanel();
f.add(mainPanel);
//设置面板的布局等
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);
/ *更多代码* /
f.setVisible(true);
}
}

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


public void mouseClicked(MouseEvent me){
if(!hasclicked1){// clicked first pic
haveclicked1 = true;
click1label =(JLabel)me.getSource();
} else {// clicked second pic
haveclicked1 = false;
exchangeComponents(click1label,(JLabel)me.getSource(),mainPanel);
}
//现在更改exchangeComponents,因此它使用JLabels作为参数
public void exchangeComponents(JLabel component1,JLabel component2,JPanel jpanel){
try {
Component aux1 = component1;
Point aux1Loc = aux1.getLocation();
组件aux2 = component2;
Point aux2Loc = aux2.getLocation();
aux1.setLocation(aux2Loc);
aux2.setLocation(aux1Loc);
} catch(java.lang.ArrayIndexOutOfBoundsException ex){/ *错误!函数的输入不正确* /
System.exit(1);
}
}

//此外,您将需要包含其他mouselistener实现的方法,只需
//将它们留空
}


I'm working with Netbeans IDE in Java.

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

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

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

exchangeComponents (0,8,jPanel1)

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

The function a creted is the following:

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);
   }
}

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.

解决方案

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

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);
  }
}

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

EDIT: 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)时如何调用函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆