【问题标题】:Problems with detecting mouseClick in a JPanel and preventing circle from painting in JPanel在 JPanel 中检测 mouseClick 并防止在 JPanel 中绘制圆圈的问题
【发布时间】:2015-07-08 22:23:09
【问题描述】:

我当前的程序允许用户围绕 JFrame 移动一个圆圈,并通过按下 JFrame 底部的 JPanel 中显示的一种颜色来更改其颜色。

我的代码:

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.border.Border;
import javax.swing.BorderFactory;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
import java.awt.GridLayout;

public class SixthProgram
{
    public static void main(String[] args)
    {
        GUI prog=new GUI("SixthProgram");
        prog.setBounds(350,250,500,250);
        prog.setVisible(true);
    }
}

class GUI extends JFrame implements MouseListener, MouseMotionListener
{
    JButton button;
    JPanel colorPan, color1, color2, color3 ,color4 ,color5;
    Color color=Color.BLACK;

    int x=3,y=30;

    public GUI(String header)
    {
        super(header);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        maker();

        addMouseListener(this);
        addMouseMotionListener(this);

        add(colorPan, BorderLayout.SOUTH);
    }

    public void maker()
    {
        colorPan = new JPanel();

        Border raisedbevel = BorderFactory.createRaisedBevelBorder();
        Border loweredbevel = BorderFactory.createLoweredBevelBorder();
        Border compound = BorderFactory.createCompoundBorder(raisedbevel, loweredbevel);
        colorPan.setBorder(compound);

        colorPan.setLayout(new GridLayout(1, 0));

        color1 = new JPanel();
        color2 = new JPanel();
        color3 = new JPanel();
        color4 = new JPanel();
        color5 = new JPanel();

        color1.setBackground(Color.WHITE);
        color2.setBackground(Color.GREEN);
        color3.setBackground(Color.RED);
        color4.setBackground(Color.BLUE);
        color5.setBackground(Color.BLACK);

        colorPan.add(color1);
        colorPan.add(color2);
        colorPan.add(color3);
        colorPan.add(color4);
        colorPan.add(color5);

    }

    @Override
    public void paint(Graphics g)
    {
        //g.setColor(Color.WHITE);
        //g.fillRect(0,0,getWidth(),getHeight());
        super.paint(g); //Do the same thing as above(Clear jframe)

        g.setColor(color);
        g.fillOval(x,y,50,50);
    }

    public void mouseExited(MouseEvent e) //MouseListener overrided methods
    {}

    public void mouseEntered(MouseEvent e)
    {}

    public void mouseReleased(MouseEvent e)
    {}

    public void mousePressed(MouseEvent e)
    {
        System.out.println("Press");    
        if(e.getX()+50 < getWidth() && e.getY()+50 < getHeight()) // Preventing out of bounds
        {
            x=e.getX();
            y=e.getY();
            repaint();
        }
    }

    public void mouseClicked(MouseEvent e) //Press+Release=Click
    {
        System.out.println("Click");
        if((e.getX()>=8 && e.getX()<=105) && (e.getY()>=232 && e.getY()<=243))
            color=Color.WHITE;
        else if((e.getX()>=106 && e.getX()<=203) && (e.getY()>=232 && e.getY()<=243))
            color=Color.GREEN;
        else if((e.getX()>=204 && e.getX()<=301) && (e.getY()>=232 && e.getY()<=243))
            color=Color.RED;
        else if((e.getX()>=302 && e.getX()<=399) && (e.getY()>=232 && e.getY()<=243))
            color=Color.BLUE;
        else if((e.getX()>=400 && e.getX()<=489) && (e.getY()>=232 && e.getY()<=243))
            color=Color.BLACK;
        repaint();
    }

    public void mouseDragged(MouseEvent e) //MouseMotionListener overrided methods
    {
        System.out.println("Dragged to ("+ e.getX() +","+ e.getY() +")");
        if((e.getX()>=3 && e.getY()>=30) && (e.getX()+50<getWidth() && e.getY()+50<getHeight())) //If circle is dragged in the JFrame
        {
            x=e.getX();
            y=e.getY();
            repaint();
        }
    }

    public void mouseMoved(MouseEvent e)
    {}

}

上面的代码按预期工作并产生输出:

当我也单击底部 JPanel 中的颜色时,程序会按预期工作并更改圆圈的颜色。

出现的两个问题是:

  1. 当我调整 JFrame 的大小时,颜色选择器无法按预期工作。这是由于我在 mouseClicked 方法中硬编码的愚蠢方式:

    public void mouseClicked(MouseEvent e) //Press+Release=Click
    {
        System.out.println("Click");
        if((e.getX()>=8 && e.getX()<=105) && (e.getY()>=232 && e.getY()<=243))
            color=Color.WHITE;
        else if((e.getX()>=106 && e.getX()<=203) && (e.getY()>=232 && e.getY()<=243))
            color=Color.GREEN;
        else if((e.getX()>=204 && e.getX()<=301) && (e.getY()>=232 && e.getY()<=243))
            color=Color.RED;
        else if((e.getX()>=302 && e.getX()<=399) && (e.getY()>=232 && e.getY()<=243))
            color=Color.BLUE;
        else if((e.getX()>=400 && e.getX()<=489) && (e.getY()>=232 && e.getY()<=243))
            color=Color.BLACK;
        repaint();
    }
    
  2. 第二个问题是圆圈可以拖到颜色选择器上。我不知道我应该怎么做才能防止这种情况发生。我可以像对问题 #1 所做的那样对值进行硬编码,但这会产生与问题 #1 相同的问题。

我不希望使用 setResizable(false) 并且 JFrame 可以调整大小。

如何解决上述问题?

【问题讨论】:

  • 如果您使用 JButtons 作为颜色而不是直接绘制它们,那应该可以缓解这两个问题
  • 在我的开源项目中有一个用于在组件中移动形状的包,您对此感兴趣吗,或者您宁愿自己解决这个问题以获得乐趣?
  • @ControlAltDel,谢谢。我已经用JButtons 替换了那些JPanels。我这样做是为了好玩,为了提高我的挥杆技巧。

标签: java swing jframe jpanel


【解决方案1】:

我刚刚以更好的方式重新实现了您的问题。这是现在的样子:问题解决了。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class CirclePainter implements MouseMotionListener, ActionListener {
    private JFrame mainFrame;
    private JPanel colorPanel, circlePanel;
    private JButton whiteColorButton, redColorButton, greenColorButton,
            blueColorButton;

    private int circleWidth = 3, circleHeight = 15;
    private Color circleColor = Color.black;

    public CirclePainter() {
        initGui();
    }

    public void initGui() {
        mainFrame = new JFrame("Circle");
        mainFrame.setLayout(new BorderLayout());
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.setSize(500, 400);

        colorPanel = new JPanel(new FlowLayout());

        whiteColorButton = new JButton();
        whiteColorButton.setBackground(Color.white);
        whiteColorButton.setActionCommand("white");
        whiteColorButton.addActionListener(this);
        redColorButton = new JButton();
        redColorButton.setBackground(Color.red);
        redColorButton.setActionCommand("red");
        redColorButton.addActionListener(this);
        greenColorButton = new JButton();
        greenColorButton.setBackground(Color.green);
        greenColorButton.setActionCommand("green");
        greenColorButton.addActionListener(this);
        blueColorButton = new JButton();
        blueColorButton.setBackground(Color.blue);
        blueColorButton.setActionCommand("blue");
        blueColorButton.addActionListener(this);

        colorPanel.add(whiteColorButton);
        colorPanel.add(redColorButton);
        colorPanel.add(greenColorButton);
        colorPanel.add(blueColorButton);

        circlePanel = new JPanel() {
            @Override
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.setColor(circleColor);
                g.fillOval(circleWidth, circleHeight, 50, 50);
            }
        };
        circlePanel.addMouseMotionListener(this);
        circlePanel.setBackground(Color.yellow);
        mainFrame.add(circlePanel, BorderLayout.CENTER);

        mainFrame.add(colorPanel, BorderLayout.PAGE_END);
        mainFrame.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        switch (e.getActionCommand()) {
        case "white":
            circleColor = Color.white;
            circlePanel.repaint();
            break;
        case "red":
            circleColor = Color.red;
            circlePanel.repaint();
            break;
        case "green":
            circleColor = Color.green;
            circlePanel.repaint();
            break;
        case "blue":
            circleColor = Color.blue;
            circlePanel.repaint();
            break;
        default:
            break;
        }
    }

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new CirclePainter();
            }
        });

    }

    @Override
    public void mouseDragged(MouseEvent e) {
        if ((e.getX() >= 0 && e.getY() >= 0)
                && (e.getX() <= mainFrame.getWidth() - 60)
                && (e.getY() <= mainFrame.getHeight() - 110)) {
            circleWidth = e.getX();
            circleHeight = e.getY();
            circlePanel.repaint();
        }

    }

    @Override
    public void mouseMoved(MouseEvent arg0) {

    }

}

这是输出:

【讨论】:

  • 感谢您转换我的程序。 circleWidthcircleHeight 是令人困惑的名称,因为它们表示圆的位置,而不是宽度和高度。我有一些问题:为什么要使用SwingUtilities.invokeLater?当我也删除它时,该程序的工作方式相同。 (e.getY() &lt;= mainFrame.getHeight() - 110)(e.getX() &lt;= mainFrame.getWidth() - 60) 中的 11060 是如何得到的,它们分别表示什么?
  • 对不起,我没有注意到这些坏名字。我使用 SwingUtilities.InvokeLater 是因为 Swing 数据结构不是线程安全的,因此为了向程序员提供一种易于实现的防止并发访问它们的方法,使用了它。它将在 AWT 线程上运行这段代码。这使您可以从其他线程修改 GUI。另外,您的程序将球拖到颜色面板后面,因此我仅通过添加此限制来限制它: (e.getY()
【解决方案2】:

您不应该覆盖 JFrame 的 paint() 方法。自定义绘画是通过覆盖 JPanel(或 JComponent)的 paintComponent() 方法完成的,然后将面板添加到框架中。您可以使用 BorderLayout.CENTER 将此面板添加到框架中。

对于您的颜色,您应该创建一个单独的面板并向面板添加按钮来表示每种颜色。您可以将每个按钮的背景设置为特定颜色。您可以使用 GridLayout。然后将此面板添加到框架的BorderLayout.PAGE_END

然后,您可以为每个按钮添加一个ActionListener,而不是使用 MouseListener。基本代码是:

public void actionPerformed(ActionEvent e)
{
    JButton button = (JButton)e.getSource();
    color = button.getBackground();
    repaint();
}

【讨论】:

  • 好的。谢谢。顺便说一句,从actionPerformed 获取颜色的好方法。
猜你喜欢
  • 2015-01-05
  • 1970-01-01
  • 1970-01-01
  • 2020-08-09
  • 1970-01-01
  • 2018-08-09
  • 2012-12-12
  • 2014-05-20
  • 1970-01-01
相关资源
最近更新 更多