Java:JPanel Rectangle问题?

时间:2015-06-11 07:40:25

标签: java swing

我制作的程序可以在用户鼠标按下时再次改变矩形的颜色 按下默认颜色我在jpanel上画了这个 绘制组件方法的帮助。我用鼠标适配器写了鼠标按下的方法。但是如何制作一个 当用户鼠标按在矩形上时,将矩形颜色改为红色的功能在再次按下时也会出现默认颜色。这是图片
enter image description here
代码:

    public class A extends JPanel{
    public void paintComponent(Graphics g){
            super.paintComponents(g);

            g.setColor(Color.BLACK);
            g.fillRect(6, 10, 66, 70);

            addMouseListener(new MouseAdapter(){

            public void mousePressed(MouseEvent e){


            }
            });
            }
    }

主:

public class Main {

    public static void main(String[] args) {

        JFrame obj = new JFrame();
        A object = new A();
        obj.add(object);

        obj.setSize(450, 400);
        obj.setResizable(false);
        obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        obj.setLocationRelativeTo(null);

        obj.setVisible(true);
        }
}

3 个答案:

答案 0 :(得分:4)

添加到Gosu的答案:

如果您希望仅在单击矩形时切换颜色,则只需在if内添加MouseListener语句。

    frame.addMouseListener(new MouseAdapter() {

        public void mousePressed(MouseEvent e) {

             // Check if the point where the mouse was clicked is contained
             // within the drawing Rectangle.                          
             if (new Rectangle(6, 10, 66, 70).contains(e.getPoint())) {
                    object.switchColor();
                    object.revalidate();
                    object.repaint();
             }
         }
    });

但是,请记住,在实际的应用程序中,您不应该像本例中的矩形那样硬编码。

答案 1 :(得分:2)

试试这个:

public class QuickTester {

    public static void main(String[] args) {

        JFrame frame = new JFrame();
        final A object = new A();
        frame.add(object);

        frame.setSize(450, 400);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);

        frame.addMouseListener(new MouseAdapter() {

            public void mousePressed(MouseEvent e) {

                object.switchColor();
                //object.revalidate();
                object.repaint();
            }
        });

        frame.setVisible(true);
    }
}

class A extends JPanel {

    private Color currColor = Color.BLACK;

    public void switchColor() {
        currColor = (currColor == Color.BLACK) ? Color.RED : Color.BLACK;
    }

    public void paintComponent(Graphics g) {
        super.paintComponents(g);

        g.setColor(currColor);
        g.fillRect(6, 10, 66, 70);
    }
}

只要您在框架内单击,矩形就会在黑色和红色之间切换。

答案 2 :(得分:2)

你需要另一个JPanel(或类似的东西)用于必须改变颜色的方块。否则,您将无法注册广场内发生的鼠标事件以及外部发生的事件。 以下是一个小工作示例。

public class A extends JPanel {
    private Color currColor = Color.BLACK;
    JPanel p = new JPanel();
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        p.setSize(new Dimension(66,70));
        p.setLocation(6, 10);
        p.setBackground(currColor);

        p.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                switchColor();
                p.revalidate();
                p.repaint();
            }
        });
        add(p);
    }

    public void switchColor() {
        currColor = (currColor == Color.BLACK) ? Color.RED : Color.BLACK;
    }

    public static void main(String [] args) {
        JFrame frame = new JFrame();
        A t = new A();
        frame.add(t);
        frame.setSize(450,400);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}