读书人

大家给小弟我看看有关问题在哪儿啊

发布时间: 2012-01-02 22:40:04 作者: rapoo

大家给我看看问题在哪儿啊?
public static void main(String[] args)
{
final JPanel panel1= new JPanel();
panel1.setLayout(new FlowLayout());
JButton b1 = new JButton( "画树 ");
panel1.add(b1);
b1.addActionListener(new ActionListener()
{
BitTree theTree = new BitTree();
public void actionPerformed(ActionEvent e)
{

theTree.root = theTree.setup(1,320,160);
Node2 node = theTree.root;
String tree_ch = String.valueOf(node.ch);
int circle_x = node.x;
int circle_y = node.y;
int line_x = circle_x + 15;
int line_y = circle_y + 40;

Graphics d_circle = panel1.getGraphics();
Graphics d_string = panel1.getGraphics();
Graphics d_line = panel1.getGraphics();
d_circle.setColor(Color.white);
d_circle.fillOval(circle_x,circle_y,40,40);
d_string.setColor(Color.red);
d_string.drawString(tree_ch,circle_x + 15,circle_y + 20);
d_line.drawLine(line_x,line_y,185,100);

}
});
JFrame test= new cooky_jframe();
test.setSize(640,480);
test.setContentPane(panel1);
test.setVisible(true);
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

这里是画树的一个节点。。。不过为什么我画的节点都是创建树时最后创建的那个节点????????

下面是创建树部分
public static Node2 root;
public final char[] treeLine = { 'a ', 'b ', '# '};
//用于标志二叉树节点在数组中的存储位置,以便在创建二叉树时能够找到节点对应的数据。
static int index = 0;
public Node2 setup(int h,int t,int w)
{

if (index > = treeLine.length)
return root = null;
if (treeLine[index] == '# ')
return root = null;
if (treeLine[index] == ' ')
return root = null;

root = new Node2(treeLine[index],t,h*50);

index = index * 2 + 1;

//System.out.println(h);
///System.out.println(t);
//System.out.println(w);
root.left = setup(h+1,t-w,w/2);


index ++;

root.right = setup(h+1,t+w,w/2);
index = index / 2 - 1;

return root;
}

这个是节点的class
class Node2
{
static char ch;
static Node2 left;
static Node2 right;
static int x;
static int y;

//构造函数
public Node2(char c,int x1,int y1)
{
this.ch = c;
this.left = null;
this.right = null;
this.x = x1;
this.y = y1;
}
}

我这里画第一个节点应该输出的是a 为什么我输出这里是最后一个b????


[解决办法]
码太长,分太少
[解决办法]
感觉public Node2 setup(int h,int t,int w) 里边不应该修改成员变量root,而应该重新定义一个Node2型变量最后返回.

读书人网 >J2SE开发

热点推荐