當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java Component用法及代碼示例

Component 類是所有組件的超類。組件類可以與頁麵、Web 應用程序的組件鏈接。組件清楚地表明這是對象的圖形表示。

Component類的重要方法:

  1. 公共無效添加(組件c):該方法通過將組件作為參數將組件插入到容器中。
  2. 公共無效setSize(int寬度,int高度):該方法以高度和寬度為參數來設置組件的大小。
  3. 公共無效setLayout(LayoutManager lm):該方法以LayoutManager為參數,設置布局以特定方式設置組件。
  4. 公共無效setVisible(布爾狀態):該方法將組件的可見性設置為可見或不可見。如果它設置為 true,則該組件將在輸出中可見,否則如果它設置為 false 或未定義的組件將在輸出中不可見。

Note: LayoutManager helps us to give the positioning and size of components that should be visible.

組件類中組件的類型

組件類中的組件如下:

  1. Container
  2. Button
  3. Label
  4. Checkbox
  5. Choice
  6. List

所有這些組件都存在於java.awt包。我們可以單獨導入每個組件,即導入 java.awt.Button,導入 java.awt.ContainerETC。

Note: If we want to import all components at a time we can do that by importing like import java.awt.*

above-listed組件的層次結構如下:

組件的層次結構

Hierarchical Structure of Components

讓我們詳細看看每個組件。

1. 容器

Container 是一個組件,將用於擴展其他組件,如窗口、麵板、框架、對話框和 Applet,如上圖所示。

  • 窗戶:窗口是一個不包含邊框和菜單欄的容器。我們必須使用另一個窗口、框架和對話框來創建一個窗口。創建實例就是創建Window Container 的方式。
  • 控製板:麵板也是一個不包含標題欄、菜單或邊框的容器。它是一個容器,保存按鈕、文本字段等組件。創建實例就是創建麵板容器的方法,可以添加組件。
  • 框架:Frame 是創建 AWT 應用程序時使用的容器。它可以有標題欄、菜單欄、邊框以及按鈕、滾動條等組件。
  • 對話:對話框是一個容器,它將顯示我們想要在屏幕上顯示的消息。

2. 按鈕

按鈕是一個帶標簽的組件,單擊時會執行事件。它是由 Button 類創建的。單擊時,它會通過 AWT 發送 ActionEvent 的實例來執行某些操作。 ActionEvent 在按鈕上調用 processEvent,它接收所有事件並通過調用自己的 processActionEvent 方法來執行操作。為了完成這些事情,它需要實現 ActionListener。按鈕類的聲明將是

public class Button extends Component implements Accessible

它包含 2 個構造函數:

  1. Button() : This constructor will create a button with no label
  2. Button(String label) : This constructor creates a button with label value as a value when we creates an object

例子:

Java


import java.awt.*;
// Driver Class
class SubmitButton extends Frame {
      // main function
    public static void main(String[] args)
    {
        // create instance of frame with label
        Frame frame = new Frame("Submit form");
        // create instance of button with label
        Button button = new Button("Submit");
        // set the position for the button in frame
        button.setBounds(40, 130, 70, 20);
        // adding button to the frame
        frame.add(button);
        // setting size for the frame
        frame.setSize(500, 500);
        // setting layout for the frame
        frame.setLayout(null);
        // visibility of frame to display the output\
         // without this output will be blank
        frame.setVisible(true);
    }
}

我們可以通過以下命令來運行它:

輸出:

Output

3. 標簽

它用於顯示容器中的文本。它將以以下形式顯示文本隻讀,用戶不能直接更改。我們需要創建一個Label Class的實例來創建Label。標簽類別聲明將是

public class Label extends Component implements Accessible

它有 3 個構造函數:

  1. Label() : Creates an Empty Label.
  2. Label(String labelname) : Creates a Label with labelname as parameter value.
  3. Label(String labelname, int align) : Creates a Label with labelname as parameter value and proper alignments.

Note: Align parameter aligns the text in proper alignment and it has 3 types of alignment.

  • LEFT:指定文本應左對齊。
  • RIGHT:指定文本應右對齊。
  • CENTER 指定文本應居中對齊。

例子:

Java


import java.awt.*;
public class ShowLabelText extends Frame {
    public static void main(String args[])
    {
        // creating objects for Frame and Label class
        Frame frame = new Frame("Label Text");
        // Creating label One
        Label label1 = new Label("Label One", Label.LEFT);
        // Creating label Two
        Label label2 = new Label("Label Two", Label.RIGHT);
        // set the location of label in px
        label1.setBounds(50, 100, 100, 50);
        label2.setBounds(50, 150, 100, 50);
        // adding labels to the frame
        frame.add(label1);
        frame.add(label2);
        // setting size, layout
        // and visibility of frame
        frame.setSize(500, 500);
        frame.setLayout(null);
        frame.setVisible(true);
    }
}

我們可以通過以下命令來運行它:

輸出:

Output

4. 複選框

它用於在容器中創建複選框。它可以通過選中和取消選中複選框來獲取 true 或 false 值。

  • 檢查 - 返回 true
  • 未選中 - 返回 false

它可以通過創建 Checkbox 的實例來創建。標簽類別聲明將是

public class Checkbox extends Component implements ItemSelectable, Accessible  

它有 5 個構造函數:

  1. Checkbox() : Creates a checkbox with empty label
  2. Checkbox(String checkboxlabel) : Creates a Checkbox with checkboxlabel as parameter value.
  3. Checkbox(String checkboxlabel, boolean status) : Creates a Checkbox with checkboxlabel as parameter value and sets the status either true or false.
  4. Checkbox(String checkboxlabel, boolean status, CheckboxGroup cbgroup) : Creates a Checkbox with checkboxlabel as parameter value and sets the status to the specified checkbox group.
  5. Checkbox(String checkboxlabel, CheckboxGroup cbgroup, boolean status) : Creates a Checkbox with checkboxlabel as parameter value for the specified cbgroup and sets the status.

示例 1:

Java


// importing AWT class
import java.awt.*;
public class CourseCheck {
    // main method
    public static void main(String args[])
    {
        // creating the frame with the label
        Frame frame = new Frame("Courses");
        // creating checkbox java
        Checkbox java = new Checkbox("Java");
        // setting location of checkbox in frame
        java.setBounds(100, 100, 50, 50);
        // creating checkbox python with status as true
        Checkbox python = new Checkbox("Python", true);
        // setting location of checkbox in frame
        python.setBounds(100, 150, 50, 50);
        // adding checkboxes to frame
        frame.add(java);
        frame.add(python);
        // setting size, layout and
        // visibility of frame
        frame.setSize(400, 400);
        frame.setLayout(null);
        frame.setVisible(true);
    }
}

我們可以通過以下命令來運行它:

輸出:

Output

在上麵的輸出中,我們可以選擇這兩個選項。

示例2:

Java


// importing AWT class
import java.awt.*;
public class GenderCheck {
    // main method
    public static void main(String args[])
    {
        // creating the frame with the label
        Frame frame = new Frame("Gender");
        // creating a CheckboxGroup
        CheckboxGroup cbgroup = new CheckboxGroup();
        // creating checkbox male with
        // status as true for cbgroup
        Checkbox male = new Checkbox("Male", cbgroup, true);
        male.setBounds(100, 100, 50, 50);
        // creating checkbox female with
        // status as false for cbgroup
        Checkbox female = new Checkbox("Female", cbgroup, false);
        // setting location of checkbox in frame
        female.setBounds(100, 150, 50, 50);
        // adding checkboxes to frame
        frame.add(male);
        frame.add(female);
        // setting size, layout
        // and visibility of frame
        frame.setSize(400, 400);
        frame.setLayout(null);
        frame.setVisible(true);
    }
}

我們可以使用命令來運行它:

輸出:

Output

在上麵的輸出中,我們可以選擇任何一個選項,因為它充當單選按鈕。

5. 選擇

它用於顯示彈出菜單以從菜單項中選擇任何項目。所選選項將顯示在菜單欄的頂部。我們需要創建 Choice 類的實例來創建 Choice。選擇類別的聲明將是

public class Choice extends Component implements ItemSelectable, Accessible  

它有 1 個構造函數:

Choice() : Creates a new Choice menu of items

例子:

Java


import java.awt.*;
public class SelectItems {
    // main method
    public static void main(String args[])
    {
        // creating a Frame
        Frame frame = new Frame();
        // creating a choice component
        Choice choice = new Choice();
        // setting the bounds of choice menu
        choice.setBounds(70, 70, 75, 75);
        // adding items to the choice menu
        choice.add("--select--");
        choice.add("Shampoo");
        choice.add("Eggs");
        choice.add("Bottles");
        // adding choice menu to frame
        frame.add(choice);
        // setting size, layout
        // and visibility of frame
        frame.setSize(300, 300);
        frame.setLayout(null);
        frame.setVisible(true);
    }
}

我們可以通過以下命令來運行它:

輸出:

Output

6. 清單

列表對象創建一個項目列表,我們可以在其中一次選擇一個或多個項目。我們需要創建一個List類的實例來創建一個List。標簽類別聲明將是

public class List extends Component implements ItemSelectable, Accessible  

它有 3 個構造函數:

  1. List() : Creates a new Scrolling List
  2. List(int noofrows) : Creates a new Scrolling List which displays the list of items with given no. of rows visible with parameter noofrows.
  3. List(int noofrows, boolean multi) : Creates a new Scrolling list which displays the list of items with given no. of rows visible and allows to select multiple items at a time.

例子:

Java


import java.awt.*;
public class SelectList {
    // main method
    public static void main(String args[])
    {
        // creating frame1
        Frame frame1 = new Frame();
        // creating list1 with 5 rows
        List list1 = new List(5);
        // setting the position of list component
        list1.setBounds(100, 100, 75, 75);
        // adding list items to the list1
        list1.add("Shampoo");
        list1.add("Conditioner");
        list1.add("Eggs");
        list1.add("Bottles");
        list1.add("Butter");
        // adding the list1 to frame1
        frame1.add(list1);
        // setting size, layout
        // and visibility of frame1
        frame1.setSize(400, 400);
        frame1.setLayout(null);
        frame1.setVisible(true);
        // creating frame2
        Frame frame2 = new Frame();
        // creating list2 with 5 rows
        // and multi select items as true
        List list2 = new List(5, true);
        // setting the position of list component
        list2.setBounds(100, 100, 75, 75);
        // adding list items to the list2
        list2.add("Shampoo");
        list2.add("Conditioner");
        list2.add("Eggs");
        list2.add("Bottles");
        list2.add("Butter");
        // adding the list2 to frame2
        frame2.add(list2);
        // setting size, layout
        // and visibility of frame2
        frame2.setSize(400, 400);
        frame2.setLayout(null);
        frame2.setVisible(true);
    }
}

我們可以通過以下命令來運行它:

輸出1:

Output

在此輸出列表中,我們可以一次選擇任何一項。

輸出2:

Output

在此輸出列表中,我們可以一次選擇多個項目。這些是 Component 類和有關組件類中存在的所有組件。

經常問的問題

1.Java中什麽是組件?

組件是可以在屏幕上顯示的具有圖形表示的交互對象。

2.哪些組件不能添加到框架中?

Java Swing 中的某些組件無法直接添加到框架中。下麵提到了一些組件:

  1. MenuBar
  2. Scrollbar
  3. Dialog
  4. Applet


相關用法


注:本文由純淨天空篩選整理自devasishakula503大神的英文原創作品 Component Class in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。