最近在学习java语言,关于Swing编程在该例子实现的非常不错。整理完给大家参考。更加深入学习java。后面主机mainframe的实现会在另外一篇发表,还未完成。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class login extends JPanel {
static final int WIDTH = 300;
static final int HEIGHT = 150;
JFrame loginframe;
public void add(Component c, GridBagConstraints constraints, int x, int y, int w, int h){
constraints.gridx = x;
constraints.gridy = y;
constraints.gridwidth = w;
constraints.gridheight = h;
add(c,constraints);
}
login(){
//创建界面框架
loginframe = new JFrame("信息管理系统");
//窗口关闭方法
loginframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//创建网格布局的对象
GridBagLayout lay = new GridBagLayout();
setLayout(lay);
//添加当前对象到登录界面
loginframe.add(this, BorderLayout.WEST);
loginframe.setSize(WIDTH, HEIGHT);
//实现居中显示的功能
Toolkit kit = Toolkit.getDefaultToolkit();