Reputation: 39
I am having a problem with my Jython GUI. I need to make the text field appear. I believe I am running into a problem with the Layout, but I am unsure about how to create a new FlowLayout in Jython.
#!/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()
Upvotes: 1
Views: 452
Reputation: 2383
I added the changes with a comment above starting with CHANGED. Your code works with minor modifications.
1) Do use layouts, don't set the layout to None, no absolute layout manager, etc.
2) Add components to the main content pane container before you pack the frame. If not, you'll need to revalidate the container among other things (with a possible repaint after the frame is visible).
#!/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()
Upvotes: 3