十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
import java.awt.*;
海港ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为成都创新互联公司的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18982081108(备注:SSL证书合作)期待与您的合作!
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
class LoginFrm extends JFrame implements ActionListener
{
JLabel lbl1=new JLabel("用户名");
JLabel lbl2=new JLabel("密码");
JTextField txt=new JTextField(15);
JPasswordField pf=new JPasswordField();
JButton btn1=new JButton("确定");
JButton btn2=new JButton("取消");
public LoginFrm()
{
this.setTitle("登陆");
JPanel jp=(JPanel)this.getContentPane();
jp.setLayout(new GridLayout(3,2,10,10));
jp.add(lbl1);jp.add(txt);
jp.add(lbl2);jp.add(pf);
jp.add(btn1);jp.add(btn2);
btn1.addActionListener(this);
btn2.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==btn1)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:MyDB","","");
Statement cmd=con.createStatement();
ResultSet rs=cmd.executeQuery("select * from loginAndpassword where login='"+txt.getText()+"' and password='"+pf.getText()+"'");
if(rs.next())
{
JOptionPane.showMessageDialog(null,"登陆成功!");
}
else
JOptionPane.showMessageDialog(null,"用户名或密码错误!");
} catch(Exception ex){}
if(ae.getSource()==btn2)
{
txt.setText("");
pf.setText("");
}
}
}
public static void main(String arg[])
{
JFrame.setDefaultLookAndFeelDecorated(true);
LoginFrm frm=new LoginFrm();
frm.setSize(400,200);
frm.setVisible(true);
}
}
Java用户登陆这块,主要还是类:
1,边界布局:BorderLayout。他主要分为五个布局,是JFrame(顶层容器),JDialog(创建对话框窗口的类)的默认布局方式。其最多容量为5个组件,超出5个得用其他的。设置方式为:BorderLayout.NORTH;BorderLayout.SOUTH;BorderLayout.CENTER;Borderlayout.CENTER;BorderLayout.LEFT;BorderLayout.RIGHT。
2,流式布局:FlowLayout。布局方式为从左到右,从上到下。是JPanel(轻量级容器)的默认面板布局。
3,网格布局:GridLayout。布局方式为行和列组成的网络。布局方法:setLayout(new
GridLayout(3,2,3,3));其中强两位数字表示三行两列,后两位表示行与行的间距为3,列与列的间距为3.
接着,就接触到JPanel面板。JPanel是非顶层容器,所以,一个界面只能由一个JFrame,但是可以有多个JPanel组件。其默认布局方式为流式布局。在JPanel这块,学到了用户登录界面的设计。从而接触到另外三个组件:文本框组件:JTextField;密码框组件:JPasswordField;标签组件:JLabel;复选框组件:JCheckBox;单选框组件:JRadioButton;按钮组件JButton。
方法一:
采用JOptionPane中的一个非常有用的静态方法 showOptionPane();
源码如下:
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JOptionPane;
import javax.swing.BoxLayout;
import javax.swing.Box;
import javax.swing.BorderFactory;
public class Login1 {
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
static void createAndShowGUI() {
JFrame mainFrame = new JFrame();
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setBounds(250,250,400,300);
mainFrame.setVisible(false);
usernameField = new JTextField(10);
passwordField = new JPasswordField(10);
Object[] options = {"登录","取消"};
int i = JOptionPane.showOptionDialog(null,createLoginPanel(),"登录信息",JOptionPane.DEFAULT_OPTION,JOptionPane.PLAIN_MESSAGE,null,options,options[0]);
if(i==0) {
String username = usernameField.getText();
String password = passwordField.getText();
if(!username.equals("") !password.equals("")) {
mainFrame.getContentPane().add(new JLabel("用户名:"+username+" 密码是:"+password,JLabel.CENTER));
mainFrame.show();
}
else {
JOptionPane.showMessageDialog(null,"用户名和密码不能为空","提示",JOptionPane.WARNING_MESSAGE);
System.exit(1);
}
}
else System.exit(0);
}
static JPanel createLoginPanel() {
JPanel ret = new JPanel();
JPanel usernamePanel = new JPanel();
usernamePanel.add(new JLabel("用户名:",JLabel.RIGHT));
usernamePanel.add(usernameField);
JPanel passwordPanel = new JPanel();
passwordPanel.add(new JLabel("密 码:",JLabel.RIGHT));
passwordPanel.add(passwordField);
Box box = new Box(BoxLayout.Y_AXIS);
box.add(usernamePanel); box.add(passwordPanel);
ret.add(box);
ret.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(new Color(244,144,44)),"填写登录信息"));
return ret;
}
static JFrame mainFrame = null;
static JTextField usernameField = null;
static JPasswordField passwordField = null;
}
运行:
javac -deprecation Login1.java
java Login
(因为有一个过期的API,所以用了 -deprecation 命令)
方法二,使用了两个JFrame类共同实现,第一次显示第一个frame,当点了登录后且操作合法时,第一个窗口就被释放了 dispose();再显示第二个窗口:
源码如下:
import java.awt.Color;
import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;
public class Login2 {
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
static void createAndShowGUI() {
//////////////////////////////////////////////////////////////
loginWindow = new JFrame("登录信息");
loginWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
loginWindow.setBounds(350,350,250,200);
loginWindow.setResizable(false);
JPanel usernamePanel = new JPanel();
usernamePanel.add(new JLabel("用户名:",JLabel.CENTER));
usernamePanel.add(usernameField);
JPanel passwordPanel = new JPanel();
passwordPanel.add(new JLabel("密 码:",JLabel.CENTER));
passwordPanel.add(passwordField);
Box box = new Box(BoxLayout.Y_AXIS);
box.add(usernamePanel); box.add(passwordPanel);
JPanel infoPanel = new JPanel();
infoPanel.add(box);
infoPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(new Color(244,144,44)),"填写登录信息"));
JButton submitButton = new JButton("登录");
JButton cancelButton = new JButton("取消");
submitButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
String username = usernameField.getText();
String password = passwordField.getText();
if(!username.equals("") !password.equals("")) {
loginWindow.dispose();
mainFrame.getContentPane().add(new JLabel("用户名:"+username+" 密码是:"+password,JLabel.CENTER));
mainFrame.setVisible(true);
}
else {
JOptionPane.showMessageDialog(null,"用户名和密码不能为空","提示",JOptionPane.WARNING_MESSAGE);
System.exit(1);
}
}
});
cancelButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
JPanel buttonPanel = new JPanel();
buttonPanel.add(submitButton); buttonPanel.add(cancelButton);
loginWindow.getContentPane().add(infoPanel,BorderLayout.CENTER);
loginWindow.getContentPane().add(buttonPanel,BorderLayout.SOUTH);
loginWindow.getContentPane().add(new JPanel(),BorderLayout.EAST);
loginWindow.getContentPane().add(new JPanel(),BorderLayout.WEST);
loginWindow.setVisible(true);
/////////////////////////////////////////////////////////////////
mainFrame = new JFrame();
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setBounds(250,250,400,300);
mainFrame.setVisible(false);
}
static JFrame loginWindow,mainFrame;
static final JTextField usernameField = new JTextField(10);
static final JPasswordField passwordField = new JPasswordField(10);
}
运行:
javac -deprecation Login2.java
java Login2
你要先学会截图哦,你发的看不清楚,重新写了一个你参考参考!
import java.awt.GridLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class Day30A extends JFrame {
private static final long serialVersionUID = 1L;
private JLabel labelName,labelId,labelPass,labelMoney,labelSelect,labelCar;
private JComboBoxString jcb;
private JPanel jp1,jp2,jp3,jp4,jp5,jp6,jp7;
private ButtonGroup btg;
private JRadioButton jr1,jr2;
Day30A(){
this.setTitle("注册账户");
this.setLayout(new GridLayout(7,1));
this.setSize(300,280);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
init();
this.setVisible(true);
}
private void init() {
String str="卡片类型1,卡片类型2,卡片类型3,卡片类型4,卡片类型5";
jcb=new JComboBox(str.split(","));
labelId=new JLabel("账号: ");
labelName=new JLabel("姓名: ");
labelPass=new JLabel("密码: ");
labelMoney=new JLabel("开户金额:");
labelSelect=new JLabel("存款类型:");
labelCar=new JLabel("卡片类型:");
addFun1();
addFun2();
}
private void addFun2() {
this.add(jp1);
this.add(jp2);
this.add(jp3);
this.add(jp4);
this.add(jp5);
this.add(jp6);
this.add(jp7);
}
private void addFun1() {
jp1=new JPanel();
jp1.add(labelId);
jp1.add(new JTextField(15));
jp2=new JPanel();
jp2.add(labelName);
jp2.add(new JTextField(15));
jp3=new JPanel();
jp3.add(labelPass);
jp3.add(new JTextField(15));
jp4=new JPanel();
jp4.add(labelMoney);
jp4.add(new JTextField(13));
jp5=new JPanel();
jp5.add(labelSelect);
btg=new ButtonGroup();
jr1=new JRadioButton("定期");
jr2=new JRadioButton("活期",true);
btg.add(jr1);
btg.add(jr2);
jp5.add(jr1);
jp5.add(jr2);
jp6=new JPanel();
jp6.add(labelCar);
jp6.add(jcb);
jp7=new JPanel();
jp7.add(new JButton("确定"));
jp7.add(new JButton("取消"));
}
public static void main(String[] args) {
new Day30A();
}
}
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class Test26 {
public static void main(String[] args) {
final String userName = "abc";
final String passwrod = "123";
JFrame jFrame = new JFrame("登陆界面");
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
jFrame.setBounds(((int)dimension.getWidth() - 200) / 2, ((int)dimension.getHeight() - 300) / 2, 200, 150);
jFrame.setResizable(false);
jFrame.setLayout(null);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label1 = new JLabel("姓名");
label1.setBounds(10, 10, 100, 30);
jFrame.add(label1);
JLabel label2 = new JLabel("密码");
label2.setBounds(10, 40, 100, 30);
jFrame.add(label2);
final JTextField text1 = new JTextField();
text1.setBounds(50, 15, 130, 20);
jFrame.add(text1);
final JPasswordField text2 = new JPasswordField();
text2.setBounds(50, 45, 130, 20);
jFrame.add(text2);
JButton button = new JButton("Login");
button.setBounds(10, 75, 170, 40);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(userName.equals(text1.getText()) passwrod.equals(text2.getText())) {
JOptionPane.showMessageDialog(null, "登陆成功误", "提示", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, "错误", "提示", JOptionPane.ERROR_MESSAGE);
text1.setText("");
text2.setText("");
}
}
});
jFrame.add(button);
jFrame.setVisible(true);
}
}
我有一个微信公众号,经常会分享一些Java技术相关的干货,还有一些学习资源。
如果你喜欢我的分享,可以用微信搜索“Java团长”或者“javatuanzhang”关注。