我希望在MouseReleased事件中获取JLabel的名称。 所以,我有100个标签的JLabel数组,我想取回他们的名字, 所以我可以修改100布尔的布尔数组。 也许如果你熟悉1010game我必须创建它。 简单地说,我想在MouseReleased事件中获取JPanel的名称。
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
public class needsrefactor extends JFrame {
JFrame Window;
JPanel a, b;
SouthFrame sf = new SouthFrame();
final public JLabel[] GamePlace;
GridLayout lay;
MouseListener m1;
Boolean[] BackEnd;
MouseAdapter lis;
LabelAdapter lb = new LabelAdapter();
public needsrefactor() {
BackEnd = new Boolean[100];
b = new JPanel();
b.setOpaque(true);
b.setBackground(Color.red);
b.setVisible(true);
// EVENTES
m1 = new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
JComponent JC = (JComponent) e.getSource();
TransferHandler TH = JC.getTransferHandler();
TH.exportAsDrag(JC, e, TransferHandler.COPY);
System.out.println("Foo");
}
@Override
public void mouseReleased(MouseEvent e) {
System.out.println(e.getID());
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
};
Window = new JFrame();
Window.doLayout();
Window.setVisible(true);
Window.setSize(800, 1000);
Window.setResizable(false);
// Window.getContentPane().setLayout(new BorderLayout());
Window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Window.setLayout(new BorderLayout());
// lay = new GridLayout(10,10);
GamePlace = new JLabel[100];
a = new JPanel(new GridLayout(10, 10, 1, 1));
a.setSize(799, 500);
// a = new JPanel(new FlowLayout(10));
a.setBackground(Color.white);
for (int i = 0; i < 100; i++) {
BackEnd[i] = false;
this.GamePlace[i] = new JLabel("Label");
this.GamePlace[i].setOpaque(true);
this.GamePlace[i].setBackground(Color.black);
this.GamePlace[i].setVisible(true);
// this.GamePlace[i].setMinimumSize(new Dimension(10, 10));
// this.GamePlace[i].setMaximumSize(new Dimension(15, 15));
this.a.add(GamePlace[i]);
this.GamePlace[i].addMouseListener(m1);
this.GamePlace[i].setTransferHandler(new TransferHandler("icon"));
this.GamePlace[i].setName(Integer.toString(i));
/*
* this.GamePlace[i].addMouseListener(new MouseAdapter() {
*
* @Override public void mouseEntered(MouseEvent me) {
* System.out.println(JLabel.getName()); } });
*/
}
// this.a.setPreferredSize(new Dimension(800,300));
// this.GamePlace[1].setIcon(new
// ImageIcon(needsrefactor.class.getResource("/sources/picture.png")));
// a.setBounds(1, 1, 800, 600);
Window.add(a, BorderLayout.CENTER);
// b=new JPanel(new GridLayout(5,5));
// b.setSize(800, 299);
// Window.add(b,BorderLayout.SOUTH);
sf.setOpaque(true);
sf.setBackground(Color.green);
sf.setVisible(true);
sf.setBounds(WIDTH, WIDTH, 799, 300);
// Window.add(sf,BorderLayout.SOUTH );
Window.add(sf.SouthFrame, BorderLayout.SOUTH);
}
}
SouthFrame: http://pastebin.com/J29RtZgh