2013-06-06 74 views
1

我的Jython GUI有問題。我需要讓文本字段出現。我相信我遇到了佈局問題,但我不確定如何在Jython中創建新的FlowLayout。Jython GUI的問題

#!/usr/bin/env jython 

# Created by Joe Castleberry 
# 2013 

# Imports 
from javax.swing import * 
from java.awt import * 

class Window: 
    def __init__(self): 
     # Global variables 
     global frame 
     global label 
     global container 
     global text 

     # Definition of global variables 
     frame = JFrame("Joe's first Java program") 
     label = JLabel("Hello World!", JLabel.CENTER) 
     container = JPanel() 
     text = JTextField(10) 

    def builder(self): 
     # Building container 
     container.setLayout(None) 
     container.setBackground(Color.BLACK) 

     # Building frame 
     frame.getContentPane().add(container)  # Adding Container to JFrame 
     frame.setSize(300,300) 
     frame.setVisible(True) 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) 

     # Adding components to container 
     container.add(text) 



a = Window() 
a.builder() 

回答

3

我加入上面CHANGED開始註釋的變化。您的代碼只需稍作修改即可使用

1)不要使用佈局,沒有佈局設置爲,沒有絕對的佈局管理器等

2)組件添加到主內容窗格容器你壓縮幀之前。 如果不是,您將需要重新驗證容器等(可能重新繪製後框架可見)。

#!/usr/bin/env jython 

# Created by Joe Castleberry 
# 2013 

# Imports 
from javax.swing import * 
from java.awt import * 

class Window: 
    def __init__(self): 
     # Global variables 
     global frame 
     global label 
     global container 
     global text 

     # Definition of global variables 
     frame = JFrame("Joe's first Java program") 
     label = JLabel("Hello World!", JLabel.CENTER) 
     container = JPanel() 
     text = JTextField(10) 

    def builder(self): 
     # Building container 
     # CHANGED Removed container.setLayout(None) 
     container.setBackground(Color.BLACK) 
     # CHANGED Move the add text call here 
     container.add(text) 

     # Building frame 
     frame.getContentPane().add(container) 
     frame.setSize(300,300) 
     frame.setVisible(True) 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) 



a = Window() 
a.builder() 
+0

太棒了!謝謝。 –