gpt4 book ai didi

java - 组件未显示在面板上

转载 作者:行者123 更新时间:2023-11-29 05:31:47 24 4
gpt4 key购买 nike

我自己的组件没有显示在面板上。

import rysujdrzewo.nodeLabel.NodeLabel;
import rysujdrzewo.nodeLabel.PointOfContact;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import javax.swing.JComponent;

public class PaintLinesBetweenNodes extends JComponent{
private final NodeLabel oNode1;
private final NodeLabel oNode2;
private final PointOfContact oPointForNode1;
private final PointOfContact oPointForNode2;
private final String sEdgeName;

public PaintLinesBetweenNodes(NodeLabel oNode1,NodeLabel oNode2, PointOfContact oPointForNode1, PointOfContact oPointForNode2, String sEdgeName){
super();
this.oNode1 = oNode1;
this.oNode2 = oNode2;
this.oPointForNode1 = oPointForNode1;
this.oPointForNode2 = oPointForNode2;
this.sEdgeName = sEdgeName;
}

private void paintNodeConnections(Graphics2D oGraphics) {
Point p1 = oNode1.getConnectionPoint(oPointForNode1);
Point p2 = oNode2.getConnectionPoint(oPointForNode2);

oGraphics.drawLine(p1.x, p1.y, p2.x, p2.y);
oGraphics.drawString(sEdgeName, (float)(p1.x + p2.x) / (float)2, (float)(p1.y + p2.y) / (float)2);
}

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
paintNodeConnections(g2d);
}
}

这个显示正确:

import java.awt.Point;
import java.awt.event.ComponentEvent;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JLabel;

public class NodeLabel extends JLabel{
Map<PointOfContact, Point> connectionPoints = new HashMap<>();

public NodeLabel(String text) {
super(text);
addComponentListener(new java.awt.event.ComponentAdapter(){
@Override
public void componentResized(java.awt.event.ComponentEvent evt) {
mapConnectionPoints();
}
@Override
public void componentMoved(ComponentEvent e) {
mapConnectionPoints();
}

});
}

// updates the mapped positions of the connection points
// called whenever the component get's resized or moved
private void mapConnectionPoints(){
connectionPoints.clear();
Point point = new Point(getX(),getY()+getHeight()/2);
connectionPoints.put(PointOfContact.LEFT, point);

point = new Point(getX() + getWidth(), getY() + getHeight()/2);
connectionPoints.put(PointOfContact.RIGHT, point);

point = new Point(getX() + getWidth()/2, getY());
connectionPoints.put(PointOfContact.TOP, point);

point = new Point(getX() + getWidth()/2, getY() + getHeight());
connectionPoints.put(PointOfContact.BOTTOM, point);
}

public Point getConnectionPoint(PointOfContact key) {
return connectionPoints.get(key);
}
}

和执行:

import java.awt.Color;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import rysujdrzewo.nodeLabel.NodeLabel;
import rysujdrzewo.nodeLabel.PointOfContact;


public class PaintTree extends JFrame{

public PaintTree() {
initComponents();
}

private void initComponents() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setMinimumSize(new Dimension(800, 800));
JPanel oJPanel = new JPanel();
oJPanel.setLayout(null);

NodeLabel oNode1 = new NodeLabel("Node 1");

oNode1.setHorizontalAlignment(SwingConstants.CENTER);
oNode1.setBorder(BorderFactory.createLineBorder(new Color(0, 0, 0)));
NodeLabel oNode2 = new NodeLabel("Node 2");

oNode2.setHorizontalAlignment(SwingConstants.CENTER);
oNode2.setBorder(BorderFactory.createLineBorder(new Color(0, 0, 0)));

NodeLabel oNode3 = new NodeLabel("Node 3");

oNode3.setHorizontalAlignment(SwingConstants.CENTER);
oNode3.setBorder(BorderFactory.createLineBorder(new Color(0, 0, 0)));

oJPanel.add(oNode1);
oJPanel.add(oNode2);
oJPanel.add(oNode3);
PaintLinesBetweenNodes o1 = new PaintLinesBetweenNodes(oNode1, oNode2, PointOfContact.BOTTOM, PointOfContact.TOP, "Czesc1");
PaintLinesBetweenNodes o2 = new PaintLinesBetweenNodes(oNode1, oNode3, PointOfContact.BOTTOM, PointOfContact.TOP, "Czesc2");

oJPanel.add(o1);
oJPanel.add(o2);

getContentPane().add(oJPanel);
oNode1.setBounds(100, 52, 56, 28);
oNode2.setBounds(54, 200, 56, 28);
oNode3.setBounds(150, 200, 56, 28);

pack();
}
}

我不知道这段代码有什么问题。我不使用任何布局,因为我尝试创建树数据(决策树)的一些图形表示。

最佳答案

基本上,您永远不会设置 PaintLinesBetweenNodes 实例的大小,这意味着它们的初始大小将为 0x0

这是使用 null 布局的问题之一,您需要负责调整组件的位置

我还认为您会发现 pack 不会按照您期望的方式工作,因为它依赖于布局管理器提供的信息

关于java - 组件未显示在面板上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20828564/

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