为什么这段代码里awt.canvas里没有画出东西来?

这是一段Jython代码,也就是使用Python语言,但是可以调用Java的库,需要下载jython2.7.

运行程序:

PS D:\jython2.7> java -jar .\jython.jar ..\my-gui-browser\G\G.py

import java
from java import awt

def m(e):
    print("m called!\n")
    
def k(e):
    print("k called!\n")
    
w1 = [ 
    [1, "Frame", {"id":"w"}],
    [2, "Panel", {"mouse":m, "keyboard":k, "width":"","height":""}],  
    [3, "Text", {}]
]

w2 = [
    [1, "Frame", {"id":"wa"}],
    [2, "Panel", {"mouse":m, "keyboard":k, "width":"","height":""}], 
    [3, "Text", {}]
]

class MyCanvas(awt.Canvas):
    def paint(g):
        g.setColor(Color.RED)
        g.drawLine(0, 0, 400, 800)
        g.drawString("QI Dian", 70, 115)

class Element:
    def __init__(self, father, brother, child, l):
        self.level = l[0]
        self.name = l[1]
        self.d = l[2]
        self.father = father
        self.child = child
        self.brother = brother
    
class Tree:
    def __init__(self, w):
        if w[0][1] != "Frame" or w[0][0] != 1:
            print("Level 1 Error\n")
            return
        else:
            self.head = Element(None, None, None, w[0])
        tmp = self.head
        for i in w[1:]:        
            if i[0] == tmp.level + 1:
                tmp.child = Element(tmp, None, None, i);
                tmp = tmp.child    
            while i[0] < tmp.level:
                tmp = tmp.father
            if tmp == None:
                print("Level < 1, it's wrong!\n")                
                return
            if i[0] == tmp.level:
                tmp.brother = Element(tmp.father, None, None, i);
                tmp = tmp.brother
                
    def draw_tree(self):
        pass
        
    def draw_element(self, e):
        pass
        
    def draw_window(self, n):
        tmp = self.head;
        while n > 0:
            tmp = tmp.brother;
            n = n - 1
        if tmp.name == "Frame":
            frame = awt.Frame('AWT Example', visible=1)
            
            p = awt.Panel()
            p.setBackground(awt.Color.GREEN)      

            l = awt.Label("This is test Label");
            p.add(l)
            
            button = awt.Button('Close Me!', actionPerformed=exit)
            p.add(button, 'Center')
            
            canvas = MyCanvas()
            p.add(canvas)
     
            frame.add(p)            
            
            i = awt.MouseInfo.getPointerInfo().getLocation();
            
            frame.pack()
            print(i)
        if tmp.child != None:
            pass
            #draw_element(tmp.child)

t = Tree(w1)
t.draw_window(0)

def exit(e): java.lang.System.exit(0)

窗口时出来,panel\label\button都正常 ,但是canvas里的内容没有画出来,为什么呢?哪里出错了?

阅读 525
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题