JAVA中的AWT组件

JAVA中的AWT组件
1、  标签组件
Label组件有下列三种构造方法:
      Label()创建一个空字符串的标签组件
        Label(String strCaption)创建一个指定字符串的标签组件
        Label(String strCaption,int alignment)创建一个指定字符串并且按一定方式对齐的标签组件,其中参数alignment的值有LEFT、RIGHT、CENTER,默认是LEFT左对齐。
获取和改变当前的显示文本内容和对齐方式的方法有;
        public void setText(String strCaptionText)设置Label的显示文本内容
       public String gettext()获取Label当前的显示文本内容
        public int getAlignment()获取Label的对齐方式
       public void setAlignment(int alignment)设置Label的对齐方式
//Label示例
import java.awt.*;
import java.applet.*;
 
public class LabelDemo extends Applet
{
       Label lblOneLabel=new Label("标签一");
       Label lblSecondLabel=new Label("标签二");
       Label lblThirdLabel=new Label("标签三");
      
       public void init()
       {
              //向Applet容器中加入第一个Label
              add(lblOneLabel);
              //向Applet容器中加入第二个Label
              add(lblSecondLabel);
              //向Applet容器中加入第三个Label
              add(lblThirdLabel);
       }    
}
2、  按钮组件
Button类提供了两个构造方法来构造按钮组件:
l        Button()Constructs a Button with no label.
l        Button(String label) Constructs a Button with the specified label.
// ButtonDemo.java
import java.awt.*;
import java.applet.*;
 
public class ButtonDemo extends Applet
{
       Button btnOneButton=new Button ("按钮一");
       Button btnSecondButton=new Button ("按钮二");
       Button btnThirdButton=new Button ("按钮三");
      
       public void init()
       {
              //向Applet容器中加入第一个Button
              add(btnOneButton);
              //向Applet容器中加入第二个Button
              add(btnSecondButton);
              //向Applet容器中加入第三个Button
              add(btnThirdButton);
       }    
}
3、  复选框组件
复选框组件可以说是一种特殊的Button组件,它有两种状态:一种是on表示选中,另外一种是off表示没有选中,默认是off,Checkbox可以单独使用,也可以利用CheckboxGroup类来组成组使用,在这种情况下,就只能选择一个Checkbox组件,就变成了Radiobox的效果。
Checkbox()
          Creates a check box with no label.
Checkbox(String label)
          Creates a check box with the specified label.
Checkbox(String label, boolean state)
          Creates a check box with the specified label and sets the specified state.
Checkbox(String label, boolean state, CheckboxGroup group)
          Constructs a Checkbox with the specified label, set to the specified state, and in the specified check box group.
Checkbox(String label, CheckboxGroup group, boolean state)
          Creates a check box with the specified label, in the specified check box group, and set to the specified state.
// CheckboxDemo.java
import java.awt.*;
import java.applet.*;
 
public class CheckboxDemo extends Applet
{
       //创建单选按钮
//CheckboxGroup cbg = new CheckboxGroup();
       //Checkbox chkOneCheckbox=new Checkbox ("复选框一",cbg,true);
       //Checkbox chkSecondCheckbox=new Checkbox ("复选框二",cbg,false);
       //Checkbox chkThirdCheckbox=new Checkbox ("复选框三",cbg,false);
    Checkbox chkOneCheckbox=new Checkbox ("复选框一");
       Checkbox chkSecondCheckbox=new Checkbox ("复选框二");
       Checkbox chkThirdCheckbox=new Checkbox ("复选框三");
       public void init()
       {
 
 
              //向Applet容器中加入第一个Checkbox
              add(chkOneCheckbox);
              //向Applet容器中加入第二个Checkbox
              add(chkSecondCheckbox);
              //向Applet容器中加入第三个Checkbox
              add(chkThirdCheckbox);
       }
}
4、  单行文本组件
Constructor Summary
TextField()
          Constructs a new text field.
 
TextField(int columns)
          Constructs a new empty text field with the specified number of columns.
 
TextField(String text)
          Constructs a new text field initialized with the specified text.
 
TextField(String text, int columns)
          Constructs a new text field initialized with the specified text to be displayed, and wide enough to hold the specified number of columns.
 
基本属性:
l        columns表明要输入或显示的字符数的限制情况,可用getColumns()方法得到,setColumns(int column)方法进行设置
l        echoChar输入掩码,在屏幕中掩码输入时显示的字符,可用getEchoChar()返回一个char型,用setEchoChar(Char c)来设置掩码值
l        Editable表明该文本组件的读写属性:true表明可读写,false为只读,可用isEditable()、getEditable()和setEditable(boolean b)方法来操作读写属性
// TextFieldDemo.java
import java.awt.*;
import java.applet.*;
 
public class TextFieldDemo extends Applet
{
       Label lblUserName=new Label("用户姓名:");
       TextField txtUserName=new TextField (12);
      
       Label lblUserPasswd=new Label("用户密码:");
       TextField txtUserPasswd=new TextField (12);
      
       Button btnLogin=new Button("登录");
       Button btnReset =new Button("重置");
      
      
       public void init()
       {
              //设置密码的回显字符为*
              txtUserPasswd.setEchoChar('*');
             
              add(lblUserName);
              add(txtUserName);
             
              add(lblUserPasswd);
              add(txtUserPasswd);
             
              add(btnLogin);
              add(btnReset);
       }    
}
5、  多行文本组件
多行文本和单行文本组件相似,TextArea组件允许我们输入多行文本,可以任意给定宽度和高度,默认有滚动条,我们可以利用它大量输入文本。TextArea组件具有TextField组件和Scrollbar组件两者的属性和方法,TextField组件所有的属性和方法在TextArea组件中一样可以使用。另外,TextArea组件有自己特殊的属性:
rows属性:int值,表示在文本域中能够显示的文本的行数。我们可以用getRows()和setRows(int nRows)方法来操作rows属性值。
Constructor Summary
TextArea()
          Constructs a new text area with the empty string as text.
 
TextArea(int rows, int columns)
          Constructs a new text area with the specified number of rows and columns and the empty string as text.
 
TextArea(String text)
          Constructs a new text area with the specified text.
 
TextArea(String text, int rows, int columns)
          Constructs a new text area with the specified text, and with the specified number of rows and columns.
 
TextArea(String text, int rows, int columns, int scrollbars)
          Constructs a new text area with the specified text, and with the rows, columns, and scroll bar visibility as specified.
 
 
// TextAreaDemo.java
import java.awt.*;
import java.applet.*;
 
public class TextAreaDemo extends Applet
{
       TextArea txtOneTextArea=new TextArea ();
       TextArea txtSecondTextArea=new TextArea ("多行文本框");
 
      
       public void init()
       {
              //向Applet容器中加入第一个TextArea
              add(txtOneTextArea);
              //向Applet容器中加入第二个TextArea
              add(txtSecondTextArea);
 
       }    
}
6、  列表组件
列表组件List是包含多项可选文本项的滚动区域,与Checkbox组件不同的是,List组件可以允许单项和多项选择。
Constructor Summary
List()
          Creates a new scrolling list.
 
List(int rows)
          Creates a new scrolling list initialized with the specified number of visible lines.
 
List(int rows, boolean multipleMode)
          Creates a new scrolling list initialized to display the specified number of rows.
 
List组件最常用方法为void add(String name);void add(String name,int index);
List lst = new List(4, false);
lst.add("Mercury");
lst.add("Venus");
lst.add("Earth");
lst.add("JavaSoft",0);
lst.add("Mars");
lst.add("Jupiter");
lst.add("Saturn");
lst.add("Uranus",4);
lst.add("Neptune");
lst.add("Pluto");
List组件允许多项或单项选择,对于单项选择的列表,我们可以使用getSelectedItem()或者getSelectedIndex()方法来获取当前选中项的名称或者下标索引值。
//ListDemo.java
import java.awt.*;
import java.applet.*;
 
public class ListDemo extends Applet
{
             
       public void init()
       {
              List lstYear= new List(4,false);
              lstYear.add ("1990");
              lstYear.add ("1991");
              lstYear.add ("1992");
              lstYear.add ("1993");
              //把1998加入到列表的第三项中
              lstYear.add ("1998",2);
              lstYear.add ("1994");
              lstYear.add ("1995");
              lstYear.add ("1996");
              lstYear.add ("1997");
              //把lstYear加入到容器中并显示出来
              add(lstYear);
       }    
}
7、  菜单组件
菜单组件是AWT比较特殊的一类,它包括菜单栏(MenuBar)组件、菜单(Menu)组件和菜单项(MenuItem)三部分。通常,MenuBar中包含一个或者多个Menu,每个Menu有包括一个或者多个MenuItem。
MenuBar的构造函数为MenuBar() Creates a new menu bar.
Menu Constructor Summary
Menu()
          Constructs a new menu with an empty label.
 
Menu(String label)
          Constructs a new menu with the specified label.
 
Menu(String label, boolean tearOff)
          Constructs a new menu with the specified label, indicating whether the menu can be torn off.
 
Menu mnuFile= new Menu("文件");
Menu mnuEdit=new Menu("编辑");
Menu mnuSearch=new Menu("搜索");
Menu mnuHelp=new Menu("帮助");
创建完Menu以后,还需要为每一个Menu创建若干个MenuItem组件对象,这样整个菜单才算构造完成。MenuItem组件的构造方法为:
MenuItem Constructor Summary
MenuItem()
          Constructs a new MenuItem with an empty label and no keyboard shortcut.
 
MenuItem(String label)
          Constructs a new MenuItem with the specified label and no keyboard shortcut.
 
MenuItem(String label, MenuShortcut s)
          Create a menu item with an associated keyboard shortcut.
 
Menu mnuFile= new Menu("文件");
MenuItem mnuFileNew=new MenuItem ("新建");
MenuItem mnuFileOpen=new MenuItem ("打开…");
MenuItem mnuFileSave=new MenuItem ("保存");
MenuItem mnuFileSaveAs=new MenuItem ("另存为…");
MenuItem mnuFilePageSetting=new MenuItem ("页面设置…");
MenuItem mnuFilePrint=new MenuItem ("打印");
MenuItem mnuFileQuit=new MenuItem ("退出");
我们可以使用setEnabled(boolean bEnabled)方法来设置某个菜单项不可用。
mnuFileNew.setEnabled(false);//把新建文件菜单项变为不可用状态
mnuFileOpen.setEnabled(true);//把打开文件菜单项变为可用状态
还可以使用getLabel()和setLabel()方法来分别获取和改变菜单项的名称:
void setLabel(String strMenuName);
String getLabel;
我们可以使用MenuItem的子类CheckboxMenuItem类创建一个可选择的菜单项,构造方法为:
Constructor Summary
CheckboxMenuItem()
          Create a check box menu item with an empty label.
 
CheckboxMenuItem(String label)
          Create a check box menu item with the specified label.
 
CheckboxMenuItem(String label, boolean state)
          Create a check box menu item with the specified label and state.
 
CheckboxMenuItem类的特殊方法有:boolean getState()和void setState(boolean bCheckState)来获取菜单项的当前状态和设置当前状态。
//MenuDemo.java
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
 
public class MenuDemo extends Frame
{
       public MenuDemo(String strTitle)
       {
              //设置框架窗体标题
              super(strTitle);
 
              //创建菜单条并加入到框架窗体中
              MenuBar mnuMenuBar=new MenuBar();
              this.setMenuBar( mnuMenuBar );
 
              //创建File菜单和相应的菜单项
              Menu mnuFile= new Menu("文件");
              MenuItem mnuFileNew=new MenuItem ("新建");
              MenuItem mnuFileOpen=new MenuItem ("打开…");
              MenuItem mnuFileSave=new MenuItem ("保存");
              MenuItem mnuFileSaveAs=new MenuItem ("另存为…");
              MenuItem mnuFilePageSetting=new MenuItem ("页面设置…");
              MenuItem mnuFilePrint=new MenuItem ("打印");
              MenuItem mnuFileQuit=new MenuItem ("退出");
              //把菜单项加入到File菜单中
              mnuFile.add(mnuFileNew);
              mnuFile.add(mnuFileSave);
              mnuFile.add(mnuFileSaveAs);
              mnuFile.addSeparator();//添加分割条
              mnuFile.add(mnuFilePageSetting);
              mnuFile.add(mnuFilePrint);
              mnuFile.add(mnuFileQuit);
 
              //创建Edit菜单和相关菜单项并加入到Edit菜单中
              Menu mnuEdit=new Menu("编辑");
              mnuEdit.add( new MenuItem("剪切") );
              mnuEdit.add( new MenuItem("复制") );
              mnuEdit.add( new MenuItem("粘贴") );
 
              //创建Search菜单和相关菜单项并加入到Search菜单中
              Menu mnuSearch=new Menu("搜索");
              mnuSearch.add( new MenuItem("查找...") );
              mnuSearch.add( new MenuItem("查找下一个") );
              mnuSearch.add( new MenuItem("替换...") );
 
              //创建Help菜单和相关菜单项并加入到Help菜单中
              Menu mnuHelp=new Menu("帮助");
              mnuHelp.add( new MenuItem("关于帮助") );
              mnuHelp.add( new MenuItem("帮助主题") );
 
              //把所有菜单加入到菜单条中
              mnuMenuBar.add( mnuFile );
              mnuMenuBar.add( mnuEdit );
              mnuMenuBar.add( mnuSearch );
              mnuMenuBar.add( mnuHelp );
 
       }
 
       //设置框架窗体的大小为宽400,高为400
       public Dimension getPreferredSize()
       {
              return new Dimension(400,400);
       }
 
       //程序的入口方法
       public static void main( String[] args )
       {
              //创建框架窗体
              MenuDemo frmMenuDemo=new MenuDemo("这是个使用菜单的例子");
 
              //设置框架窗体的事件监听(关闭窗体事件)
              frmMenuDemo.addWindowListener(new WindowAdapter(){
                     public void windowClosing(WindowEvent e)
                     {
                            System.exit(0);
                     }
              });
 
              //显示框架窗体
              frmMenuDemo.pack();
              frmMenuDemo.show();
 
       }
}
8、  下拉列表(Choise
构造方法为:Choice() Creates a new choice menu.
//ChoiseDemo.java
import java.awt.*;
import java.applet.*;
 
public class ChoiseDemo extends Applet
{
       public void init()
       {
               Choice ColorChooser = new Choice();
               ColorChooser.add("Green");
               ColorChooser.add("Red");
               ColorChooser.add("Blue");
               add(ColorChooser);
       }
}
9、  画布(Canvas
画布组件提供了空白的绘图面,画布组件可以接受事件,而Applet小程序不接受事件。
//CanvasDemo.java
import java.awt.*;
import java.applet.*;
 
public class CanvasDemo extends Applet
{
       public void init()
       {
              //创建Canvas实例
              Canvas canvas=new MyCanvas();
              //在Applet容器中显示Canvas组件
              this.add(canvas);
       }
}
//这是我们扩展的Canvas类
class MyCanvas extends Canvas
{
       public void paint(Graphics g)
       {
              //得到画布的大小           
              Dimension size= this.getSize();
              //绘制画布的外围矩形区域
              g.drawRect(0,0,size.width-1,size.height-1);
              //绘制画布的三维效果
              g.setColor(Color.lightGray);
              g.draw3DRect(1,1,size.width-3,size.height-3,true);
             
              g.setColor(Color.blue);
              g.drawString("This is Canvas",120,20);
             
              g.setColor(Color.red);
              g.fillRect(10,30,60,60);
             
              g.setColor(Color.green);
              g.drawLine(120,150,20,90);
       }
       //重写该方法来设置Canvas组件的首选大小
       //这个方法必须覆盖,不然Canvas无法正确显示       
       public Dimension getPreferredSize()
       {
              return new Dimension(400,400);             
       }
}
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值