java代码大全及详解?java开发ai工具
大家好,如果您还对java代码大全及详解不太了解,没有关系,今天就由本站为大家分享java代码大全及详解的知识,包括java开发ai工具的问题都会给大家分析到,还望可以解决大家的问题,下面我们就开始吧!
利用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.Point;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
/**
*一个简单的Swing窗口,输入内容单击“确定”按钮后,在文本域中显示输入的内容。
*单击“取消”按钮,清空页面内容。
*@author yzg
*
*/
public class Test extends JFrame{
private static final long serialVersionUID= 1L;
private JLabel nameLabel;
private JLabel pwdLabel;
private JTextField name;
private JTextField pwd;
public Test(String title){
super(title);
this.getContentPane().setLayout(null);
//下面两行是取得屏幕的高度和宽度
double lx= Toolkit.getDefaultToolkit().getScreenSize().getWidth();
double ly= Toolkit.getDefaultToolkit().getScreenSize().getHeight();
this.setLocation(new Point((int)(lx/ 2)- 150,(int)(ly/ 2)- 200));//设定窗口出现位置
this.setSize(340, 440);//设定窗口大小
}
public void showWin(){
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
//姓名
nameLabel= new JLabel("姓名:");
nameLabel.setBounds(30, 10, 50, 25);
name= new JTextField();
name.setBounds(80, 10, 120, 20);
pwdLabel= new JLabel("密码:");
pwdLabel.setBounds(30, 40, 50, 55);
pwd= new JPasswordField();
pwd.setBounds(80, 40, 120, 50);
//确定按钮
JButton ok= new JButton("确定");
ok.setBounds(50, 190, 60, 25);
ok.addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent e){
if("admin".equals(name.getText())&&"123456".equals(pwd.getText())){
JOptionPane.showMessageDialog(getContentPane(),"登录成功");
}else{
JOptionPane.showMessageDialog(getContentPane(),"登录不成功");
}
}
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
public void mousePressed(MouseEvent e){
}
public void mouseReleased(MouseEvent e){
}
});
//取消按钮
JButton cancel= new JButton("取消");
cancel.setBounds(120, 190, 60, 25);
cancel.addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent e){
}
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
public void mousePressed(MouseEvent e){
}
public void mouseReleased(MouseEvent e){
}
});
this.getContentPane().add(nameLabel);
this.getContentPane().add(pwdLabel);
this.getContentPane().add(name);
this.getContentPane().add(pwd);
this.getContentPane().add(ok);
this.getContentPane().add(cancel);
// this.pack();
this.setVisible(true);
}
/**
*@param args
*/
public static void main(String[] args){
Test reg= new Test("test");
reg.showWin();
}
}
第二个
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
/**
*一个简单的Swing窗口,输入内容单击“确定”按钮后,在文本域中显示输入的内容。
*单击“取消”按钮,清空页面内容。
*@author yzg
*
*/
public class Test extends JFrame{
private static int count=0;
private static final long serialVersionUID= 1L;
public Test(String title){
super(title);
this.getContentPane().setLayout(null);
//下面两行是取得屏幕的高度和宽度
double lx= Toolkit.getDefaultToolkit().getScreenSize().getWidth();
double ly= Toolkit.getDefaultToolkit().getScreenSize().getHeight();
this.setLocation(new Point((int)(lx/ 2)- 150,(int)(ly/ 2)- 200));//设定窗口出现位置
this.setSize(340, 440);//设定窗口大小
}
public void showWin(){
//确定按钮
JButton ok= new JButton("确定");
ok.setBounds(50, 190, 60, 25);
ok.addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent e){
count++;
JOptionPane.showMessageDialog(getContentPane(),count);
}
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
public void mousePressed(MouseEvent e){
}
public void mouseReleased(MouseEvent e){
}
});
this.getContentPane().add(ok);
// this.pack();
this.setVisible(true);
}
/**
*@param args
*/
public static void main(String[] args){
Test reg= new Test("test");
reg.showWin();
}
}
在java的学习中,请高手解释下如下的代码是什么意思
ResultSet rs=conn.executeQuery("select* from tb_manager where manager='"+manager+"'");
这一句执行括号中的sql语句,将得到的结果置给rs。
if(rs.next()),这一句是看rs,也就是结果集中是否有数据,如果有,则进去if代码段,否则输入“您输入的管理员或密码错误!”这句话。
String PWD=request.getParameter("PWD");这一句取得从上一个页面传过来的参数,pwd,也就是你输入的密码。
f(PWD.equals(rs.getString("PWD")))这一句判断你输入的密码和从数据库中取得的密码是否相同。
session.setAttribute("manager",manager);
如果输入的密码和取得的密码相同,就将它放入session中,以便以后使用。
response.sendRedirect("index.jsp");
这一句会将页面转到“index.jsp”中。
out.println("<script language='javascript'>alert('您输入的管理员或密码错误!');如果输入的密码和得到的不同,就输入一句话提示用户。
window.location.href='../index.jsp';</script>");
打开“index.jsp”页面。
catch(Exception e){
out.println("<script language='javascript'>alert('您的操作有误!');window.location.href='../index.jsp';</script>");
}
这几行是捕捉程序发生的异常,这一大段代码上面还有一个try代码块,和catch是配对的,将可能会发生异常的代码放入try块中监视,如果发生异常,就执行catch块中的代码。
OK,本文到此结束,希望对大家有所帮助。