A component is an independent visual control and Java Swing Framework contains a large set of these components which provide rich functionalities and allow high level of customization. They all are derived from JComponent class. All these components are lightweight components. This class provides some common functionality like pluggable look and feel, support for accessibility, drag and drop, layout, etc.
组件是一个独立的可视控件,Java Swing框架包含大量的这些组件,这些组件提供了丰富的功能并允许进行高级别的自定义。 它们都是从JComponent类派生的。 所有这些组件都是轻量级组件。 此类提供一些常用功能,例如可插拔的外观,对辅助功能的支持,拖放,布局等。
A container holds a group of components. It provides a space where a component can be managed and displayed. Containers are of two types:
一个容器容纳一组组件。 它提供了一个可以管理和显示组件的空间。 容器有两种类型:
Top level Containers
顶级容器
It inherits Component and Container of AWT.
它继承了AWT的组件和容器。
It cannot be contained within other containers.
它不能包含在其他容器中。
Heavyweight.
重量级的。
Example: JFrame, JDialog, JApplet
示例:JFrame,JDialog,JApplet
Lightweight Containers
轻型集装箱
- It inherits JComponent class.
- It is a general purpose container.
- It can be used to organize related components together.
- Example: JPanel
摇摆JButton (Swing JButton)
JButton类提供按钮的功能。 它用于创建按钮组件。 JButton类具有三个构造函数,
JButton的例子
(JButton class provides functionality of a button. It is used to create button component. JButton class has three constuctors,Example of JButton
)In this example, we are creating two buttons using Jbutton class and adding them into Jframe container.
在此示例中,我们使用Jbutton类创建两个按钮,并将它们添加到Jframe容器中。
import javax.swing.*; import java.awt.event.*; import java.awt.*; public class testswing extends JFrame { testswing() { JButton bt1 = new JButton("Yes"); //Creating a Yes Button. JButton bt2 = new JButton("No"); //Creating a No Button. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) //setting close operation. setLayout(new FlowLayout()); //setting layout using FlowLayout object setSize(400, 400); //setting size of Jframe add(bt1); //adding Yes button to frame. add(bt2); //adding No button to frame. setVisible(true); } public static void main(String[] args) { new testswing(); } }
JTextField (JTextField)
JTextField is used for taking input of single line of text. It is most widely used text component. It has three constructors,
JTextField用于接受单行文本的输入。 它是使用最广泛的文本组件。 它具有三个构造函数,
JTextField(int cols) JTextField(String str, int cols) JTextField(String str)
cols represent the number of columns in text field.
cols表示文本字段中的列数。
使用JTextField的示例 (Example using JTextField)
In this example, we are creating text field using JtextField class and adding into the jframe container.
在此示例中,我们使用JtextField类创建文本字段并将其添加到jframe容器中。
import javax.swing.*; import java.awt.event.*; import java.awt.*; public class MyTextField extends JFrame { public MyTextField() { JTextField jtf = new JTextField(20); //creating JTextField. add(jtf); //adding JTextField to frame. setLayout(new FlowLayout()); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(400, 400); setVisible(true); } public static void main(String[] args) { new MyTextField(); } }
JCheckBox (JCheckBox)
The JcheckBox class is used to create chekbox in swing framework.