我自己的组件未在面板上显示。
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();
}
}
我不知道这段代码有什么问题。我不使用任何布局,因为我尝试创建树数据的图形表示(决策树)。
答案 0 :(得分:3)
基本上,您从未设置PaintLinesBetweenNodes
实例的大小,这意味着它们的初始大小为0x0
这是使用null
布局的问题之一,您负责调整组件的位置
我也认为你会发现pack
不会按预期的方式工作,因为它依赖于布局管理员提供的信息