我已经定义了一个名为LogicalNode的类,它可以用来形成一个树。我想显示整个树,但我不知道如何设置每个节点的位置。在程序中,我可以在父节点下插入一个树节点,所以当我插入一些节点时,它会导致节点之间的冲突。 这是我的班级:
public class LogicalNode implements Serializable{
private String text;
private String logicalType;
private LogicalNode parent;
private LinkedList<LogicalNode> sons = new LinkedList<>();
private List<LogicalNode> tmpNodes = new ArrayList<>();
public LogicalNode(){}
public LogicalNode(String text,String logicalType,LogicalNode parent){
this.text = text;
this.logicalType = logicalType;
this.parent = parent;
}
public void addSon(LogicalNode node){
this.sons.offer(node);
this.tmpNodes.add(node);
}
public int getOrder(){
LogicalNode parent = this.getParent();
if(parent == null){
return 0;
}else{
return parent.getSons().indexOf(this);
}
}
public int getLevel(){
LogicalNode node = this;
int level = 0;
while(node.getParent() != null){
node = node.getParent();
level++;
}
return level;
}
@Override
public String toString() {
return this.text;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getLogicalType() {
return logicalType;
}
public void setLogicalType(String logicalType) {
this.logicalType = logicalType;
}
public LogicalNode getParent() {
return parent;
}
public void setParent(LogicalNode parent) {
this.parent = parent;
}
public LinkedList<LogicalNode> getSons() {
return sons;
}
public void setSons(LinkedList<LogicalNode> sons) {
this.sons = sons;
}
public List<LogicalNode> getTmpNodes() {
return tmpNodes;
}
public void setTmpNodes(List<LogicalNode> tmpNodes) {
this.tmpNodes = tmpNodes;
}
}
此类可以用作某些GUI组件的用户Object来显示它,例如JGraphX中的mxCell。