0

非常感谢你,但我需要 Listener 方法并现在解决它。但现在我无法看到按钮,直到我将鼠标悬停在它们上!我认为这只是一个小错误,但我无法弄清楚!现在我的代码如下...

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class PaintMain extends JFrame
{
    JPanel colorPanel = new JPanel();
    JPanel drawPanel = new JPanel();
    JPanel selectPanel = new JPanel();
    JButton[][] randomColors = new JButton[5][5];
    Color selectedColor = Color.BLACK;
    Random colorGenerator = new Random();
    int curx = drawPanel.getX();
    int cury = drawPanel.getY();
    public PaintMain()
    {
        setLayout(new BorderLayout());
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        colorPanel.setLayout(new GridLayout(5,5));
        drawPanel.setLayout(new BorderLayout());
        drawPanel.setBackground(Color.WHITE);

        for (int i = 0; i < 5; i++) 
        {
            for (int j = 0; j < 5; j++) 
            {
                randomColors[i][j]=new JButton();
                randomColors[i][j].setBackground(getRandomColor());
                randomColors[i][j].addActionListener(new ActionListener() 
                {                   

                    @Override
                    public void actionPerformed(ActionEvent e) 
                    {
                        if( e.getSource() instanceof JButton) 
                        {
                               selectedColor=((JButton)e.getSource()).getBackground();
                               System.out.println(selectedColor);
                        }                       
                    }
                });
                colorPanel.add(randomColors[i][j]);
            }           
        }

        this.addMouseMotionListener(new MouseLsnr());
        add(colorPanel, BorderLayout.WEST);
        add(drawPanel, BorderLayout.CENTER);

        setVisible(true);
        pack();     
    }

    public Color getRandomColor() 
    {
        return new Color(colorGenerator.nextInt(256), colorGenerator.nextInt(256), colorGenerator.nextInt(256));
    }

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

    public void paint(Graphics g)
    {
        //super.paint(g);
        g.setColor(selectedColor);
        g.drawRect(curx, cury, 2, 1);
    }

    class MouseLsnr implements MouseMotionListener
    {       
        public void mouseDragged(MouseEvent arg0) {

            System.out.println(arg0.getX()+":"+arg0.getY());
            curx=arg0.getX();
            cury=arg0.getY();

            repaint();
        }

        public void mouseMoved(MouseEvent arg0) 
        {


        }
    }
}
4

1 回答 1

1

你会想仔细看看自定义绘画

在此处输入图像描述

您可能想阅读如何编写鼠标侦听器

public class RandomColorPane {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        new RandomColorPane();
    }

    public RandomColorPane() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new MainPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }

        });
    }

    public class MainPane extends JPanel {

        JPanel colorPanel = new JPanel();
        JPanel drawPanel = new DrawPane();
        JPanel selectPanel = new JPanel();
        JPanel[][] randomColors = new JPanel[5][5];
        Color selectedColor = Color.BLACK;
        Random colorGenerator = new Random();
        int curx = drawPanel.getX();
        int cury = drawPanel.getY();

        public MainPane() {
            setLayout(new BorderLayout());

            colorPanel.setLayout(new GridLayout(5, 5));
            drawPanel.setLayout(new BorderLayout());
            drawPanel.setBackground(Color.WHITE);

            for (int i = 0; i < 5; i++) {
                for (int j = 0; j < 5; j++) {
                    randomColors[i][j] = new JPanel();
                    randomColors[i][j].setPreferredSize(new Dimension(25, 25));
                    randomColors[i][j].setBackground(getRandomColor());
//                    randomColors[i][j].addActionListener(new ActionListener() {
//                        @Override
//                        public void actionPerformed(ActionEvent e) {
//                            if (e.getSource() instanceof JButton) {
//                                selectedColor = ((JButton) e.getSource()).getBackground();
//                                drawPanel.setForeground(selectedColor);
//                                drawPanel.repaint();
//                            }
//                        }
//
//                    });
                    randomColors[i][j].addMouseListener(new MouseAdapter() {

                        @Override
                        public void mouseEntered(MouseEvent e) {
                            drawPanel.setForeground(((JPanel)e.getSource()).getBackground());
                            drawPanel.repaint();
                        }

                    });
                    colorPanel.add(randomColors[i][j]);
                }
            }

            add(colorPanel, BorderLayout.WEST);
            add(drawPanel, BorderLayout.CENTER);
        }

        public Color getRandomColor() {
            return new Color(colorGenerator.nextInt(256), colorGenerator.nextInt(256), colorGenerator.nextInt(256));
        }

    }

    public class DrawPane extends JPanel {

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(20, 20);
        }

        @Override
        protected void paintComponent(Graphics g) {

            super.paintComponent(g);

            int x = (getWidth() - 10) / 2;
            int y = (getHeight() - 10) / 2;

            g.setColor(getForeground());
            g.fillRect(x, y, 20, 20);

        }

    }

}
于 2012-10-21T19:44:19.403 回答