【发布时间】:2012-04-13 19:03:36
【问题描述】:
我设计了 java 桌面应用程序 当我按下按钮时在那个应用程序中 显示了另一个绘制树的 Jframe 但是当我关闭 Jframe 时,整个操作就关闭了 但我只想关闭那个 Jfarme 我该怎么办? 这是jframe代码:
public class DrawTree extends JFrame{
public int XDIM, YDIM;
public Graphics display;
@Override
public void paint(Graphics g) {} // override method
// constructor sets window dimensions
public DrawTree(int x, int y)
{
XDIM = x; YDIM = y;
this.setBounds(0,0,XDIM,YDIM);
this.setVisible(false);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
display = this.getGraphics();
// draw static background as a black rectangle
display.setColor(Color.black);
display.fillRect(0,0,x,y);
display.setColor(Color.red);
try{Thread.sleep(500);} catch(Exception e) {} // Synch with system
} // drawingwindow
public static int depth(BinaryNode N) // find max depth of tree
{
if (N==null) return 0;
int l = depth(N.left);
int r = depth(N.right);
if (l>r) return l+1; else return r+1;
}
// internal vars used by drawtree routines:
private int bheight = 50; // branch height
private int yoff = 30; // static y-offset
// l is level, lb,rb are the bounds (position of left and right child)
private void drawnode(BinaryNode N,int l, int lb, int rb)
{
if (N==null) return;
try{Thread.sleep(100);} catch(Exception e) {} // slow down
display.setColor(Color.green);
display.fillOval(((lb+rb)/2)-10,yoff+(l*bheight),20,20);
display.setColor(Color.red);
display.drawString(N.element+"",((lb+rb)/2)-5,yoff+15+(l*bheight));
display.setColor(Color.blue); // draw branches
if (N.left!=null)
{
display.drawLine((lb+rb)/2,yoff+10+(l*bheight),((3*lb+rb)/4),yoff+(l*bheight+bheight));
drawnode(N.left,l+1,lb,(lb+rb)/2);
}
if (N.right!=null)
{
display.drawLine((lb+rb)/2,yoff+10+(l*bheight),((3*rb+lb)/4),yoff+(l*bheight+bheight));
drawnode(N.right,l+1,(lb+rb)/2,rb);
}
} // drawnode
public void drawtree(BinaryNode T)
{
if (T==null) return;
int d = depth(T);
bheight = (YDIM/d);
display.setColor(Color.white);
display.fillRect(0,0,XDIM,YDIM); // clear background
drawnode(T,0,0,XDIM);
}}
还有一个问题
当我从树类中新建一个对象时,我想在所有按钮代码中访问该对象 那么我应该在哪里定义或者更好地说,我应该如何定义可以在我的所有代码中访问的对象??
【问题讨论】:
-
你能给我们展示一下首先显示JFrame的代码吗?
-
1) "another Jframe is shown" 见The Use of Multiple JFrames, Good/Bad Practice? 2) 请找到你的 shift 键并在每个句子的开头应用它,对于单词 I 和 class JFrame 之类的名称。尝试和阅读这种混乱是痛苦的。 3)该代码似乎与问题无关。 4) "and another question" 那是..another question 的话题。请不要试图将 2 个问题合二为一。