AWT常用组件实战

一 按钮与标签的使用

1 代码

import java.awt.*;
import java.awt.event.*;
/**

* @ClassName: GridFrameDemo

* @Description: 按钮与标签的使用

* @author Superhao+YuHong

* @date 2016年12月22日 下午7:31:32  

*/

import java.awt.*;
import java.awt.event.*;

public class AWTButtonLabel {
    private Frame myFrame;
    private Label headerLabel;
    private Label statusLabel;
    private Panel controlPanel;
    private Font font;

    public AWTButtonLabel() {
        myFrame = new Frame("Java按钮与标签案例");
        myFrame.setLayout(new GridLayout(3, 1));
        myFrame.addWindowListener(new WindowAdapter() {
            public void windowClosing( WindowEvent windowEvent ) {
                System.exit(0);
            }
        });

        font = new Font("楷体", Font.PLAIN, 30);
        headerLabel = new Label();
        headerLabel.setAlignment(Label.CENTER);
        headerLabel.setFont(font);

        statusLabel = new Label();
        statusLabel.setAlignment(Label.CENTER);
        statusLabel.setSize(200, 100);

        controlPanel = new Panel();
        controlPanel.setLayout(new FlowLayout());

        myFrame.add(headerLabel);
        myFrame.add(controlPanel);
        myFrame.add(statusLabel);
        myFrame.setVisible(true);
    }

    private void showButtonDemo() {
        headerLabel.setText("按钮点击动作监控");

        Button okButton = new Button("确定");
        Button submitButton = new Button("提交");
        Button cancelButton = new Button("取消");

        font = new Font("楷体", Font.PLAIN, 20);
        statusLabel.setFont(font);

        okButton.addActionListener(new ActionListener() {
            public void actionPerformed( ActionEvent e ) {
                statusLabel.setText("确定按钮被点击!");
            }
        });

        submitButton.addActionListener(new ActionListener() {
            public void actionPerformed( ActionEvent e ) {
                statusLabel.setText("提交按钮被点击!");
            }
        });

        cancelButton.addActionListener(new ActionListener() {
            public void actionPerformed( ActionEvent e ) {
                statusLabel.setText("取消按钮被点击!");
            }
        });

        controlPanel.add(okButton);
        controlPanel.add(submitButton);
        controlPanel.add(cancelButton);
        myFrame.pack();
        myFrame.setVisible(true);
    }

    public static void main( String[] args ) {
        AWTButtonLabel awtButtonDemo = new AWTButtonLabel();
        awtButtonDemo.showButtonDemo();
    }

}

2 运行

二 文本域使用

1 代码

import java.awt.*;
import java.awt.event.*;

public class TestTextField {
    public static void main( String[] args ) {
        Frame frame = new Frame();
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing( WindowEvent windowEvent ) {
                System.exit(0);
            }
        });

        Label message = new Label("请输入信息");
        TextField text = new TextField(10);
        Panel centerPanel = new Panel();
        Button enter = new Button("确认");
        enter.addActionListener(new ActionListener() {
            public void actionPerformed( ActionEvent e ) {
                message.setText("输入信息为:" + text.getText());
            }
        });
        frame.add(message, BorderLayout.NORTH);
        centerPanel.add(text);
        centerPanel.add(enter);
        frame.add(centerPanel, BorderLayout.CENTER);
        frame.setSize(300, 200);
        frame.setTitle("文本域范例");
        frame.pack();
        frame.setVisible(true);
    }
}

2 运行

三 绘图空间使用

1 代码

import java.awt.*;

public class DrawCircle {
    public DrawCircle() {
        Frame frame = new Frame("DrawCircle");
        DrawCanvas draw = new DrawCanvas();
        frame.add(draw);
        frame.setSize(260, 250);
        frame.setVisible(true);
    }

    public static void main( String[] args ) {
        new DrawCircle();
    }

    class DrawCanvas extends Canvas {
        public void paint( Graphics g ) {   // 创建画布时,默认用此方法画图
            g.setColor(Color.BLUE);
            g.drawOval(10, 50, 80, 80);
            g.setColor(Color.BLACK);
            g.drawOval(80, 50, 80, 80);
            g.setColor(Color.RED);
            g.drawOval(150, 50, 80, 80);
            g.setColor(Color.RED);
            g.setFont(new Font("楷体", Font.BOLD, 20));
            g.drawString("好好学习、天天向上", 45, 200);
        }
    }
}

2 运行

3 参考

绘制椭圆API请参考

http://www.doc88.com/p-7798832222239.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值