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

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



javadoc组件 java组件容器_swing

(JTextField)

JTextField

JTextField用于接受单行文本的输入。 它是使用最广泛的文本组件。 它具有三个构造函数,

JTextField(int cols)
JTextField(String str, int cols)
JTextField(String str)

cols

cols表示文本字段中的列数。

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


javadoc组件 java组件容器_android_02

(JCheckBox)

The JcheckBox class is used to create chekbox in swing framework. In this example, we are creating three checkboxes to get user response.

JcheckBox类用于在swing框架中创建chekbox。 在此示例中,我们将创建三个复选框以获取用户响应。

JCheckBox(String str)
  • using JCheckBox
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Test extends JFrame
{
  public Test()
  {
    JCheckBox jcb = new JCheckBox("yes");   //creating JCheckBox.
    add(jcb);                               //adding JCheckBox to frame.
    jcb = new JCheckBox("no");              //creating JCheckBox.
    add(jcb);                               //adding JCheckBox to frame.
    jcb = new JCheckBox("maybe");           //creating JCheckBox.
    add(jcb);                               //adding JCheckBox to frame.
    setLayout(new FlowLayout());
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(400, 400);
    setVisible(true);
  }
  public static void main(String[] args)
  {
    new Test();
  }
}


javadoc组件 java组件容器_java_03

(JRadioButton)

Radio button is a group of related button in which only one can be selected. JRadioButton class is used to create a radio button in Frames. Following is the constructor for JRadioButton,

单选按钮是一组相关的按钮,其中只能选择一个。 JRadioButton类用于在Frames中创建单选按钮。 以下是JRadioButton的构造函数,

JRadioButton(String str)

(Example using JRadioButton)

To create radio button in swing, we used jradiobutton class. It is used to get single user response at a time.

为了创建单选按钮,我们使用了jradiobutton类。 它用于一次获得单个用户的响应。

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Testextends JFrame
{
 public Test()
 {
  JRadioButton jcb = new JRadioButton("A");	//creating JRadioButton.
  add(jcb);					//adding JRadioButton to frame.
  jcb = new JRadioButton("B");			//creating JRadioButton.
  add(jcb);					//adding JRadioButton to frame.
  jcb = new JRadioButton("C");			//creating JRadioButton.
  add(jcb);					//adding JRadioButton to frame.
  jcb = new JRadioButton("none");
  add(jcb);
  setLayout(new FlowLayout());
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  setSize(400, 400);
  setVisible(true);
 }
 public static void main(String[] args)
 {
  new Test();
 }
}


javadoc组件 java组件容器_java_04

(JComboBox)

Combo box is a combination of text fields and drop-down list.JComboBox

JComboBox组件用于在Swing中创建一个组合框。 以下是JComboBox的构造函数,

JComboBox(String arr[])

(Example using JComboBox)

Lets create an example to add combobox to the jframe . Combo box is used to create a drop-down menu. See the below example.

让我们创建一个示例,将combobox添加到jframe。 组合框用于创建下拉菜单。 请参见以下示例。

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Testextends JFrame
{
 String name[] = {"Abhi","Adam","Alex","Ashkay"};  //list of name.
 public Test()
 {
  JComboBox jc = new JComboBox(name);	//initialzing combo box with list of name.
  add(jc);				//adding JComboBox to frame.
  setLayout(new FlowLayout());
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  setSize(400, 400);
  setVisible(true);
 }
 public static void main(String[] args)
 {
  new Test();
 }
}


javadoc组件 java组件容器_swing_05

(A program to change background color of a frame (Using Action Event))

import java.awt.*;  //importing awt package
import javax.swing.*;  //importing swing package
import java.awt.event.*;  //importing event package

//For an event to occur upon clicking the button, ActionListener interface should be implemented
class StColor extends JFrame implements ActionListener{

JFrame frame;
JPanel panel;
JButton b1,b2,b3,b4,b5;

StColor(){

   frame = new JFrame("COLORS");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

   panel = new JPanel();  //Creating a panel which is a container and will hold all the buttons
   panel.setSize(100, 50);

   b1 = new JButton("BLUE");  //Creating a button named BLUE
   b1.addActionListener(this);  //Registering the button with the listener

   b2 = new JButton("RED");  //Creating a button named RED
   b2.addActionListener(this);  //Registering the button with the listener

   b3 = new JButton("CYAN");//Creating a button named CYAN
   b3.addActionListener(this);//Registering the button with the listener

   b4 = new JButton("PINK");  //Creating a button named PINK
   b4.addActionListener(this);  //Registering the button with the listener

   b5 = new JButton("MAGENTA");  //Creating a button named MAGENTA
   b5.addActionListener(this);  //Registering the button with the listener

   //Adding buttons to the Panel
   panel.add(b1);
   panel.add(b2);
   panel.add(b3);
   panel.add(b4);
   panel.add(b5);

   frame.getContentPane().add(panel);  //adding panel to the frame
   frame.setSize(500,300);
   frame.setVisible(true);
   frame.setLayout(new FlowLayout());

}
//The below method is called whenever a button is clicked
    @Override
    public void actionPerformed(ActionEvent e) {

        //This method returns an object of the button on which the Event-
        Pressing of button initially occurred
        Object see = e.getSource();

        if(see ==(b1)){  //Checking if the object returned is of button1
        frame.getContentPane().setBackground(java.awt.Color.blue);  //changing the panel color to blue
        }
        if(see == b2){  //Checking if the object returned is of button2
            frame.getContentPane().setBackground(java.awt.Color.red);  //changing the panel color to red
        }
        if(see == b3){  //Checking if the object returned is of button3
        frame.getContentPane().setBackground(java.awt.Color.cyan);//changing the panel color to cyan
        }
        if(see == b4){  //Checking if the object returned is of button4
            frame.getContentPane().setBackground(java.awt.Color.pink);  //changing the panel color to pink
        }
        if(see == b5){  //Checking if the object returned is of button5
        frame.getContentPane().setBackground(java.awt.Color.magenta);  //changing the panel color to magenta
        }
    }
}

class Test {
    public static void main(String[] args) {
        StColor o = new StColor();
    }
}

(Output:)


javadoc组件 java组件容器_android_06

(JLabel)

In Java, Swingtoolkit contains a JLabel Class. It is under package javax.swing.JLabel class. It is used for placing text in a box. Only Single line text is allowed and the text can not be changed directly.

在Java中,Swingtoolkit包含一个JLabel类。 它在包javax.swing.JLabel类下。 用于将文本放置在框中。 仅允许使用单行文本,并且不能直接更改文本。

(Declaration )

public class JLabel extends JComponent implements SwingConstants, Accessible

公共类JLabel扩展JComponent实现SwingConstants,可访问

(The JLabel Contains 4 constructors. They are as following:)

1. JLabel()

1. JLabel()

2. JLabel(String s)

2. JLabel(String s)

3. JLabel(Icon i)

3. JLabel(Icon i)

4. JLabel(String s, Icon i, int horizontalAlignment)

4. JLabel(String s,Icon i,int horizontalAlignment)

(Example:)

import javax.swing.*;  
class SLabelDemo1  
{  
public static void main(String args[])  
{  
    JFrame label_f= new JFrame("studytonight ==> Label Demo");  
    JLabel label_l1,label_l2;  
    label_l1=new JLabel("Welcome to studytonight.com");  
    label_l1.setBounds(50,50, 200,30);  
    label_l2=new JLabel("How are You?");  
    label_l2.setBounds(50,100, 200,30);  
    label_f.add(label_l1); 
    label_f.add(label_l2);  
    label_f.setSize(300,300);  
    label_f.setLayout(null);  
    label_f.setVisible(true);  
    }  
}


javadoc组件 java组件容器_java_07

javadoc组件 java组件容器_swing_08

(JTextArea)

In Java, Swing toolkit contains a JTextArea Class. It is under package javax.swing.JTextArea class. It is used for displaying multiple-line text.

在Java中,Swing工具箱包含一个JTextArea类。 它在包javax.swing.JTextArea类下。 用于显示多行文本。

(Declaration)

public class JTextArea extends JTextComponent

公共类JTextArea扩展了JTextComponent

(The JTextArea Contains 4 constructors. They are as following:)

1. JTextArea()

1. JTextArea()

2. JTextArea(String s)

2. JTextArea(String s)

3. JTextArea(int row, int column)

3. JTextArea(int行,int列)

4. JTextArea(String s, int row, int column)

4. JTextArea(String s,int row,int column)

(Example:)

Lets take an example to create text area in swing. We are using JtextArea class to create text area and adding to Jframe container.

让我们举个例子来创建摆动的文本区域。 我们使用JtextArea类创建文本区域并添加到Jframe容器中。

import javax.swing.*;  
public class STextAreaDemo1  
{  
    STextAreaDemo1()
  {  
        JFrame textArea_f= new JFrame();  
        JTextArea textArea_area=new JTextArea("Welcome to studytonight.com ");  
        textArea_area.setBounds(10,30, 200,200);  
        textArea_f.add(textArea_area);  
        textArea_f.setSize(400,400);  
        textArea_f.setLayout(null);  
        textArea_f.setVisible(true);  
        }  
public static void main(String args[])  
    {  
   new STextAreaDemo1();  
}
}


javadoc组件 java组件容器_spring_09

javadoc组件 java组件容器_javadoc组件_10

(JPasswordField)

In Java, Swing toolkit contains a JPasswordField Class. It is under package javax.swing.JPasswordField class. It is specifically used for password and it can be edited.

在Java中,Swing工具箱包含JPasswordField类。 它在包javax.swing.JPasswordField类下。 它专门用于密码,可以进行编辑。

(Declaration)

public class JPasswordField extends JTextField

公共类JPasswordField扩展JTextField

(The JPasswordFieldContains 4 constructors. They are as following:)

1. JPasswordField()

1. JPasswordField()

2. JPasswordField(int columns)

2. JPasswordField(int列)

3. JPasswordField(String text)

3. JPasswordField(字符串文本)

4. JPasswordField(String text, int columns)

4. JPasswordField(字符串文本,int列)

(Example:)

To generate a password component, swing provides Jpasswordfield that takes user input in encrypted format.

为了生成密码组件,swing提供了Jpasswordfield,它以加密格式接收用户输入。

import javax.swing.*;    
public class SPasswordFieldDemo1 
{  
    public static void main(String[] args) 
  {    
  JFrame passWord_f=new JFrame("studytonight ==> Password Field Demo");    
  JPasswordField passWord_value = new JPasswordField();   
  JLabel passWord_l1=new JLabel("Password ");    
        passWord_l1.setBounds(20,100, 100,30);    
        passWord_value.setBounds(100,100,100,30);    
        passWord_f.add(passWord_value);  
  passWord_f.add(passWord_l1);  
        passWord_f.setSize(300,300);    
        passWord_f.setLayout(null);    
        passWord_f.setVisible(true);     
  }  
}


javadoc组件 java组件容器_swing_11

javadoc组件 java组件容器_spring_12

(JTable)

In Java, Swing toolkit contains a JTable Class. It is under package javax.swing.JTable class. It used to draw a table to display data.

在Java中,Swing工具箱包含一个JTable类。 它在包javax.swing.JTable类下。 它用来绘制表格来显示数据。

(The JTableContains 2 constructors. They are as following:)

1. JTable()

1. JTable()

2. JTable(Object[][] rows, Object[] columns)

2. JTable(Object [] []行,Object []列)

(Example:)

We are creating an example to create a table using Jtable class and then add it to the Jframe container.

我们正在创建一个示例,该示例使用Jtable类创建表,然后将其添加到Jframe容器中。

import javax.swing.*;    
public class STableDemo1 
{    
    JFrame table_f;    
    STableDemo1(){    
    table_f=new JFrame();    
    String table_data[][]={ {"1001","Cherry"}, {"1002","Candy"}, {"1003","Coco"}};    
    String table_column[]={"SID","SNAME"};         
    JTable table_jt=new JTable(table_data,table_column);    
    table_jt.setBounds(30,40,200,300);          
    JScrollPane table_sp=new JScrollPane(table_jt);    
    table_f.add(table_sp);          
    table_f.setSize(300,400);    
    table_f.setVisible(true);    
}     
public static void main(String[] args) 
{    
    new STableDemo1();    
}    
}


javadoc组件 java组件容器_java_13

javadoc组件 java组件容器_android_14

(JList)

In Java, Swing toolkit contains a JList Class. It is under package javax.swing.JList class. It is used to represent a list of items together. One or more than one item can be selected from the list.

在Java中,Swing工具箱包含一个JList类。 它在包javax.swing.JList类下。 它用于一起表示项目列表。 可以从列表中选择一项或多项。

(Declaration)

public class JList extends JComponent implements Scrollable, Accessible

公共类JList扩展JComponent实现Scrollable,Accessible

(The JListContains 3 constructors. They are as following:)

1. JList()

1. JList()

2. JList(ary[] listData)

2. JList(ary [] listData)

3. JList(ListModel<ary> dataModel)

3. JList(ListModel <ary> dataModel)

(Example:)

In this example, we are creating a list of items using Jlist class. this list is used to show the items in a list format and get user input from the list of items. See the below example.

在此示例中,我们使用Jlist类创建项目列表。 此列表用于以列表格式显示项目,并从项目列表中获取用户输入。 请参见以下示例。

import javax.swing.*;  
public class SListDemo  
{  
     SListDemo()
  {  
        JFrame list_f= new JFrame();  
        DefaultListModel<String> list_l1 = new DefaultListModel<>();  
        list_l1.addElement("Red");  
        list_l1.addElement("Pink");  
        list_l1.addElement("Blue");  
        list_l1.addElement("Black");  
        JList<String> list1 = new JList<>(list_l1);  
        list1.setBounds(100,100, 75,75);  
        list_f.add(list1);  
        list_f.setSize(400,400);  
        list_f.setLayout(null);  
        list_f.setVisible(true);  
     }  
public static void main(String args[])  
    {  
  new SListDemo();  
    }
}


javadoc组件 java组件容器_javadoc组件_15

javadoc组件 java组件容器_spring_16