java代码功能

Python020

java代码功能,第1张

Java 是一种编程语言,它本身的作用是通过编写应用程序,帮助人们解决日常工作、生活和学习遇到的问题。

一些常见的java功能代码。如复制文件夹及文件到指定目录,遍历指定盘符所有文件,检索字符串是否符合指定要求。

很简单的应用,为了节省字数,代码注释我就不加了

首先是显示层,LoinWindow:

import java.awt.FlowLayout

import java.awt.GridBagConstraints

import java.awt.GridBagLayout

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 javax.swing.JButton

import javax.swing.JFrame

import javax.swing.JLabel

import javax.swing.JOptionPane

import javax.swing.JPanel

import javax.swing.JTextField

import javax.swing.border.EmptyBorder

public class LoinWindow extends JFrame implements ActionListener, FocusListener {

private JPanel mainPanel, namePanel, btnPanel

private JTextField tfName, tfPsd

private JButton btnLogin, btnCancel

private static final int WIDTH = 300

private static final int HEIGHT = 200

private LoginService service = new LoginService()

public LoinWindow() {

super("登录窗体")

}

public void launch() {

setSize(WIDTH, HEIGHT)

setVisible(true)

setDefaultCloseOperation(EXIT_ON_CLOSE)

GridLayout mainLayout = new GridLayout(2, 1)

mainLayout.setVgap(10)

mainPanel = new JPanel(mainLayout)

GridBagLayout nameLayout = new GridBagLayout()

namePanel = new JPanel(nameLayout)

namePanel.setBorder(new EmptyBorder(10, 10, 10, 10))

JLabel nameLabel = new JLabel("姓名:")

tfName = new JTextField()

JLabel psdLabel = new JLabel("密码:")

tfPsd = new JTextField()

JLabel blank = new JLabel(" ")

namePanel.add(nameLabel)

namePanel.add(tfName)

namePanel.add(blank)

namePanel.add(psdLabel)

namePanel.add(tfPsd)

GridBagConstraints s = new GridBagConstraints()

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)

FlowLayout btnLayout = new FlowLayout()

btnLayout.setAlignment(FlowLayout.CENTER)

btnPanel = new JPanel(btnLayout)

btnLogin = new JButton("确定")

btnCancel = new JButton("取消")

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

public void actionPerformed(ActionEvent e) {

Object source = e.getSource()

if(source == btnCancel) {

System.exit(0)

} else if(source == btnLogin) {

String username = tfName.getText()

String password = tfPsd.getText()

boolean success = service.login(username, password)

if(success) {

warn("成功", "登录成功!")

} else {

warn("失败", "您输入的用户名或密码错误 !")

}

}

}

@Override

public void focusGained(FocusEvent arg0) {

}

@Override

public void focusLost(FocusEvent e) {

Object source = e.getSource()

if(source == tfName) {

String username = tfName.getText()

try {

service.matchUsername(username)

} catch (LoginException e1) {

warn("验证错误", e1.getMessage())

}

} else if(source == tfPsd) {

String password = tfPsd.getText()

try {

service.matchPassword(password)

} catch (LoginException e1) {

warn("验证错误", e1.getMessage())

}

}

}

private void warn(String title, String msg) {

JOptionPane.showMessageDialog(null, msg, title, JOptionPane.INFORMATION_MESSAGE)

}

public static void main(String[] args) {

new LoinWindow().launch()

}

}

然后是模型层:LoginDao

public class LoginDao {

public boolean login(String username, String password) {

if(username.equals("admin") && password.equals("12345")) {

return true

}

return false

}

}

LoginService

import java.util.regex.Pattern

public class LoginService {

private static final Pattern LOGIN_PATTERN = Pattern.compile("[a-zA-Z]+")

private static final Pattern PASSWORD_PATTERN = Pattern.compile("[1-9]+")

private LoginDao dao = new LoginDao()

public boolean matchUsername(String username) throws LoginException {

if(null == username || username.isEmpty()) {

return false

}

if(!LOGIN_PATTERN.matcher(username).matches()) {

throw new LoginException("您输入的用户名不合法,请输入英文!")

}

return true

}

public boolean matchPassword(String password) throws LoginException {

if(null == password || password.isEmpty()) {

return false

}

if(!PASSWORD_PATTERN.matcher(password).matches()) {

throw new LoginException("您输入的密码不合法,请输入数字!")

}

return true

}

public boolean login(String username, String password) {

if(null == username || username.isEmpty()) {

return false

}

if(null == password || password.isEmpty()) {

return false

}

if(!dao.login(username, password)) {

return false

}

return true

}

}

LoginException

public class LoginException extends Exception {

public LoginException(String arg0) {

super(arg0)

}

}

不知道分层设计思想是不是我想的这样

CS结构系统的退出如下:public void init() {\x0d\x0a this.setTitle("用户登录界面")\x0d\x0a this.add(createCenterPane())\x0d\x0a this.setDefaultCloseOperation(this.DO_NOTHING_ON_CLOSE)\x0d\x0a this.setSize(new Dimension(450, 335))\x0d\x0a this.setLocationRelativeTo(null)\x0d\x0a // this.setVisible(true)\x0d\x0a this.addWindowListener(new WindowAdapter() {\x0d\x0a public void windowClosing(WindowEvent e) {\x0d\x0aint choose = JOptionPane.showConfirmDialog(null, "是否要退出登录界面?",\x0d\x0a "系统提示:", JOptionPane.YES_NO_OPTION)\x0d\x0aif (choose == JOptionPane.YES_OPTION) {\x0d\x0a System.exit(1)\x0d\x0a}\x0d\x0a }\x0d\x0a })\x0d\x0a }其中this为JFrame对象。BS结构的退出直接用windows.close()方法就行了!