📜  Jython-使用Swing GUI库

📅  最后修改于: 2020-11-08 07:18:44             🧑  作者: Mango


Jython的主要功能之一是它能够在JDK中使用Swing GUI库。标准Python发行版(通常称为CPython)附带了Tkinter GUI库。其他GUI库(例如PyQtWxPython)也可以与它一起使用,但是swing库提供了独立于平台的GUI工具箱。

与在Java中使用它相比,在Jython中使用swing库要容易得多。在Java中,必须使用匿名类创建事件绑定。在Jython中,我们可以简单地出于相同目的传递函数。

基本的顶层窗口是通过声明JFrame类的对象并将其visible属性设置为true来创建的。为此,需要从swing包中导入Jframe类。

from javax.swing import JFrame

JFrame类具有多个构造函数,这些构造函数具有不同数量的参数。我们将使用一个,它将字符串作为参数并将其设置为标题。

frame = JFrame(“Hello”)

在将框架的visible属性设置为true之前,请设置框架的大小和位置属性。将以下代码存储为frame.py

from javax.swing import JFrame

frame = JFrame("Hello")
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
frame.setLocation(100,100)
frame.setSize(300,200)
frame.setVisible(True)

从命令提示符处运行以上脚本。它将显示以下显示窗口的输出。

窗口

swing GUI库以Java中javax.swing包的形式提供。它的主要容器类JFrameJDialog分别从AWT库中的Frame和Dialog类派生。其他的GUI控件(如JLabel,JButton,JTextField等)均从JComponent类派生。

下图显示了Swing包类层次结构。

Swing包类层次结构

下表总结了秋千库中的不同GUI控件类-

Sr.No. Class & Description
1

JLabel

A JLabel object is a component for placing text in a container.

2

JButton

This class creates a labeled button.

3

JColorChooser

A JColorChooser provides a pane of controls designed to allow a user to manipulate and select a color.

4

JCheckBox

A JCheckBox is a graphical component that can be in either an on (true) or off (false) state.

5

JRadioButton

The JRadioButton class is a graphical component that can be either in an on (true) or off (false) state. in a group.

6

JList

A JList component presents the user with a scrolling list of text items.

7

JComboBox

A JComboBox component presents the user with drop down list of items

8

JTextField

A JTextField object is a text component that allows for the editing of a single line of text.

9

JPasswordField

A JPasswordField object is a text component specialized for password entry.

10

JTextArea

A JTextArea object is a text component that allows editing of a multiple lines of text.

11

ImageIcon

A ImageIcon control is an implementation of the Icon interface that paints Icons from Images

12

JScrollbar

A Scrollbar control represents a scroll bar component in order to enable the user to select from range of values.

13

JOptionPane

JOptionPane provides set of standard dialog boxes that prompt users for a value or informs them of something.

14

JFileChooser

A JFileChooser control represents a dialog window from which the user can select a file.

15

JProgressBar

As the task progresses towards completion, the progress bar displays the task’s percentage of completion.

16

JSlider

A JSlider lets the user graphically select a value by sliding a knob within a bounded interval.

17

JSpinner

A JSpinner is a single line input field that lets the user select a number or an object value from an ordered sequence.

在后续示例中,我们将使用其中一些控件。