Java Swing组件和容器

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:

一个容器容纳一组组件。 它提供了一个可以管理和显示组件的空间。 容器有两种类型:

  1. 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();
      }
    }
    swing button example

    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();
      }
    }
    jtextfield example

    JCheckBox (JCheckBox)

    The JcheckBox class is used to create chekbox in swing framework.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值