JFrame是由这么几部分组成:最底下一层JRootPane,上面是glassPane(一个JPanel)和layeredPane(一个JLayeredPane),而layeredPane又由contentPane(一个JPanel)和menuBar(用来方些菜单之类的)构成。
咱们通常在JFrame上添加组件每每都是加在contentPane上面:下面会使用一个背景案例来讲明。java
1 //设置一个label,用来放背景图片 2 JLabel lalBack=new JLabel(new ImageIcon("images/qwe.jpg")); 3 lalBack.setBounds(0, 0, 400, 300); 4 this.getLayeredPane().add(lalBack, new Integer(Integer.MIN_VALUE)); 5 ((JPanel)getContentPane()).setOpaque(false);
一个LoginFrame类用来放一些简单的组建,ide
1 package com.sxt.test; 2 3 import java.awt.Dimension; 4 import java.awt.Toolkit; 5 import java.awt.event.ActionEvent; 6 import java.awt.event.ActionListener; 7 import java.awt.event.MouseAdapter; 8 import java.awt.event.MouseEvent; 9 import java.awt.event.WindowAdapter; 10 import java.awt.event.WindowEvent; 11 import java.text.SimpleDateFormat; 12 import java.util.Date; 13 14 import javax.swing.ImageIcon; 15 import javax.swing.JButton; 16 import javax.swing.JFrame; 17 import javax.swing.JLabel; 18 import javax.swing.JPanel; 19 import javax.swing.JPasswordField; 20 import javax.swing.JTextField; 21 import javax.xml.crypto.Data; 22 23 public class LoginFrame extends JFrame { 24 private Dimension screenSize;//获取屏幕的分辨率 25 private MyActionLister myActionLister; 26 private JTextField txtName;//用户名框 27 private JPasswordField txtPassword;//密码框 28 public LoginFrame() { 29 30 // super("学生管理系统"); 31 this.myActionLister = new MyActionLister(this); 32 this.setLayout(null); 33 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 34 this.screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 35 int x = (screenSize.width - 500) / 2; 36 int y = (screenSize.height - 300) / 2; 37 this.setBounds(x, y, 400, 300); 38 39 //设置一个label,用来放背景图片 40 JLabel lalBack=new JLabel(new ImageIcon("images/qwe.jpg")); 41 lalBack.setBounds(0, 0, 400, 300); 42 this.getLayeredPane().add(lalBack, new Integer(Integer.MIN_VALUE)); 43 ((JPanel)getContentPane()).setOpaque(false); 44 45 this.addWindowListener(new WindowAdapter() { 46 47 @Override 48 public void windowOpened(WindowEvent e) { 49 Date data = new Date(); 50 SimpleDateFormat simpleDateFormat = new SimpleDateFormat( 51 "yyyy年MM月dd日hh:mm:ss"); 52 String str = simpleDateFormat.format(data); 53 LoginFrame.this.setTitle(str); 54 } 55 }); 56 // lal姓名 57 JLabel lalName = new JLabel("姓名:"); 58 lalName.setBounds(50, 50, 80, 25); 59 this.getContentPane().add(lalName); 60 61 // txt姓名 62 txtName = new JTextField(20); 63 txtName.setBounds(150, 50, 120, 25); 64 this.getContentPane().add(txtName); 65 66 // lal密码 67 JLabel lalPassword = new JLabel("密码:"); 68 lalPassword.setBounds(50, 100, 80, 25); 69 this.getContentPane().add(lalPassword); 70 71 // txt密码 72 txtPassword = new JPasswordField(20); 73 txtPassword.setBounds(150, 100, 120, 25); 74 this.getContentPane().add(txtPassword); 75 76 // btn提交 77 JButton btnLogin = new JButton("提交"); 78 btnLogin.setBounds(100, 150, 80, 20); 79 btnLogin.addActionListener(this.myActionLister); 80 btnLogin.setActionCommand("login"); 81 this.getContentPane().add(btnLogin); 82 83 // btn取消 84 JButton btnCancel = new JButton("取消"); 85 btnCancel.setBounds(200, 150, 80, 20); 86 btnCancel.addActionListener(this.myActionLister); 87 btnCancel.setActionCommand("exit"); 88 this.getContentPane().add(btnCancel); 89 90 //this.addMouseListener(new MouseAdapter() { 91 92 // @Override 93 // public void mouseEntered(MouseEvent e) { 94 // int x = e.getX(); 95 // int y = e.getY(); 96 //txtName.setText("x:" + x + "y:" + y); 97 // } 98 99 // }); 100 this.setVisible(true); 101 } 102 public JTextField getTxtName() { 103 return txtName; 104 } 105 public JPasswordField getTxtPassword() { 106 return txtPassword; 107 } 108 109 110 }
作一个MyActionLister类继承ActionListener用来判断按钮事件与用户的输入信息(如下有两种判断方法)this
1 package com.sxt.test; 2 3 import java.awt.event.ActionEvent; 4 import java.awt.event.ActionListener; 5 6 import javax.swing.Icon; 7 import javax.swing.ImageIcon; 8 import javax.swing.JButton; 9 import javax.swing.JOptionPane; 10 11 public class MyActionLister implements ActionListener { 12 private LoginFrame loginFrame;//链接对象 13 private String name;//存储name 14 private String passWord;//存储password 15 16 //构造方法使两个对象链接 17 public MyActionLister(LoginFrame loginFrame) { 18 this.loginFrame=loginFrame; 19 } 20 21 @Override 22 public void actionPerformed(ActionEvent e) { 23 this.name=this.loginFrame.getTxtName().getText();//得到name 24 char[]pwd=this.loginFrame.getTxtPassword().getPassword(); 25 //this.passWord=this.loginFrame.getTxtPassword().getPassword().toString();//直接加密 26 this.passWord=new String(pwd, 0, pwd.length);//得到密码 没有加密 27 28 if(e.getActionCommand().equals("login")){ 29 System.out.println(this.name+this.passWord); 30 }else if(e.getActionCommand().equals("exit")){ 31 Icon icon=new ImageIcon("images/123.jpg"); 32 int count=JOptionPane.showConfirmDialog(null, "亲,你真的要离开了我?", "想死", 33 JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, 34 icon); 35 if(count==JOptionPane.YES_OPTION){ 36 System.exit(0); 37 }else{ 38 System.out.println("退出!!!"); 39 } 40 } 41 //JButton btnAction = (JButton) e.getSource(); 42 //if (btnAction.getText().equals("提交")) { 43 // System.out.println("login"); 44 //} else if (btnAction.getText().equals("取消")) { 45 // Icon icon=new ImageIcon("images/123.jpg"); 46 // int count=JOptionPane.showConfirmDialog(null, "亲,你真的要离开了我?", "想死", 47 // JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, 48 // icon); 49 // if(count==JOptionPane.YES_OPTION){ 50 // System.exit(0); 51 // }else{ 52 // System.out.println("退出!!!"); 53 // } 54 //} 55 56 } 57 58 }