package com.ycty.totlosystem.gui.test;java
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;ide
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
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;
private JPanel jpanel;
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();
}
/**
* init()初始化並顯示界面
*/
private void init(){
jframe.setTitle("全屏幕測試");
jpanel = new JPanel(){
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
ImageIcon img = new ImageIcon(ImgJframeTest.class.getResource("\\img\\2.jpg"));
img.paintIcon(this, g, 0, 0);
}
};
jlabel.setText("用戶名:");
jlabel1.setText("密 碼:");
jbutton1.setText("登 錄");
jbutton2.setText("退 出");
jbutton3.setText("更改密碼");
//設置JFrame爲全屏
jframe.setUndecorated(true);
jframe.getGraphicsConfiguration().getDevice().setFullScreenWindow(jframe);
//設置JPanel爲透明,且使用GridBagLayout佈局管理器
jpanel.setOpaque(true);
jpanel.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);
jpanel.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);
jpanel.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);
jpanel.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);
jpanel.add(jpfield1);
//更改密碼按鈕顯示
constraints = getGridBagConstraints(0,2,1,1,0,0,GridBagConstraints.CENTER,
GridBagConstraints.NONE,new Insets(10,0,10,0),0,0);ui
gridbag.setConstraints(jbutton3, constraints);
jpanel.add(jbutton3);
//登陸按鈕顯示
constraints = getGridBagConstraints(1,2,1,1,0,0,GridBagConstraints.CENTER,
GridBagConstraints.NONE,new Insets(10,0,10,0),0,0);this
gridbag.setConstraints(jbutton1, constraints);
jpanel.add(jbutton1);ip
//退出按鈕顯示
constraints = getGridBagConstraints(2,2,1,1,0,0,GridBagConstraints.CENTER,
GridBagConstraints.NONE,new Insets(10,0,10,0),0,0);get
gridbag.setConstraints(jbutton2, constraints);
jpanel.add(jbutton2);
jframe.add(jpanel);
}
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){it
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();
}
}io