2

我有个问题。我可以在 body MousePressed 中创建 glasspane 吗?如果是的话,任何人都可以写信给我吗?我的意思是我按下鼠标按钮,玻璃窗格可见,我可以在他身上绘画。

编辑

好的,我现在有我想要的了。当我单击鼠标按钮时,我的玻璃窗格正在创建,当我释放此按钮时消失。现在我有另一个问题。我应该在哪里创建我的绘画方法。我想使用鼠标拖动在这个玻璃窗格上绘制矩形。我必须在哪里实施绘画方法?在其他班级还是在这个活动中?我实现了一个我的尝试绘制功能,但我不知道这是否是好方法。这是我的代码:

public class Selection extends JPanel
{
    static Point startPoint;
    public static void GUI()
    {

        final JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        JButton button = new JButton("Select");
        final JPanel glassPane = new JPanel();


        frame.setSize(400, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        panel.add(button);
        glassPane.setOpaque(false);
        frame.add(panel);


        frame.setGlassPane(glassPane);
        glassPane.addMouseListener(new MouseAdapter()
        {

            @Override
            public void mousePressed(MouseEvent e)
            {
                super.mousePressed(e);
                System.out.println("f.getGlassPane() mousePressed");
                if(e.getButton() == MouseEvent.BUTTON1)


                frame.getGlassPane().setVisible(true);

                startPoint=e.getPoint();

                Graphics2D g = null;
                Graphics2D g2 = (Graphics2D) g;
                Rectangle2D rect = new Rectangle2D.Double();
                rect.setFrameFromDiagonal(e.getPoint().x, e.getPoint().y,startPoint.x, startPoint.y);
                g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5F));
                g2.setColor(Color.BLUE);
                g2.fill(rect);
                g2.draw(rect);

            }

        });
        glassPane.addMouseMotionListener(new MouseMotionListener() {


            @Override
            public void mouseDragged(MouseEvent e) 
            {


            }

            @Override
            public void mouseMoved(MouseEvent e) {
                // TODO Auto-generated method stub

            }
        });

        frame.addMouseListener(new MouseAdapter()
        {
            @Override
            public void mousePressed(MouseEvent e)
            {
                super.mousePressed(e);
                if(e.getButton() == MouseEvent.BUTTON1)

                frame.getGlassPane().setVisible(true);

            }
            public void mouseReleased(MouseEvent e)
            {                     
                    frame.getGlassPane().setVisible(false);
            }
        });



        frame.setVisible(true);
    }


        int x1, x2, y1,y2;
        public void paintComponent(Graphics g)
        {
            Graphics2D g2 = (Graphics2D)g;
             g2.drawRect(x1,y1, x2, y2);
        }


    public static void main(String[] args) 
    {

        GUI();
    }

}
4

2 回答 2

2

嗨,请查看我对其他问题的回答,我在其中介绍了一种可以使用玻璃窗格来模拟对话行为的方法。在那里,您已经展示了如何在我的情况下单击鼠标右键来显示和隐藏它。这个例子应该让你很好地开始。

于 2011-05-13T10:15:44.320 回答
1

我认为创建玻璃板并将其附加到RootPaneContainer从内部moussePressed()方法没有问题。

但是,我可能想知道为什么每次用户单击鼠标时都创建一个新的玻璃窗格;那不会很高效;在前面创建和附加一个玻璃窗格然后在鼠标单击期间更改其内容可能更明智)。

现在,关于“在玻璃板上绘画”,这取决于你所说的“绘画”是什么意思,如果这意味着使用“ Graphics”实例直接在玻璃板上绘画,答案是否定的(好吧,实际上你可以但是,您的绘画将在第一次 UI 刷新时消失...)

这种绘画必须paintComponent()以您的玻璃窗格的方法发生(您必须覆盖)。

于 2011-05-13T10:16:01.090 回答