Java----AWT组件开发和Swing界面编程(一)

一、AWT组件开发

1、AWT入门

       AWT是抽象窗口工具箱的缩写,它为编写图形用户界面提供了用户接口,通过这个接口就可以继承很多方法,省去了很多工作。AWT还能使应用程序更好地同用户进行交互。

       AWT中的容器是一种特殊的组件,他可以包含其他组件,即可以把组件方法容器中。Container类是用来存放其他组件的Component类的子类,Frame类又是Component的子类。Frame类用于创建具有标题栏和边界的窗口。这里通过继承Frame类来建立自己的界面。

[java]  view plain  copy
 print ?
  1. public class test extendsFrame{  
  2.     //创建构造器  
  3.     public test() throws HeadlessException {  
  4.         this.setTitle("第一个窗口程序");  
  5.         //x , y是距离显示器左上角的距离;w , h是窗口的长宽  
  6.         this.setBounds(100100250250);  
  7.         //或者使用以下方式代替上面的一句代码  
  8. //        this.setLocation(100, 100);  
  9. //        this.setSize(250 , 250);  
  10.         this.setVisible(true);  
  11.     }  
  12.     public static void main(String[] str) {  
  13.         new test();  
  14.     }  
  15. }  

       上面是窗口中一些必不可少的东西,下面是一些窗口的基础应用:

       >setTitle(“窗口”):定义窗口名称

       >add(button):把按钮添加到窗口中

       >setBackground(Color):设置窗口的背景颜色

       >setResizable(boolean):设置窗口大小是否可以改变

       >setAlwaysOnTop(boolean):设置窗口是否总在最上面

       >setBounds(x, y, w, h):设置窗口起始位置和大小

       >setVisible(boolean):设置窗口可见

       如果想创建多个窗口,只需要在主方法main()中创建多个对象就行了。

2、布局管理器

       Java中的图形界面在布局管理上采用容器和布局管理相分离的方案,也就是说容器只是把组件放进来,但它不管怎样放置。至于如何放置需要用到布局管理器。Java中有几种布局管理器,分别是:FlowLayout , BorderLayout, GridLayout和GardLayout。

       1)、FlowLayout

       FlowLayout布局管理器是默认的布局管理器,它将组件按照从左到右、从上到下的顺序来安排,并在默认情况下使组件尽量居中放置。下面的代码要添加到test()类中:

        this.setLayout(new FlowLayout());

        //将按钮组件放入容器中

        this.add(new Button("确定"));

        this.add(new Button("取消"));

       大家可以通过创建许多按钮来观察FlowLayout对组件的摆放规律,下面是使用一个按钮监听事件来实现按钮的添加:

[java]  view plain  copy
 print ?
  1. public class test extendsFrame implements ActionListener{  
  2.     int i;  
  3.     Button b = new Button("Add");  
  4.     public test() throws HeadlessException {  
  5.         this.setTitle("第一个窗口程序");  
  6.         this.setLayout(new FlowLayout());  
  7.         this.add(b);  
  8.         b.addActionListener(this);  
  9.         this.setBounds(100100250250);  
  10.         this.setVisible(true);  
  11.     }  
  12.     @Override  
  13.     public void actionPerformed(ActionEvent e){  
  14.         i++;  
  15.         Button bi = newButton("Button" + i);  
  16.         this.add(bi);  
  17.         this.setVisible(true);  
  18.     }  
  19.     public static void main(String[] str) {  
  20.         new test();  
  21.     }  
  22. }  

       在本程序中,由于按钮的大小不同,其整体看起来不太整齐,但这更说明了FlowLayout布局管理器的规则。

       2)、BorderLayout

       BorderLayout布局管理器只允许在容器内放置5个组件,这5个组件的位置是由BorderLayout类中的North、South、East、West和Center5个常量来确定的,他们对应着容器中的上下左右中,用法如下:

       this.add(new Button(“按钮”) ,BorderLayout.NORTH);

       this.add(new Button(“按钮”) ,BorderLayout.CENTER);

       组件在BorderLayout中的大小都是可以改变的。一般情况下可以让中间区域大一些,而且可以只用其中几个区域。

       3)、GridLayout

       GridLayout布局管理器是矩形网格,在网格中放置组件,每个网格的高度和宽度都相等,组件的排列顺序与FlowLayout相同。组件随着网格的大小而在水平和垂直方向上拉伸,网格的大小是由容器的大小和创建网格的多少来确定的。其用法如下:

       this.setLayout(newGridLayout(2 , 3)); //创建一个2行3列的网格

       this.add(new Button(“按钮”));

       当组件数目大于网格数时,GridLayout保持行数不变而自动增加列数。

       4)、CardLayout

       CardLayout运行在一个组件中每次只显示一组组件中的某一个,用户可以根据需要来选择使用哪个组件。CardLayout类提供了如下选择组件的方法。

       >First(Container p):选择容器中的第一个组件

       >last(Container p):选择容器中的最后一个组件

       >next(Container p):选择容器中当前组件的下一个组件

       >prebvious(Container p):选择容器中当前组件的上一个组件

       >show(Container p ,String name):选择容器中指定的组件

       前面几种很容易理解,而show()方法来选择容器中指定的组件。它的第一个参数是管理组件的容器,第二个参数是标识要显示组件字符串,该字符串与将组件添加到容器时使用的字符串相同。其用法如下:

[java]  view plain  copy
 print ?
  1. public class test extendsFrame implements ActionListener{  
  2.     Panel p = new Panel();  
  3.     Button bf = new Button("First");  
  4.     Button bl = new Button("Last");  
  5.     Button bn = new Button("next");  
  6.     Button bp = newButton("previous");  
  7.     Button bg = new Button("Go");  
  8.     TextField tf = new TextField();  
  9.     //设置CardLayout布局管理器  
  10.     CardLayout cl = new CardLayout();  
  11.     public test() throws HeadlessException {  
  12.         this.setTitle("CardLayout布局管理器");  
  13.         this.setLayout(null);  
  14.         this.add(p);  
  15.         //定义p面板为CardLayout布局管理器  
  16.         p.setLayout(cl);  
  17.         //为CardLayout布局管理器添加按钮  
  18.         for (int i = 1; i <= 10; i++) {  
  19.             Button btemp = newButton("Button" + i);  
  20.             p.add(btemp, "" + i);  
  21.         }  
  22.         //为每个组件设置大小位置并添加到容器中  
  23.         p.setBounds(1040100100);  
  24.         this.add(bf);  
  25.         bf.addActionListener(this);  
  26.         bf.setBounds(120406020);  
  27.         this.add(bl);  
  28.         bl.addActionListener(this);  
  29.         bl.setBounds(120706020);  
  30.         this.add(bn);  
  31.         bn.addActionListener(this);  
  32.         bn.setBounds(1201006020);  
  33.         this.add(bp);  
  34.         bp.addActionListener(this);  
  35.         bp.setBounds(1201306020);  
  36.         this.add(bg);  
  37.         bg.addActionListener(this);  
  38.         bg.setBounds(601604020);  
  39.         this.add(tf);  
  40.         tf.setBounds(201604020);  
  41.         //设置窗口的位置和大小  
  42.         this.setBounds(200200210220);  
  43.         this.setVisible(true);  
  44.     }  
  45.     @Override  
  46.     public void actionPerformed(ActionEvent e){  
  47.         if (e.getSource() == bn) {  
  48.             cl.next(p);  
  49.         }  
  50.         if (e.getSource() == bp) {  
  51.             cl.previous(p);  
  52.         }  
  53.         if (e.getSource() == bf) {  
  54.             cl.first(p);  
  55.         }  
  56.         if (e.getSource() == bl) {  
  57.             cl.last(p);  
  58.         }  
  59.         if (e.getSource() == bg) {  
  60.             cl.show(p, tf.getText().trim());  
  61.             tf.setText("");  
  62.         }  
  63.     }  
  64.     public static void main(String[] args){  
  65.         new test();  
  66.     }  

3、组件和监听接口

       组件和监听接口是分不开的。组件和监听接口在AWT中有很多,我们只对复杂的、难以理解的进行讲解,其他的大家可以通过API自行学习。

       1)、按钮和ActionListener

       ActionListener是由处理ActionEvent事件的监听器对象实现的。当单击按钮、在文本区域中按回车键、选择菜单项、双击列表项都会触发监听器。该接口中的方法为:

       public voidactoinPerformed(ActionEvent e)

       下面是一个实现ActionListener接口的实例:

[java]  view plain  copy
 print ?
  1. public class mytest01extends Frame implements ActionListener{  
  2.     Button b1 = new Button("进入社区");  
  3.     Button b2 = new Button("退出");  
  4.     public mytest01() throws HeadlessException{  
  5.         this.setTitle("论坛");  
  6.         this.add(b1);  
  7.         this.add(b2 , BorderLayout.SOUTH);  
  8.         //为按钮添加监听  
  9.         b1.addActionListener(this);  
  10.         b2.addActionListener(this);  
  11.         this.setBounds(100100200300);  
  12.         this.setVisible(true);  
  13.     }  
  14.     //实现ActionListener接口中的方法  
  15.     public void actionPerformed(ActionEvent e){  
  16.         if (e.getSource() == b1) {  
  17.             System.out.println("进入社区");  
  18.         }  
  19.         else{  
  20.             System.out.println("退出");  
  21.         }  
  22.     }  
  23.     public static void main(String[] args){  
  24.         new mytest01();  
  25.     }  
  26. }  

       2)、运用WindowListener

       WindowListener是处理WindowEvent事件的对象实现的。这个监听器确定了窗口设么时候被打开、关闭、激活、不激活、最小化和最大化。该接口中的方法有:

       >public voidwindowActivated(WindowEvent e)

       >public voidwindowClosed(WindowEvent e)

       >public voidwindowClosing(WindowEvent e)

       >public voidwindowDeactivated(WindowEvent e)

       >public voidwindowDeiconfied(WindowEvent e)

       >public void windowIconified(WindowEvente)

       >public voidwindowOpened(WindowEvent e)

       这些接口很容易但是有点繁琐,下面的实例程序非常清楚:

[java]  view plain  copy
 print ?
  1. public class mytest01extends Frame implements WindowListener{  
  2.     public mytest01() throws HeadlessException{  
  3.        this.setTitle("WindowListener");  
  4.         this.addWindowListener(this);  
  5.         this.setBounds(100100200300);  
  6.         this.setVisible(true);  
  7.     }  
  8.     //实现接口中的方法  
  9.     public void windowOpened(WindowEvent e) {  
  10.         System.out.println("打开");  
  11.     }  
  12.     public void windowClosing(WindowEvent e) {  
  13.         System.out.println("菜单关闭");  
  14.         this.dispose();  
  15.     }  
  16.     public void windowClosed(WindowEvent e) {  
  17.         System.out.println("释放");  
  18.     }  
  19.     public void windowIconified(WindowEvent e){  
  20.         System.out.println("最小化");  
  21.     }  
  22.     public void windowDeiconified(WindowEvente) {  
  23.         System.out.println("最大化");  
  24.     }  
  25.     public void windowActivated(WindowEvent e){  
  26.         System.out.println("激活");  
  27.     }  
  28.     public void windowDeactivated(WindowEvente) {  
  29.         System.out.println("失去焦点");  
  30.     }  
  31.     public static void main(String[] args){  
  32.         new mytest01();  
  33.     }  
  34. }  

       3)、文本组件和TextListener

       文本组件就像把窗口空白处看成记事本一样,它是一个用来写内容的组件。TextListener用来确定何时文本值改变。该接口还可以用到很多地方,其接口方法如下:

       public voidtextValueChanged(TextEvent e)

       下面通过实例来了解TextListener监听事件的使用

[java]  view plain  copy
 print ?
  1. public class mytest01extends Frame implements TextListener{  
  2.     TextArea ta = newTextArea("saasdfgadsfg");  
  3.     public mytest01() throws HeadlessException{  
  4.         this.setTitle("textArea");  
  5.         this.add(ta);  
  6.         //设置文字样式  
  7.         Font f = new Font("宋体", Font.ITALIC+ Font.BOLD, 50);  
  8.         ta.setFont(f);  
  9.         ta.addTextListener(this);  
  10.         ta.setForeground(Color.red);  
  11.         this.setBounds(100100300300);  
  12.         this.setVisible(true);  
  13.     }  
  14.     @Override  
  15.     public void textValueChanged(TextEvent e) {  
  16.         System.out.println(ta.getText());  
  17.     }  
  18.     public static void main(String[] args){  
  19.         new mytest01();  
  20.     }  
  21. }  

       上面的这段程序使用TextListener监听文本的输入、删除等操作,就像记事本一样可以对文本进行修改、录入。

二、Swing界面编程

       随着Java的发展,AWT已经渐渐被淘汰,它已经不能适应发展的需要,不能满足开发功能强大的用户界面的需要。这时Swing出现了,它是建立在AWT之上的组件集,在不同的平台上都能保持组件的界面样式,因此得到了非常广泛的应用。

1、Swing组件库

       在Swing组件中有许多种组件,它们被封装在JFC中,下面我们会对每一种组件进行详细介绍。Swing包很多,但平常用到的只有javax.swing.*和javax.swing.event.*这两个包,其他的很少用到。

       1)、JFC结构

       JFC是Java的基础类,是Java Foundation Classes的缩写形式,封装了一组用于构建图形用户界面的组件和特性。JFC包含了图形用户界面构建中需要用到的顶级容器(Applet、Dialog、Frame)、普通容器(面板、滚动面板、拆分窗格组件、选项卡插U能给个和工具条等)、特殊容器(InternalFrame、Layeredpane、root pane)、基本组件(button , combo box , list , menu , slider , spinner和textfild)等。

       2)、与AWT的区别

       最大的区别在于Swing组件的实现与本地实现无关。Swing组件比AWT组件具有更多的功能。例如在Swing中添加了按钮组件和标签组件,通过继承来更改Swing组件的行为和外观,访问技术等。

2、Jfram窗口容器

       它定义了一个UI程序的框架,是图形程序不可缺少的一部分。它与java.awt.Frame类很类似,它是RootPaneContainer的一种,是顶层的Swing容器。通过继承jframe,所构建的容器就有一些最基本的组件。例如和关闭按钮相对应的容器有一个setDefaultCloseOperation(int operation)方法,这是每个Swing程序都有的,它有四个参数:

       >DO_NOTHING_ON_CLOSE(单击后不管用)

       >HIDE_ON_CLOSE(单击后窗口隐藏)

       >DISPOSE_ON_CLOSE(单击后窗口释放)

       >EXIT_ON_CLOSE(单击后退出)

       Jframe是最重要的顶层容器,其显示效果是一个窗口,带有标题和尺寸重置角标。可以在其中添加按钮、标签等组件。下面是一个应用JFrame类的基本程序:

[java]  view plain  copy
 print ?
  1. public class test extendsJFrame{  
  2.     public test() throws HeadlessException {  
  3.         this.setLayout(new GridLayout(22));  
  4.         this.setBounds(1010600400);  
  5.         this.setVisible(true);  
  6.         //设置当单机窗口的关闭按钮时退出  
  7.        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  8.     }  
  9.     public static void main(String[] args){  
  10.         new test();  
  11.     }  
  12. }  

3、通过Icon接口进行图像操作

       Icon接口用于显示图像。在Icon接口中定义了许多方法,这些方法都是图像应用方面必不可少的。

       在Swing组件中支持图像显示。ImageIcon类用来描述图像。创建Icon对象的方法有3种:

       >new ImageIcon(Image i)

       >new ImageIcon(Stringfilename)(参数为图像文件的名称)

       >new ImageIcon(URL u)(参数为网络地址)

       实现Icon接口的方法也有3种:

       >PaintIcon(Graphics)

       >getIconWidth()(设置图像宽度)

       >getIconHeight()(设置图像长度)

       JLable是一种既可以包含文本又可以包含图像的控件,该控件不能响应用户的动作。创建一个JLable的方法如下:

       Jlabel j1 = new JLable(“a”);

       this.add(j1);

       Icon组件可以实现带有图标的按钮或标签。Icon是一个固定大小的图片,通常用于装饰组件。下面是一个Icon的实例应用:

[java]  view plain  copy
 print ?
  1. public class test extendsJFrame{  
  2.     JLabel j1 = new JLabel("a");  
  3.     JLabel j2 = new JLabel("b");  
  4.     public test() throws HeadlessException {  
  5.         this.setLayout(new GridLayout(22));  
  6.         this.add(j1);  
  7.         this.add(j2);  
  8.         //引入原有图片  
  9.         ImageIcon i1 = new ImageIcon("D:\\桌面\\桌面\\安卓开发工具\\素材\\图标\\a1.png");  
  10.         j1.setIcon(i1);  
  11.         //引入自做图片  
  12.         Icon i2 = new MyIconlmp();  
  13.         j2.setIcon(i2);  
  14.         this.setBounds(1010600400);  
  15.         this.setVisible(true);  
  16.         //设置当单机窗口的关闭按钮时退出  
  17.        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  18.     }  
  19.     //定义一个挥之图片的类  
  20.     class MyIconlmp implements Icon{  
  21.         public void paintIcon(Component c,Graphics g, int x, int y) {  
  22.             for (int i = 0; i < 3; i++) {  
  23.                 g.setColor(new Color(30*i,50*i, 60*i));  
  24.                 g.fillOval(1010 + 100*i, 120,80);  
  25.             }  
  26.         }  
  27.         //设置图片宽度  
  28.         public int getIconWidth() {  
  29.             return 200;  
  30.         }  
  31.         //设置图片高度  
  32.         public int getIconHeight() {  
  33.             return 200;  
  34.         }  
  35.     }  
  36.     public static void main(String[] args){  
  37.         new test();  
  38.     }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值