第一个swing程序

swing是建立在AWT基础之上的,在不同平台能保持组件的界面样式。同样下面来看第一个例子:“Hello, world!”

首先,建立一个Java项目,编写代码如下:


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package swing;

/**
 *
 * @author L
 */
import java.awt.*;//引入AWT包
//import java.awt.*;//引入AWT包
import javax.swing.*;//引入AWT包

public class Swing extends JFrame{

    /**
     * @param args the command line arguments
     */
    //定义组件
    TextArea hello = new TextArea("Hello world!");
    //创建构造方法
    public Swing()
    {
        this.setTitle("第一个swing程序");        //设置窗口名称
        this.setLayout(new GridLayout(4, 4));   //设置一个布局管理器    
        this.add(hello);                        //添加组件
        this.setBackground(Color.red);          //设置窗口背景颜色
        this.setResizable( false);              //设置大小不变
        this.setAlwaysOnTop(true);              //设置总在最上面
        this.setBounds(10, 10, 400, 400);       //设置窗口大小
        this.setVisible(true);                  //设置窗口可见
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    //设置关闭按钮
    }
    public static void main(String[] args) {
        // TODO code application logic here
        new Swing();
    }
    
}

运行程序,效果如下



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值