51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 4898|回复: 0
打印 上一主题 下一主题

[转贴] JAVA GUI 开发---简单登录界面

[复制链接]
  • TA的每日心情
    擦汗
    2 小时前
  • 签到天数: 518 天

    连续签到: 5 天

    [LV.9]测试副司令

    跳转到指定楼层
    1#
    发表于 2018-12-29 17:02:59 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

    看起复杂,其实不复杂,嘿嘿!
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPasswordField;
    import javax.swing.JTextField;

    public class LoginJFrameTest {
    private JFrame jframe;
    private JLabel jlabel,jlabel1;
    private GridBagLayout gridbag;
    private GridBagConstraints constraints;
    private JTextField jtfield1;
    private JPasswordField jpfield1;
    private JButton jbutton1,jbutton2,jbutton3;
      
    public LoginJFrameTest(){
      jframe = new JFrame();
      jlabel = new JLabel();
      jlabel1 = new JLabel();
      jtfield1 = new JTextField();
      jpfield1 = new JPasswordField();
      gridbag = new GridBagLayout();
      jbutton1 = new JButton();
      jbutton2 = new JButton();
      jbutton3 = new JButton();
      
      init();
    }

    private void init(){
      jframe.setTitle("全屏幕测试");
      jlabel.setText("用户名:");
      jlabel1.setText("密    码:");
      jbutton1.setText("登    录");
      jbutton2.setText("退    出");
      jbutton3.setText("更改密码");
      jframe.setUndecorated(true);
      jframe.getGraphicsConfiguration().getDevice().setFullScreenWindow(jframe);
      jframe.setLayout(gridbag);
         
         //用户名文本框显示
         constraints = getGridBagConstraints(0,0,1,1,0,0,GridBagConstraints.CENTER,
            GridBagConstraints.NONE,new Insets(10,0,10,0),0,0);
         
         gridbag.setConstraints(jlabel, constraints);
      jframe.add(jlabel);
      
      //用户名输入框显示
         constraints = getGridBagConstraints(1,0,1,1,0,0,GridBagConstraints.CENTER,
            GridBagConstraints.NONE,new Insets(10,0,10,0),100,0);
       
         gridbag.setConstraints(jtfield1, constraints);
         jframe.add(jtfield1);
         
         //密码文本框显示
         constraints = getGridBagConstraints(0,1,1,1,0,0,GridBagConstraints.CENTER,
           GridBagConstraints.NONE,new Insets(10,0,10,0),0,0);
         gridbag.setConstraints(jlabel1, constraints);
         jframe.add(jlabel1);

         //密码输入框显示
      constraints = getGridBagConstraints(1,1,1,1,0,0,GridBagConstraints.CENTER,
           GridBagConstraints.NONE,new Insets(10,0,10,0),100,0);

      gridbag.setConstraints(jpfield1, constraints);
      jframe.add(jpfield1);
      
      //更改密码按钮显示
      constraints = getGridBagConstraints(0,2,1,1,0,0,GridBagConstraints.CENTER,
           GridBagConstraints.NONE,new Insets(10,0,10,0),0,0);

      gridbag.setConstraints(jbutton3, constraints);
      jframe.add(jbutton3);
      
      //登录按钮显示
      constraints = getGridBagConstraints(1,2,1,1,0,0,GridBagConstraints.CENTER,
           GridBagConstraints.NONE,new Insets(10,0,10,0),0,0);

      gridbag.setConstraints(jbutton1, constraints);
      jframe.add(jbutton1);

      //退出按钮显示
      constraints = getGridBagConstraints(2,2,1,1,0,0,GridBagConstraints.CENTER,
           GridBagConstraints.NONE,new Insets(10,0,10,0),0,0);

      gridbag.setConstraints(jbutton2, constraints);
      jframe.add(jbutton2);
      
    }

    private static GridBagConstraints getGridBagConstraints(int gridx,int gridy,int gridwidth,
       int gridheight,double weightx,double weighty,int anchor,int fill,Insets insets,
       int ipadx,int ipady){

       return new GridBagConstraints(gridx, gridy, gridwidth, gridheight, weightx, weighty,
         anchor, fill, insets, ipadx, ipady);
    }

    public void showMe(){
      jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      jframe.setVisible(true);
    }

    public static void main(String[] args) {
      new LoginJFrameTest().showMe();
    }
    }


    参数说明:

           gridx=0;   //放在第0列
           gridy=0;   //放在第0行
           gridwidth=1;   //组件的长度
           gridheight=1;   //组件的高度
           weightx=10;   //组件根据窗口的大小而变化,系数为10,1
           weighty=1;   
           anchor=GridBagConstraints.CENTER;   //组件大于放置空间位置时,将组件放置在中心
           fill=GridBagConstraints.HORIZONTAL;   //显示区域变化时,设置组件是否变化。
           inset=new Insets(0,0,0,0);   //设置组件间上下左右间距
           ipadx=0;   //设置组件内的间距
           ipady=0;  

    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /2 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-11-1 11:30 , Processed in 0.063004 second(s), 23 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表