java包awt
Java AWT is an API that contains large number of classes and methods to create and manage graphical user interface ( GUI ) applications. The AWT was designed to provide a common set of tools for GUI design that could work on a variety of platforms. The tools provided by the AWT are implemented using each platform's native GUI toolkit, hence preserving the look and feel of each platform. This is an advantage of using AWT. But the disadvantage of such an approach is that GUI designed on one platform may look different when displayed on another platform that means AWT component are platform dependent.
Java AWT是一种API,其中包含大量用于创建和管理图形用户界面(GUI)应用程序的类和方法。 AWT旨在为GUI设计提供一套通用的工具,这些工具可以在多种平台上工作。 AWT提供的工具是使用每个平台的本机GUI工具包实现的,因此保留了每个平台的外观。 这是使用AWT的优势。 但是这种方法的缺点是,在一个平台上设计的GUI在另一个平台上显示时可能看起来有所不同,这意味着AWT组件依赖于平台。
AWT is the foundation upon which Swing is made i.e Swing is a improved GUI API that extends the AWT. But now a days AWT is merely used because most GUI Java programs are implemented using Swing because of its rich implementation of GUI controls and light-weighted nature.
AWT是制作Swing的基础,即Swing是改进的GUI API,它扩展了AWT。 但是现在仅使用AWT是因为大多数GUI Java程序都是使用Swing实现的,因为它具有丰富的GUI控件实现和轻量级的特性。
The hierarchy of Java AWT classes are given below, all the classes are available in java.awt package.
Java AWT类的层次结构如下所示,所有类均在java.awt包中提供。
Component class is at the top of AWT hierarchy. It is an abstract class that encapsulates all the attributes of visual component. A component object is responsible for remembering the current foreground and background colors and the currently selected text font.
组件类位于AWT层次结构的顶部。 它是一个抽象类,封装了可视组件的所有属性。 组件对象负责记住当前的前景色和背景色以及当前选择的文本字体。
Container is a component in AWT that contains another component like button, text field, tables etc. Container is a subclass of component class. Container class keeps track of components that are added to another component.
容器是AWT中的一个组件,包含另一个组件,例如按钮,文本字段,表格等。 容器是组件类的子类。 容器类跟踪添加到另一个组件的组件。
Panel class is a concrete subclass of Container. Panel does not contain title bar, menu bar or border. It is container that is used for holding components.
面板类是Container的一个具体子类。 面板不包含标题栏,菜单栏或边框。 它是用于容纳组件的容器。
Window class creates a top level window. Window does not have borders and menubar.
Window类创建一个顶层窗口。 窗口没有边框和菜单栏。
Frame is a subclass of Window and have resizing canvas. It is a container that contain several different components like button, title bar, textfield, label etc. In Java, most of the AWT applications are created using Frame window. Frame class has two different constructors,
框架是Window的子类,具有调整画布大小。 它是一个包含几个不同组件的容器,如按钮,标题栏,文本字段,标签等。在Java中,大多数AWT应用程序都是使用“ 框架”窗口创建的。 框架类有两个不同的构造函数,
Frame() throws HeadlessException
Frame(String title) throws HeadlessException
There are two ways to create a Frame. They are,
有两种创建框架的方法。 他们是,
By Instantiating Frame class
通过实例化框架类
By extending Frame class
通过扩展框架类
import java.awt.*;
public class Testawt
{
Testawt()
{
Frame fm=new Frame(); //Creating a frame
Label lb = new Label("welcome to java graphics"); //Creating a label
fm.add(lb); //adding label to the frame
fm.setSize(300, 300); //setting frame size.
fm.setVisible(true); //set frame visibilty true
}
public static void main(String args[])
{
Testawt ta = new Testawt();
}
}
package testawt;
import java.awt.*;
import java.awt.event.*;
public class Testawt extends Frame
{
public Testawt()
{
Button btn=new Button("Hello World");
add(btn); //adding a new Button.
setSize(400, 500); //setting size.
setTitle("StudyTonight"); //setting title.
setLayout(new FlowLayout()); //set default layout for frame.
setVisible(true); //set frame visibilty true.
}
public static void main (String[] args)
{
Testawt ta = new Testawt(); //creating a frame.
}
}
While creating a frame (either by instantiating or extending Frame class), Following two attributes are must for visibility of the frame:
在创建框架时(通过实例化或扩展Frame类),必须具有以下两个属性才能使框架可见:
When you create other components like Buttons, TextFields, etc. Then you need to add it to the frame by using the method - add(Component's Object);
当创建其他组件(如Buttons,TextFields等)时,则需要使用方法-add(Component's Object);将其添加到框架中。
You can add the following method also for resizing the frame - setResizable(true);
您还可以添加以下方法来调整框架的大小-setResizable(true);
In Java, AWT contains a Button Class. It is used for creating a labelled button which can perform an action.
在Java中,AWT包含一个Button类。 它用于创建可以执行操作的带标签的按钮。
public class Button extends Component implements Accessible
公共类Button扩展组件实现Accessible
Lets take an example to create a button and it to the frame by providing coordinates.
让我们以创建一个按钮并将其添加到框架为例。
import java.awt.*;
public class ButtonDemo1
{
public static void main(String[] args)
{
Frame f1=new Frame("studytonight ==> Button Demo");
Button b1=new Button("Press Here");
b1.setBounds(80,200,80,50);
f1.add(b1);
f1.setSize(500,500);
f1.setLayout(null);
f1.setVisible(true);
}
}
In Java, AWT contains a Label Class. It is used for placing text in a container. Only Single line text is allowed and the text can not be changed directly.
在Java中,AWT包含标签类。 用于将文本放置在容器中。 仅允许使用单行文本,并且不能直接更改文本。
public class Label extends Component implements Accessible
公共类Label扩展组件实现Accessible
In this example, we are creating two labels to display text to the frame.
在此示例中,我们将创建两个标签以在框架上显示文本。
import java.awt.*;
class LabelDemo1
{
public static void main(String args[])
{
Frame l_Frame= new Frame("studytonight ==> Label Demo");
Label lab1,lab2;
lab1=new Label("Welcome to studytonight.com");
lab1.setBounds(50,50,200,30);
lab2=new Label("This Tutorial is of Java");
lab2.setBounds(50,100,200,30);
l_Frame.add(lab1);
l_Frame.add(lab2);
l_Frame.setSize(500,500);
l_Frame.setLayout(null);
l_Frame.setVisible(true);
}
}
In Java, AWT contains aTextField Class. It is used for displaying single line text.
在Java中,AWT包含一个TextField类。 用于显示单行文本。
public class TextField extends TextComponent
公共类TextField扩展了TextComponent
We are creating two textfields to display single line text string. This text is editable in nature, see the below example.
我们正在创建两个文本字段以显示单行文本字符串。 该文本本质上是可编辑的,请参见以下示例。
import java.awt.*;
class TextFieldDemo1{
public static void main(String args[]){
Frame TextF_f= new Frame("studytonight ==>TextField");
TextField text1,text2;
text1=new TextField("Welcome to studytonight");
text1.setBounds(60,100, 230,40);
text2=new TextField("This tutorial is of Java");
text2.setBounds(60,150, 230,40);
TextF_f.add(text1);
TextF_f.add(text2);
TextF_f.setSize(500,500);
TextF_f.setLayout(null);
TextF_f.setVisible(true);
}
}
In Java, AWT contains aTextArea Class. It is used for displaying multiple-line text.
在Java中,AWT包含一个TextArea类。 用于显示多行文本。
public class TextArea extends TextComponent
公共类TextArea扩展TextComponent
In this example, we are creating a TextArea that is used to display multiple-line text string and allows text editing as well.
在此示例中,我们将创建一个TextArea,用于显示多行文本字符串并允许文本编辑。
import java.awt.*;
public class TextAreaDemo1
{
TextAreaDemo1()
{
Frame textArea_f= new Frame();
TextArea area=new TextArea("Welcome to studytonight.com");
area.setBounds(30,40, 200,200);
textArea_f.add(area);
textArea_f.setSize(300,300);
textArea_f.setLayout(null);
textArea_f.setVisible(true);
}
public static void main(String args[])
{
new TextAreaDemo1();
}
}
In Java, AWT contains a Checkbox Class. It is used when we want to select only one option i.e true or false. When the checkbox is checked then its state is "on" (true) else it is "off"(false).
在Java中,AWT包含一个Checkbox类。 当我们只想选择一个选项(即true或false)时使用它。 选中此复选框时,其状态为“开”(true),否则为“关”(false)。
public class Checkbox extends Component implements ItemSelectable, Accessible
公共类Checkbox扩展组件实现ItemSelectable,Accessible
In this example, we are creating checkbox that are used to get user input. If checkbox is checked it returns true else returns false.
在此示例中,我们正在创建用于获取用户输入的复选框。 如果选中此复选框,则返回true,否则返回false。
import java.awt.*;
public class CheckboxDemo1
{
CheckboxDemo1(){
Frame checkB_f= new Frame("studytonight ==>Checkbox Example");
Checkbox ckbox1 = new Checkbox("Yes", true);
ckbox1.setBounds(100,100, 60,60);
Checkbox ckbox2 = new Checkbox("No");
ckbox2.setBounds(100,150, 60,60);
checkB_f.add(ckbox1);
checkB_f.add(ckbox2);
checkB_f.setSize(400,400);
checkB_f.setLayout(null);
checkB_f.setVisible(true);
}
public static void main(String args[])
{
new CheckboxDemo1();
}
}
In Java, AWT contains aCheckboxGroup Class. It is used to group a set of Checkbox. When Checkboxes are grouped then only one box can be checked at a time.
在Java中,AWT包含一个CheckboxGroup类。 它用于对一组复选框进行分组。 将复选框分组后,一次只能选中一个复选框。
public class CheckboxGroup extends Object implements Serializable
公共类CheckboxGroup扩展对象实现Serializable
This example creates a checkboxgroup that is used to group multiple checkbox in a single unit. It is helpful when we have to select single choice among the multiples.
本示例创建一个复选框组,该复选框组用于将单个单元中的多个复选框分组。 当我们必须在多个选择中选择一个时,这将很有帮助。
import java.awt.*;
public class CheckboxGroupDemo
{
CheckboxGroupDemo(){
Frame ck_groupf= new Frame("studytonight ==>CheckboxGroup");
CheckboxGroupobj = new CheckboxGroup();
Checkbox ckBox1 = new Checkbox("Yes", obj, true);
ckBox1.setBounds(100,100, 50,50);
Checkbox ckBox2 = new Checkbox("No", obj, false);
ckBox2.setBounds(100,150, 50,50);
ck_groupf.add(ckBox1);
ck_groupf.add(ckBox2);
ck_groupf.setSize(400,400);
ck_groupf.setLayout(null);
ck_groupf.setVisible(true);
}
public static void main(String args[])
{
new CheckboxGroupDemo();
}
}
In Java, AWT contains a Choice Class. It is used for creating a drop-down menu of choices. When a user selects a particular item from the drop-down then it is shown on the top of the menu.
在Java中,AWT包含一个Choice类。 它用于创建选项的下拉菜单。 当用户从下拉菜单中选择特定项目时,该项目将显示在菜单顶部。
public class Choice extends Component implements ItemSelectable, Accessible
公共类Choice扩展组件实现ItemSelectable,Accessible
In this example, we are creating drop-down menu that is used to get user choice from multiple choices.
在此示例中,我们将创建一个下拉菜单,用于从多个选项中获取用户的选择。
import java.awt.*;
public class ChoiceDemo
{
ChoiceDemo()
{
Frame choice_f= new Frame();
Choice obj=new Choice();
obj.setBounds(80,80, 100,100);
obj.add("Red");
obj.add("Blue");
obj.add("Black");
obj.add("Pink");
obj.add("White");
obj.add("Green");
choice_f.add(obj);
choice_f.setSize(400,400);
choice_f.setLayout(null);
choice_f.setVisible(true);
}
public static void main(String args[])
{
new ChoiceDemo();
}
}
In Java, AWT contains a List Class. It is used to represent a list of items together. One or more than one item can be selected from the list.
在Java中,AWT包含一个列表类。 它用于一起表示项目列表。 可以从列表中选择一项或多项。
public class List extends Component implements ItemSelectable, Accessible
公共类List扩展组件实现ItemSelectable,Accessible
In this example, we are creating a list that is used to list out the items.
在此示例中,我们将创建一个用于列出项目的列表。
import java.awt.*;
public class ListDemo
{
ListDemo()
{
Frame list_f= new Frame();
List obj=new List(6);
obj.setBounds(80,80, 100,100);
obj.add("Red");
obj.add("Blue");
obj.add("Black");
obj.add("Pink");
obj.add("White");
obj.add("Green");
list_f.add(obj);
list_f.setSize(400,400);
list_f.setLayout(null);
list_f.setVisible(true);
}
public static void main(String args[])
{
new ListDemo();
}
}
In Java, AWT contains a Canvas Class. A blank rectangular area is provided. It is used when a user wants to draw on the screen.
在Java中,AWT包含一个Canvas类。 提供了一个空白的矩形区域。 当用户想要在屏幕上绘制时使用。
public class Canvas extends Component implements Accessible
公共类Canvas扩展组件实现Accessible
The canvas is used to provide a place to draw using mouse pointer. We can used it to get user architectural user input.
画布用于提供使用鼠标指针绘制的位置。 我们可以使用它来获取用户架构用户输入。
import java.awt.*;
public class CanvasDemo1
{
public CanvasDemo1()
{
Frame canvas_f= new Frame("studytonight ==> Canvas");
canvas_f.add(new CanvasDemo());
canvas_f.setLayout(null);
canvas_f.setSize(500, 500);
canvas_f.setVisible(true);
}
public static void main(String args[])
{
new CanvasDemo1();
}
}
class CanvasDemo extends Canvas
{
public CanvasDemo() {
setBackground (Color.WHITE);
setSize(300, 200);
}
public void paint(Graphics g)
{
g.setColor(Color.green);
g.fillOval(80, 80, 150, 75);
}
}
翻译自: https://www.studytonight.com/java/java-awt.php
java包awt