您的位置:首页 > 其它

第一个swing程序

2015-04-07 23:38 176 查看
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();

}

}

运行程序,效果如下

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: