通过子级将JComponent添加到父级

时间:2019-06-13 02:24:05

标签: java swing jpanel jcomponent

enter image description here

我想完成与矩形(带有选择线)相似的图像。

基本上,我有一个矩形,我想在其周围有一条选择线。 为此,我想创建一个额外的JComponent。

目前,我只能绘制矩形。如何在Rectangle类中获取parentPanel JPanel,以便添加选择器?

public class TestPanel extends JFrame {

    public class Rectangle extends JComponent {

        public Rectangle(){
            setBounds(x1, y1, x2, y2);

            JPanel Selector = new JPanel();
            //Adds Selector to parentPanel within Rectangle
            //setBounds(x1-1, y1-1, x2+1, y2+1)
            //!Problem parent is initially null! cant even a use property
            //Life hacks?
        }
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawRect(0, 0, getWidth(), getHeight());
        }
    }

    public TestPanel() {
        Rectangle Rectangle = new Rectangle();
        JPanel parentFrame = new JPanel();
        parentFrame.add(Rectangle);

        setSize(200, 200);
        setVisible(true);
    }

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

如果我尝试将选择器添加到矩形内,它将退出绘图区域。如果我调整绘图区域的大小,它将无法扩展以用于以后的开发。

如果可能的话,我会避免双重绑定:

public TestPanel() {
            Rectangle Rectangle = new Rectangle();
            JPanel parentPanel = new JPanel();
            parentPanel.add(Rectangle);
            Rectangle.addParent(parentPanel)
            ...
        }

1 个答案:

答案 0 :(得分:1)

同样,我不清楚您要达到的目标。如果要创建的是用户创建的虚线(可以随鼠标按下/拖动/释放而改变的虚线),则无需创建新组件,而可以将MouseAdapter用作MouseListener和MouseMotionListener,所有这些帮助您创建Rectangle,然后根据this answer使用适当的Stroke用虚线绘制Rectangle。

例如,类似的操作将创建用户可选择的虚线:

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Stroke;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.*;

public class SelectorPanel extends JPanel {
    private static final int PREF_W = 800;
    private static final int PREF_H = 650;
    private static final Stroke DASHED_STROKE = new BasicStroke(2, BasicStroke.CAP_BUTT,
            BasicStroke.JOIN_BEVEL, 0, new float[] { 5 }, 0);
    private static final Color DASHED_COLOR = Color.LIGHT_GRAY;
    private Rectangle rectangle = null;

    public SelectorPanel() {
        MyMouse myMouse = new MyMouse();
        addMouseListener(myMouse);
        addMouseMotionListener(myMouse);
    }

    private class MyMouse extends MouseAdapter {
        private Point p1 = null;

        @Override
        public void mousePressed(MouseEvent e) {
            p1 = e.getPoint();
            rectangle = null;
        }

        @Override
        public void mouseDragged(MouseEvent e) {
            if (p1 != null) {
                createRectangle(e);
            }
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            if (p1 != null) {
                createRectangle(e);
                p1 = null;
            }
        }

        private void createRectangle(MouseEvent e) {
            Point p2 = e.getPoint();
            int x = Math.min(p1.x, p2.x);
            int y = Math.min(p1.y, p2.y);
            int width = Math.abs(p1.x - p2.x);
            int height = Math.abs(p1.y - p2.y);
            rectangle = new Rectangle(x, y, width, height);
            repaint();
        }

    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (rectangle != null) {
            Graphics2D g2 = (Graphics2D) g.create();
            g2.setColor(DASHED_COLOR);
            g2.setStroke(DASHED_STROKE);
            g2.draw(rectangle);
            g2.dispose();
        }
    }

    @Override
    public Dimension getPreferredSize() {
        if (isPreferredSizeSet()) {
            return super.getPreferredSize();
        }
        return new Dimension(PREF_W, PREF_H);
    }

    private static void createAndShowGui() {
        SelectorPanel mainPanel = new SelectorPanel();

        JFrame frame = new JFrame("SelectorPanel");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}