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

一、AWT组件开发

1、AWT入门

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

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

public class test extendsFrame{
    //创建构造器
    public test() throws HeadlessException {
        this.setTitle("第一个窗口程序");
        //x , y是距离显示器左上角的距离;w , h是窗口的长宽
        this.setBounds(100, 100, 250, 250);
        //或者使用以下方式代替上面的一句代码
//        this.setLocation(100, 100);
//        this.setSize(250 , 250);
        this.setVisible(true);
    }
    public static void main(String[] str) {
        new test();
    }
}

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

>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对组件的摆放规律,下面是使用一个按钮监听事件来实现按钮的添加:

public class test extendsFrame implements ActionListener{
    int i;
    Button b = new Button("Add");
    public test() throws HeadlessException {
        this.setTitle("第一个窗口程序");
        this.setLayout(new FlowLayout());
        this.add(b);
        b.addActionListener(this);
        this.setBounds(100, 100, 250, 250);
        this.setVisible(true);
    }
    @Override
    public void actionPerformed(ActionEvent e){
        i++;
        Button bi = newButton("Button" + i);
        this.add(bi);
        this.setVisible(true);
    }
    public static void main(String[] str) {
        new test();
    }
}

在本程序中,由于按钮的大小不同,其整体看起来不太整齐,但这更说明了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()方法来选择容器中指定的组件。它的第一个参数是管理组件的容器,第二个参数是标识要显示组件字符串,该字符串与将组件添加到容器时使用的字符串相同。其用法如下:

public class test extendsFrame implements ActionListener{
    Panel p = new Panel();
    Button bf = new Button("First");
    Button bl = new Button("Last");
    Button bn = new Button("next");
    Button bp = newButton("previous");
    Button bg = new Button("Go");
    TextField tf = new TextField();
    //设置CardLayout布局管理器
    CardLayout cl = new CardLayout();
    public test() throws HeadlessException {
        this.setTitle("CardLayout布局管理器");
        this.setLayout(null);
        this.add(p);
        //定义p面板为CardLayout布局管理器
        p.setLayout(cl);
        //为CardLayout布局管理器添加按钮
        for (int i = 1; i <= 10; i++) {
            Button btemp = newButton("Button" + i);
            p.add(btemp, "" + i);
        }
        //为每个组件设置大小位置并添加到容器中
        p.setBounds(10, 40, 100, 100);
        this.add(bf);
        bf.addActionListener(this);
        bf.setBounds(120, 40, 60, 20);
        this.add(bl);
        bl.addActionListener(this);
        bl.setBounds(120, 70, 60, 20);
        this.add(bn);
        bn.addActionListener(this);
        bn.setBounds(120, 100, 60, 20);
        this.add(bp);
        bp.addActionListener(this);
        bp.setBounds(120, 130, 60, 20);
        this.add(bg);
        bg.addActionListener(this);
        bg.setBounds(60, 160, 40, 20);
        this.add(tf);
        tf.setBounds(20, 160, 40, 20);
        //设置窗口的位置和大小
        this.setBounds(200, 200, 210, 220);
        this.setVisible(true);
    }
    @Override
    public void actionPerformed(ActionEvent e){
        if (e.getSource() == bn) {
            cl.next(p);
        }
        if (e.getSource() == bp) {
            cl.previous(p);
        }
        if (e.getSource() == bf) {
            cl.first(p);
        }
        if (e.getSource() == bl) {
            cl.last(p);
        }
        if (e.getSource() == bg) {
            cl.show(p, tf.getText().trim());
            tf.setText("");
        }
    }
    public static void main(String[] args){
        new test();
    }

3、组件和监听接口

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

1)、按钮和ActionListener

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

public voidactoinPerformed(ActionEvent e)

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

public class mytest01extends Frame implements ActionListener{
    Button b1 = new Button("进入社区");
    Button b2 = new Button("退出");
    public mytest01() throws HeadlessException{
        this.setTitle("论坛");
        this.add(b1);
        this.add(b2 , BorderLayout.SOUTH);
        //为按钮添加监听
        b1.addActionListener(this);
        b2.addActionListener(this);
        this.setBounds(100, 100, 200, 300);
        this.setVisible(true);
    }
    //实现ActionListener接口中的方法
    public void actionPerformed(ActionEvent e){
        if (e.getSource() == b1) {
            System.out.println("进入社区");
        }
        else{
            System.out.println("退出");
        }
    }
    public static void main(String[] args){
        new mytest01();
    }
}

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)

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

public class mytest01extends Frame implements WindowListener{
    public mytest01() throws HeadlessException{
       this.setTitle("WindowListener");
        this.addWindowListener(this);
        this.setBounds(100, 100, 200, 300);
        this.setVisible(true);
    }
    //实现接口中的方法
    public void windowOpened(WindowEvent e) {
        System.out.println("打开");
    }
    public void windowClosing(WindowEvent e) {
        System.out.println("菜单关闭");
        this.dispose();
    }
    public void windowClosed(WindowEvent e) {
        System.out.println("释放");
    }
    public void windowIconified(WindowEvent e){
        System.out.println("最小化");
    }
    public void windowDeiconified(WindowEvente) {
        System.out.println("最大化");
    }
    public void windowActivated(WindowEvent e){
        System.out.println("激活");
    }
    public void windowDeactivated(WindowEvente) {
        System.out.println("失去焦点");
    }
    public static void main(String[] args){
        new mytest01();
    }
}

3)、文本组件和TextListener

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

public voidtextValueChanged(TextEvent e)

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

public class mytest01extends Frame implements TextListener{
    TextArea ta = newTextArea("saasdfgadsfg");
    public mytest01() throws HeadlessException{
        this.setTitle("textArea");
        this.add(ta);
        //设置文字样式
        Font f = new Font("宋体", Font.ITALIC+ Font.BOLD, 50);
        ta.setFont(f);
        ta.addTextListener(this);
        ta.setForeground(Color.red);
        this.setBounds(100, 100, 300, 300);
        this.setVisible(true);
    }
    @Override
    public void textValueChanged(TextEvent e) {
        System.out.println(ta.getText());
    }
    public static void main(String[] args){
        new mytest01();
    }
}

上面的这段程序使用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类的基本程序:

public class test extendsJFrame{
    public test() throws HeadlessException {
        this.setLayout(new GridLayout(2, 2));
        this.setBounds(10, 10, 600, 400);
        this.setVisible(true);
        //设置当单机窗口的关闭按钮时退出
       this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public static void main(String[] args){
        new test();
    }
}

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的实例应用:

public class test extendsJFrame{
    JLabel j1 = new JLabel("a");
    JLabel j2 = new JLabel("b");
    public test() throws HeadlessException {
        this.setLayout(new GridLayout(2, 2));
        this.add(j1);
        this.add(j2);
        //引入原有图片
        ImageIcon i1 = new ImageIcon("D:\\桌面\\桌面\\安卓开发工具\\素材\\图标\\a1.png");
        j1.setIcon(i1);
        //引入自做图片
        Icon i2 = new MyIconlmp();
        j2.setIcon(i2);
        this.setBounds(10, 10, 600, 400);
        this.setVisible(true);
        //设置当单机窗口的关闭按钮时退出
       this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    //定义一个挥之图片的类
    class MyIconlmp implements Icon{
        public void paintIcon(Component c,Graphics g, int x, int y) {
            for (int i = 0; i < 3; i++) {
                g.setColor(new Color(30*i,50*i, 60*i));
                g.fillOval(10, 10 + 100*i, 120,80);
            }
        }
        //设置图片宽度
        public int getIconWidth() {
            return 200;
        }
        //设置图片高度
        public int getIconHeight() {
            return 200;
        }
    }
    public static void main(String[] args){
        new test();
    }
}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值