0

这是我的按钮代码 onclick 我希望我的程序等待用户单击一个 JPanel,并且当用户单击 JPanel 时,它应该在控制台上打印其名称。

此按钮代码未显示输出

  JPopupMenu popupMenu_1 = new JPopupMenu();

  JMenuItem mntmOneToOne = new JMenuItem("One to One");




 mntmOneToOne.setIcon(new ImageIcon("C:\\Users\\Ashad\\Desktop\\oneToone.png"));
 popupMenu_1.add(mntmOneToOne);

 OneToOne.addMouseListener(new MouseAdapter()
 {
    @Override
    public void mouseClicked(MouseEvent arg0)
    {
      MouseListener Listen= new MouseAdapter()
      {
          public void mousePressed(MouseEvent me)
           {
              String name=new String();
              JPanel panel = (JPanel) me.getSource();   

               //   name = panel.getName(); 

               System.out.println(panel.getName());
           }
       };
    }   

 });
4

2 回答 2

2

为了获得更好的帮助,请尽快发布 SSCCE,简短,可运行,可编译,

因为在我的 SSCCE 中工作,并且答案

这是我的按钮代码 onclick 我希望我的程序等待用户单击一个 JPanel,并且当用户单击 JPanel 时,它应该在控制台上打印其名称。

问题必须在您的其余代码中,

在此处输入图像描述

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;

public class MyGridLayout {

    public MyGridLayout() {
        JPanel bPanel = new JPanel();
        bPanel.setLayout(new GridLayout(10, 10, 2, 2));
        for (int row = 0; row < 10; row++) {
            for (int col = 0; col < 10; col++) {
                JPanel b = new JPanel() {
                    private static final long serialVersionUID = 1L;

                    @Override
                    public Dimension getPreferredSize() {
                        return new Dimension(20, 20);
                    }
                };
                b.putClientProperty("column", row);
                b.putClientProperty("row", col);
                b.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        JPanel btn = (JPanel) e.getSource();
                        System.out.println("clicked column " + btn.getClientProperty("column")
                                + ", row " + btn.getClientProperty("row"));
                    }
                });
                b.setBorder(new LineBorder(Color.blue, 1));
                bPanel.add(b);
            }
        }
        JFrame frame = new JFrame("PutClientProperty Demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(bPanel);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                MyGridLayout myGridLayout = new MyGridLayout();
            }
        });
    }
}
于 2013-05-05T21:06:35.140 回答
1

您在 MouseListener mouseClicked 方法中声明了一个 MouseAdapter,它只是坐在那里并且什么都不做,因为它什么也没做。如果要将 MouseListener 添加到面板,请执行以下操作:

panel.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseClicked(MouseEvent arg0) {
        JPanel panel = (JPanel) arg0.getSource();   
        System.out.println(panel.getName());
    }
});
于 2013-05-05T21:20:58.690 回答