java新手代码大全(2fa代码生成器)
大家好,今天给各位分享java新手代码大全的一些知识,其中也会对2fa代码生成器进行解释,文章篇幅可能偏长,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在就马上开始吧!
新手要敲好Java代码有什么技巧
对于Java新手来说,很棘手的一个问题,就是如何才能够敲好代码?很多Java新手在刚开始接触Java编程的知识的时候,理论才刚上手,就希望自己能够立马在计算机上敲简单的代码,尝试过的朋友都知道,这有点急于求成了,是个很难办到的问题。
Java新手如何才能够敲好代码?作为一个Java新手,你经常思考这个问题吗?今天就跟北大青鸟小编一起来好好的想想对策。首先,一定要明确,Java代码不是你想敲写,就可以敲写的出来的,一定是在掌握一定的Java理论知识之后,才可以开启的,所以想要敲好Java代码,一定要重视Java理论知识的学习。
想必很多朋友都知道,Java新手要敲好Java代码,除了苦练,没有什么更加的技巧。但是苦练也是有技巧的,比如你已经熟练的一类Java代码,你再继续敲也很难有大的突破,但是你不熟练的Java代码,则会成为你的软肋,所以敲Java代码一定是得有方法的,而不是胡乱的敲写,没有任何想法的练习。
对于Java新手来说,代码不敲不行,当代码敲到一定的境界之后,很多Java知识你自然而然的就懂了,所以每天的学习时间一定要多敲代码,但是Java理论知识也不要落下。所以,对于Java新手来说,北京IT培训认为要敲好Java代码,的确不是一件容易的事情。
利用java编写代码实现如下功能,需要全部代码
很简单的应用,为了节省字数,代码注释我就不加了
首先是显示层,LoinWindow:
importjava.awt.FlowLayout;
importjava.awt.GridBagConstraints;
importjava.awt.GridBagLayout;
importjava.awt.GridLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.FocusEvent;
importjava.awt.event.FocusListener;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JOptionPane;
importjavax.swing.JPanel;
importjavax.swing.JTextField;
importjavax.swing.border.EmptyBorder;
publicclassLoinWindowextendsJFrameimplementsActionListener,FocusListener{
privateJPanelmainPanel,namePanel,btnPanel;
privateJTextFieldtfName,tfPsd;
privateJButtonbtnLogin,btnCancel;
privatestaticfinalintWIDTH=300;
privatestaticfinalintHEIGHT=200;
privateLoginServiceservice=newLoginService();
publicLoinWindow(){
super("登录窗体");
}
publicvoidlaunch(){
setSize(WIDTH,HEIGHT);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
GridLayoutmainLayout=newGridLayout(2,1);
mainLayout.setVgap(10);
mainPanel=newJPanel(mainLayout);
GridBagLayoutnameLayout=newGridBagLayout();
namePanel=newJPanel(nameLayout);
namePanel.setBorder(newEmptyBorder(10,10,10,10));
JLabelnameLabel=newJLabel("姓名:");
tfName=newJTextField();
JLabelpsdLabel=newJLabel("密码:");
tfPsd=newJTextField();
JLabelblank=newJLabel("");
namePanel.add(nameLabel);
namePanel.add(tfName);
namePanel.add(blank);
namePanel.add(psdLabel);
namePanel.add(tfPsd);
GridBagConstraintss=newGridBagConstraints();
s.fill=GridBagConstraints.BOTH;
s.gridwidth=1;
s.weightx=0;
s.weighty=0;
nameLayout.setConstraints(nameLabel,s);
s.gridwidth=0;
s.weightx=1;
s.weighty=0;
nameLayout.setConstraints(tfName,s);
s.gridwidth=0;
s.weightx=4;
s.weighty=0;
nameLayout.setConstraints(blank,s);
s.gridwidth=1;
s.weightx=0;
s.weighty=0;
nameLayout.setConstraints(psdLabel,s);
s.gridwidth=3;
s.weightx=1;
s.weighty=0;
nameLayout.setConstraints(tfPsd,s);
FlowLayoutbtnLayout=newFlowLayout();
btnLayout.setAlignment(FlowLayout.CENTER);
btnPanel=newJPanel(btnLayout);
btnLogin=newJButton("确定");
btnCancel=newJButton("取消");
btnPanel.add(btnLogin);
btnPanel.add(btnCancel);
btnCancel.addActionListener(this);
btnLogin.addActionListener(this);
mainPanel.add(namePanel);
mainPanel.add(btnPanel);
setContentPane(mainPanel);
tfName.addFocusListener(this);
tfPsd.addFocusListener(this);
pack();
setSize(WIDTH,HEIGHT);
setLocationRelativeTo(null);
}
@Override
publicvoidactionPerformed(ActionEvente){
Objectsource=e.getSource();
if(source==btnCancel){
System.exit(0);
}elseif(source==btnLogin){
Stringusername=tfName.getText();
Stringpassword=tfPsd.getText();
booleansuccess=service.login(username,password);
if(success){
warn("成功","登录成功!");
}else{
warn("失败","您输入的用户名或密码错误!");
}
}
}
@Override
publicvoidfocusGained(FocusEventarg0){
}
@Override
publicvoidfocusLost(FocusEvente){
Objectsource=e.getSource();
if(source==tfName){
Stringusername=tfName.getText();
try{
service.matchUsername(username);
}catch(LoginExceptione1){
warn("验证错误",e1.getMessage());
}
}elseif(source==tfPsd){
Stringpassword=tfPsd.getText();
try{
service.matchPassword(password);
}catch(LoginExceptione1){
warn("验证错误",e1.getMessage());
}
}
}
privatevoidwarn(Stringtitle,Stringmsg){
JOptionPane.showMessageDialog(null,msg,title,JOptionPane.INFORMATION_MESSAGE);
}
publicstaticvoidmain(String[]args){
newLoinWindow().launch();
}
}然后是模型层:LoginDao
publicclassLoginDao{
publicbooleanlogin(Stringusername,Stringpassword){
if(username.equals("admin")&&password.equals("12345")){
returntrue;
}
returnfalse;
}
}LoginService
importjava.util.regex.Pattern;
publicclassLoginService{
privatestaticfinalPatternLOGIN_PATTERN=Pattern.compile("[a-zA-Z]+");
privatestaticfinalPatternPASSWORD_PATTERN=Pattern.compile("[1-9]+");
privateLoginDaodao=newLoginDao();
publicbooleanmatchUsername(Stringusername)throwsLoginException{
if(null==username||username.isEmpty()){
returnfalse;
}
if(!LOGIN_PATTERN.matcher(username).matches()){
thrownewLoginException("您输入的用户名不合法,请输入英文!");
}
returntrue;
}
publicbooleanmatchPassword(Stringpassword)throwsLoginException{
if(null==password||password.isEmpty()){
returnfalse;
}
if(!PASSWORD_PATTERN.matcher(password).matches()){
thrownewLoginException("您输入的密码不合法,请输入数字!");
}
returntrue;
}
publicbooleanlogin(Stringusername,Stringpassword){
if(null==username||username.isEmpty()){
returnfalse;
}
if(null==password||password.isEmpty()){
returnfalse;
}
if(!dao.login(username,password)){
returnfalse;
}
returntrue;
}
}LoginException
publicclassLoginExceptionextendsException{
publicLoginException(Stringarg0){
super(arg0);
}
}不知道分层设计思想是不是我想的这样
急求一段简单的java源代码(用户名、密码操作界面)
下面的程序可以直接通过编译运行,自己寻找要用到的代码段。
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class UserLogin extends JPanel implements ActionListener{
JTextField userjt=null;//用户输入框
JPasswordField pwdjt=null;
JTextField sysUserjt=null;//系统显示用户名输入框
JTextField sysPwdjt=null;
public UserLogin(){
super(new GridLayout(1,2));
JPanel userPanel=new JPanel();//用户界面,左边
userPanel.setLayout(new BoxLayout(userPanel,BoxLayout.Y_AXIS));
this.add(userPanel);
JPanel userUpPanel=new JPanel();//用户界面上半部分
userPanel.add(userUpPanel);
JPanel userDownPanel=new JPanel();//用户界面下半部分
userPanel.add(userDownPanel);
JPanel sysPanel=new JPanel();//系统界面,右边
sysPanel.setLayout(new BoxLayout(sysPanel,BoxLayout.Y_AXIS));
this.add(sysPanel);
JPanel sysUserPanel=new JPanel();//系统界面上半部分
sysPanel.add(sysUserPanel);
JPanel sysPwdPanel=new JPanel();//系统界面下半部分
sysPanel.add(sysPwdPanel);
userjt=new JTextField(5);
userjt.setText("用户名");
userUpPanel.add(userjt);
pwdjt=new JPasswordField(5);
pwdjt.setText("密码");
pwdjt.setEchoChar('\0');
userDownPanel.add(pwdjt);
JLabel sysUserjl=new JLabel("用户名为:");
sysUserPanel.add(sysUserjl);
sysUserjt=new JTextField(5);
sysUserPanel.add(sysUserjt);
JLabel sysPwdjl=new JLabel("密码为:");
sysPwdPanel.add(sysPwdjl);
sysPwdjt=new JTextField(5);
sysPwdPanel.add(sysPwdjt);
userjt.addActionListener(this);
pwdjt.addActionListener(this);
userjt.addFocusListener(new FocusListener(){
public void focusGained(FocusEvent e){
if(userjt.getText().equals("用户名"))
userjt.setText("");
}
public void focusLost(FocusEvent e){
if(userjt.getText().equals(""))
userjt.setText("用户名");
}});
pwdjt.addFocusListener(new FocusListener(){
public void focusGained(FocusEvent e){
if(new String(pwdjt.getPassword()).equals("密码")){
pwdjt.setText("");
pwdjt.setEchoChar('*');
}
}
public void focusLost(FocusEvent e){
if(new String(pwdjt.getPassword()).equals("")){
pwdjt.setText("密码");
pwdjt.setEchoChar('\0');
}
}});
}
public void actionPerformed(ActionEvent e){
if(e.getSource().equals(userjt)){
pwdjt.requestFocus();
}else{
if(new String(pwdjt.getPassword()).equals("")||userjt.getText().equals("")||userjt.getText().equals("用户名")) return;
sysUserjt.setText(userjt.getText());
sysPwdjt.setText(new String(pwdjt.getPassword()));
try{
writetoFile();
} catch(IOException e1){
System.out.println("写入文件发生异常!");
e1.printStackTrace();
}
}
}
private void writetoFile() throws IOException{
File f=new File("User_Psd.txt");
// if(!f.exists()) f.createNewFile();
RandomAccessFile accessFile=new RandomAccessFile(f,"rw");
accessFile.seek(accessFile.length());
accessFile.write(("user:"+userjt.getText()+"\r\npassword:"+new String(pwdjt.getPassword())+"\r\n\r\n").getBytes());
}
public static void main(String args[]){
JFrame jf=new JFrame("用户登陆模块测试");
jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
jf.add(new UserLogin());
jf.setBounds(400,300,280,150);
jf.setVisible(true);
}
}
关于本次java新手代码大全和2fa代码生成器的问题分享到这里就结束了,如果解决了您的问题,我们非常高兴。