我的Jython GUI出现问题。我需要显示文本字段。我相信我遇到了布局的问题,但我不确定如何在Jython中创建一个新的FlowLayout。
#!/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()
答案 0 :(得分: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()