在java中有ATW和Swing两套组件用于支持图形用户界面编程。
ATW是最早出现的,但是过于依赖操作系统,有很大弊端。
Swing是在ATW之上开发的。完全有java代码编写,不依赖操作系统,可以自己调节外观,使得程序员编写程序更方便。
Swing的GUI(图形用户界面)是由容器和组件组成,容器是放置组件的。组件是独立的可视化的件,比如按钮,输入框等
java中常用的容器:面板 JPanel 框架 JFramer 对话框JDialog 窗口JWindow 滚动窗格JScrollpane
Frame 是带有标题和边界的顶层窗口
常用的组件: 按钮JButton 标签JLabel 文本域JTextField 密码域JPassWordField 文本区JTextArea
多选框JCheckBox 单选按钮JRadioButton 下拉列表框JComboBox 列表框JList
public class LoginView extends
JFrame{ //直接继承JFrame
//创建一个用户名输入框
private JTextField
userInput;
//创建一个密码输入框
private JPasswordField
passWordInput;
//创建字符
//用户名
private JLabel
user;
//密码
private JLabel passWord;
//创建登录点击
private
JButton logIn;
//创建一个图片
private ImageIcon picture =new
ImageIcon("20.jpg");
private JLabel label ;
public
LoginView(){
//设置容器为空布局
this.setLayout(null);
//用户名组件
user
= new JLabel("用户名");
user.setBounds(190, 180, 50,
20);
this.add(user);
//密码组件
passWord = new
JLabel("密码");
passWord.setBounds(190, 240, 50,
20);
this.add(passWord);
//用户名输入框组件
userInput = new
JTextField();
userInput.setBounds(250, 180, 130,
20);
this.add(userInput);
//图片组件
label=new
JLabel(picture);
label.setBounds(180, 40, 260,
100);
this.add(label);
//密码输入框组件
passWordInput = new
JPasswordField();
passWordInput.setBounds(250, 240, 130,
20);
this.add(passWordInput);
//登录组件
logIn =new
JButton("确定");
logIn.setBounds(320, 300, 60,
20);
logIn.addActionListener(new
LoginControl());
this.add(logIn);
//设置标题
this.setTitle("员工信息添加");
//设置窗口的关闭方法
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//设置窗口的大小
this.setSize(600,400);
//设置窗口居中,要写在设置窗口大小的后面
this.setLocationRelativeTo(null);
//将窗口设置为可显示
this.setVisible(true);
}
public
static void main(String[] args){ //主函数
new
LoginView(); //实例化一个窗口
}
}
原文:http://www.cnblogs.com/dssxk/p/3683828.html