微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

绘制框有效但线无效

如何解决绘制框有效但线无效

我创建了一个项目。当我按“新建框”时,我想创建一个蓝色框。然后当我按“线”时,我想画一条线并将线和框连接起来。我创建了框的代码,这是有效的,但行的代码不起作用。实际上行的代码正在另一个窗口/文件夹中工作,但在这里不起作用。可能是我写代码的时候写错了。

这个窗口和主要代码..

public class Cerceve extends JFrame implements ActionListener {
    JPanel MAINPANEL,LEFTPANEL,RIGHTPANEL;
    JButton btnBox,btnClear;
    DragListener drag = new DragListener();
    JButton btnLine;

    public Cerceve() {
        // ÇERÇEVE OLUŞTUR
        super("ÇİZGE PROGRAMI");
        setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
        setSize(1200,700);
        setResizable(false);
        setLocationRelativeto(null);
        setVisible(true);

        // ÜZERİNE BİLEŞENLERİ EKLE
        setLayout(new BorderLayout());

        MAINPANEL = new JPanel();
        MAINPANEL.setBackground(Color.WHITE);
        this.add(MAINPANEL,BorderLayout.CENTER);
        MAINPANEL.setLayout(new BorderLayout());

        LEFTPANEL = new JPanel();
        LEFTPANEL.setBackground(Color.GRAY);
        MAINPANEL.add(LEFTPANEL,BorderLayout.WEST);

        RIGHTPANEL = new JPanel();
        RIGHTPANEL.setBackground(Color.WHITE);
        MAINPANEL.add(RIGHTPANEL,BorderLayout.CENTER);

        btnBox = new JButton("New Box");
        LEFTPANEL.add(btnBox);

        btnClear = new JButton("Clear");
        LEFTPANEL.add(btnClear);

        btnLine = new JButton("Line");
        LEFTPANEL.add(btnLine);

        btnBox.addActionListener(this);
        btnClear.addActionListener(this);
        btnLine.addActionListener(this);

       }

        @Override
        public void actionPerformed(ActionEvent e) {
           if (e.getSource() == btnBox) {
               Box Box = new Box();
               RIGHTPANEL.add(Box);
               RIGHTPANEL.repaint();
               Box.addMouseMotionListener(drag);
               Box.addMouseListener(drag);
          }

           else if (e.getSource() == btnClear) {
               RIGHTPANEL.removeAll();
               RIGHTPANEL.repaint();
       }

           else if (e.getSource() == btnLine) {
               Line line = new Line();
               RIGHTPANEL.repaint();
               RIGHTPANEL.add(line);
               line.addMouseListener(line.mouseHandler);
               line.addMouseMotionListener(line.mouseMotionListener);

        }
    }
}

这是一个盒子包装

    public class Box extends JPanel {
    public Box() {
        setLayout(null);
        setBackground(Color.BLUE);
        setSize(50,50);
        setLocation(0,0);
    }

}

这是一个 DRAG LISTENER 包

    class DragListener extends MouseInputAdapter {
    Point location;
    MouseEvent mouseInfo;

    @Override
    public void mousepressed(MouseEvent me) {
        mouseInfo = me;
    }

    @Override
    public void mouseDragged(MouseEvent me) {
        if (SwingUtilities.isLeftMouseButton(me) == true) {
            Component component = me.getComponent();
            location = component.getLocation(location);
            int x = location.x - mouseInfo.getX() + me.getX();
            int y = location.y - mouseInfo.getY() + me.getY();
            component.setLocation(x,y);
        }
    }
}

这是一个完整的包裹

    public class Line extends JFrame {
    private int xBegin = 0;
    private int xEnd = 0;
    private int yBegin = 0;
    private int yEnd = 0;

    public MouseListener mouseHandler = new MouseAdapter() {

        @Override
        public void mousepressed(MouseEvent e) {
            xBegin = e.getX();
            yBegin = e.getY();
            repaint();
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            xEnd = e.getX();
            yEnd = e.getY();
            repaint();
        }
    };

    public MouseMotionListener mouseMotionListener = new MouseMotionAdapter() {

        @Override
        public void mouseDragged(MouseEvent e) {
            xEnd = e.getX();
            yEnd = e.getY();
            repaint();
        }
    };

    public void paint(Graphics g) {
        super.paint(g);
        g.drawLine(xBegin,yBegin,xEnd,yEnd);

    }

}

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。