gpt4 book ai didi

java - 通过子级向父级添加 JComponent

转载 作者:行者123 更新时间:2023-12-02 09:46:59 26 4
gpt4 key购买 nike

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();
}
}

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

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

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

最佳答案

再说一遍,我并不是 100% 清楚您想要实现的目标。如果您希望创建的是用户创建的虚线,可以通过鼠标按下/拖动/释放来更改,那么您不需要创建新组件,而是使用 MouseAdapter 作为 MouseListener 和 MouseMotionListener,所有这些都可以帮助您创建矩形,然后简单地使用适当的描边用虚线绘制矩形,如 this answer

例如,类似的内容会创建一条用户可选择的虚线:

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());
}
}

关于java - 通过子级向父级添加 JComponent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56572567/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com