第一个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();
}
}
运行程序,效果如下
首先,建立一个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();
}
}
运行程序,效果如下
相关文章推荐
- 我的第一个swing小程序,呵呵
- 啊啊啊,我的第一个java swing程序
- 我的第一个Swing程序
- 写出第一个Swing程序
- 第一个用java-swing写的 图形界面程序-扫雷
- 第一个 Swing 程序
- SWING控件学习1:第一个SWING程序
- 第一个Java SWing GUI程序
- SWT学习笔记(3)——第一个程序
- PHP第一个小程序
- 我的第一个JSP(Hello World)程序
- 我的第一个socket程序!
- 我的第一个程序,创建和显示位图
- mono:第一个gtk#程序
- 简易黑白棋--我的第一个程序
- 我的第一个PHP程序--用户登录页面
- 是第一个程序的加强版本,加入了BOSS功能
- 我的第一个socket程序!
- J2ME 3D编程——第一个3D程序(附源代码)
- mono:第一个gnome程序