鼠标单击JLabel数组(Java)

时间:2012-05-02 11:41:34

标签: java arrays swing jlabel mouse-listeners

我在java上创建了一个JLabels数组,它是[9] [9]。当我在这些标签上点击鼠标时,我想设置这些JLabels。有人可以帮帮我吗?

I tried this:
    //labels[c][d].addMouseListener(null);
    public void mouseClicked(MouseEvent me){
    //        for(Integer i=1;i<10;i++)
    //        {
    //            for(Integer j=1;j<10;j++)
    //            {
    //               
    //                if (me.getSource()==labels[i][j]);
    //                {
    //                  
    //                    labels[1][1].setVisible(true);
    //                }
    //            }
    //       
    //    }

4 个答案:

答案 0 :(得分:3)

使用JToggleButton中显示的Swing JToolbarButton pressing

在您的使用案例中,绿色未选择的图像将只是一个完全透明的图像,或者是所需BG颜色的图像。

更具体的例子:

Toggle Image

import java.awt.*;
import java.awt.image.*;
import javax.swing.*;

class ToggleImage {

    public static JToggleButton getButton(
        Image selected,
        Image unselected) {

        JToggleButton b = new JToggleButton();
        b.setSelectedIcon(new ImageIcon(selected));
        b.setIcon(new ImageIcon(unselected));
        b.setBorderPainted(false);
        b.setContentAreaFilled(false);
        b.setFocusPainted(false);
        b.setMargin(new Insets(0,0,0,0));

        return b;
    }

    public static Image getImage(boolean hasSquare) {
        int size = 60;
        BufferedImage bi = new BufferedImage(
            size,size,BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = bi.createGraphics();

        if (hasSquare) {
            g.setColor(Color.RED);
            g.fillRect(1,1,size-2,size-2);
        }

        g.dispose();
        return bi;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater( new Runnable() {
            public void run() {
                Image selected = getImage(true);
                Image unselected = getImage(false);

                int row = 2;
                int col = 5;
                JPanel p = new JPanel(new GridLayout(row,col));

                for (int ii=0; ii<row*col; ii++) {
                    p.add( getButton(selected, unselected) );
                }

                JOptionPane.showMessageDialog(null, p);
            }
        });
    }
}

请注意,按钮会对鼠标和键盘输入做出反应,而(默认情况下)标签不会。

答案 1 :(得分:2)

如果所有这些JLabel在开头都不可见 - 它们将无法捕获鼠标/键事件。如果您只想在点击后显示标签内容 - 只需在收到点击之前不要设置文字,如下所示:

public static void main ( String[] args )
{
    JFrame frame = new JFrame ();
    frame.setLayout ( new GridLayout ( 9, 9 ) );
    frame.getContentPane ().setPreferredSize ( new Dimension ( 300, 300 ) );

    final Random random = new Random ();
    for ( int i = 0; i < 9; i++ )
    {
        for ( int j = 0; j < 9; j++ )
        {
            final JLabel label = new JLabel ( "", JLabel.CENTER );
            label.setBorder ( BorderFactory.createLineBorder ( Color.LIGHT_GRAY ) );
            label.addMouseListener ( new MouseAdapter ()
            {
                public void mousePressed ( MouseEvent e )
                {
                    label.setText ( "" + random.nextInt ( 100 ) );
                }
            } );
            frame.add ( label );
        }
    }

    frame.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
    frame.pack ();
    frame.setLocationRelativeTo ( null );
    frame.setVisible ( true );
}

答案 2 :(得分:2)

您发布的代码似乎并不坏。但是,您始终将标签设置为[1][1]索引,而不是使用[i][j]

当然,如果您的JLabel确实是MouseEvent的来源,那么您可以放弃整个循环机制altogher,例如如果你使用

me.getSource()==labels[i][j]

要确定需要设置为可见的标签,可以用

替换整个双循环
((JLabel)me.getSource()).setVisible( true );

答案 3 :(得分:2)

我希望这会有所帮助。

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Clicker extends JPanel {
    private static final int ROWS = 9; 
    private static final int COLUMNS = 9;

    public Clicker() {
        setLayout(new GridLayout(COLUMNS, ROWS));

        JLabel labels[][] = new JLabel[ROWS][];
        for (int i = 0; i < ROWS; i++) {
            labels[i] = new JLabel[COLUMNS];    
        }

        for (int i = 0; i < ROWS; i++) {
            for (int j = 0; j < COLUMNS; j++) {
                labels[i][j] = new JLabel();
                labels[i][j].addMouseListener(createMouseListener());
                add(labels[i][j]);
            }
        }
    }

    public MouseAdapter createMouseListener() {
        return new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                JLabel label = (JLabel)e.getSource();

                if (!label.isEnabled()) {
                    label.setText("");
                    label.setEnabled(true);
                } else {
                    label.setText("Clicked");
                    label.setEnabled(false);
                }
            }
        };
    }


    public static void main(String[] args) {
        JFrame frame = new JFrame("Click me demo.");
        frame.setSize(500, 500);
        frame.setLayout(new BorderLayout());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new Clicker(),BorderLayout.CENTER);
        frame.setVisible(true);
    }
}