我以前没有使用过 Swing,所以我在看教程:
http ://www.zetcode.com/gui/jythonswing/layout/
在底部,有一个使用 GroupLayout 创建简单布局的示例,该布局可随窗口调整大小。我基本上复制了这个示例并放入了我自己的控件和布局。但是,当我执行代码时,所有控件都集中在窗口的左上角,根本不会移动或调整大小。
我错过了什么?
代码:
#!/usr/local/bin/jython
# -*- coding: utf-8 -*-
from java.awt import Dimension
from java.awt import Color
from javax.swing import JButton,JList
from javax.swing import SwingConstants
from javax.swing import JFrame
from javax.swing import JLabel
from javax.swing import JTextArea
from javax.swing import BorderFactory
from javax.swing import GroupLayout
class Example(JFrame):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
layout = GroupLayout(self.getContentPane())
self.getContentPane().setLayout(layout)
layout.setAutoCreateGaps(True)
layout.setAutoCreateContainerGaps(True)
self.setPreferredSize(Dimension(350, 300))
self.data = ("one","two","three","four")
flist = JList(self.data)
edit = JButton("Edit")
grab = JButton("Grab")
status = JLabel("status")
layout.setHorizontalGroup(layout.createParallelGroup()
.addGroup(layout.createSequentialGroup()
.addComponent(flist)
.addGroup(layout.createParallelGroup()
.addComponent(edit)
.addComponent(grab)
)
)
.addComponent(status)
)
layout.setVerticalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup()
.addComponent(flist)
.addGroup(layout.createSequentialGroup()
.addComponent(edit)
.addComponent(grab)
)
)
.addComponent(status)
)
layout.linkSize(SwingConstants.HORIZONTAL, [edit, grab])
self.pack()
self.setTitle("Windows")
self.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
self.setLocationRelativeTo(None)
self.setVisible(True)
if __name__ == '__main__':
Example()