
正文
java添加功能代码 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);
}
}
不知道分层设计思想是不是我想的这样
相关问答
Q1: java语言如何写增加功能
可以直接通过Calendar类的add方法进行实现,直接在DAY属性上加上指定的天数就可以了。
举例:
Calendar calendar = Calendar.newInstance();//创建一个实例
cd.set(2010,5,24,14,33,22);//实例化一个Calendar。 年、月、日、时、分、秒
calendar.add(Calendar.DAY,1);//给当前日期加上指定天数,这里加的是1天
System.out.println(calendar.getTime());
Q2: java--在main方法中添加代码完成要求的功能
public class ex4 {
public static void main(String args[]) {
// No.1 添加语句构造一个CFushu类的对象c1表示复数3+6i,构造一个CFushu类的对象c2表示复数7+6i,
CFushu c1=new CFushu(3,6);
CFushu c2=new CFushu(7,6);
// No.2 添加语句构造CFushu类对象c3复制为c1和c2的和(调用CFushu类中的add方法求c1与c2的和)
CFushu c3=c1.add(c2);
// No.3 添加语句显示输出c3的实部和虚部值,与实际运算的c1与c2和值比较是否吻合
System.out.println("c3的实部="+c3.m_Real+" c3虚部 ="+c3.m_Image );
// No.4 添加语句显示输出c1和c2的实部和虚部,观察其变化,分析原因
System.out.println("c1的实部="+c1.m_Real+" c1虚部 ="+c1.m_Image );
System.out.println("c2的实部="+c2.m_Real+" c2虚部 ="+c2.m_Image );
}
}
class CFushu// 描述复数
{
double m_Real;// 表示复数的实部
double m_Image;// 表示复数的虚部
public CFushu(double real, double image)// 定义构造函数
{
m_Real = real;
m_Image = image;
}
CFushu add(CFushu fushu)// 实现当前复数和参数复数fushu的加法
{
fushu.m_Real += m_Real;// 和值结果保存在fushu对象中
fushu.m_Image += m_Image;
return this;
}
}
结果:
c3的实部=3.0 c3虚部 =6.0
c1的实部=3.0 c1虚 =6.0
c2的实部=10.0 c2虚 =12.0
为什么明明c3是c1和c2的和,应该是10,12,但c3却和c1相等,而c2变成了c1和自身的和,分析:
c3 = c1.add(c2);
关键在这里是c1调用的add(c2);方法,而传进去的是c2的引用。在来看
这个方法的实现
CFushu add(CFushu fushu)// 实现当前复数和参数复数fushu的加法
{
fushu.m_Real += m_Real;// 和值结果保存在fushu对象中
fushu.m_Image += m_Image;
return this;
}
fushu.m_Real += m_Real;//fushu.m_Real 是c2的属性,+= m_Real 是c1的属性
这样就将c1的实部和c2的实部相加,得到的值再付给c2的实部。由于传的是c2的引用,因此通过c2的引用改变了c2实部的值,而c1的实部没有改变,仍是3。就如同注释中说的“和值结果保存在fushu对象中”这个fushu对象就是c2对象。虚部同理。
return时 ,返回的是this, 因为是c1.add()也就是说,这个this是c1对象,而c1对象没有任何变化,因此返回给c3的是c1对象。
所以出现上述情况。
如果将add()改为 c3 = c2.add(c1);
结果为:
c3的实部=3.0 c3虚部 =6.0
c1的实部=10.0 c1虚 =12.0
c2的实部=3.0 c2虚 =6.0
道理同上。
如果楼主想达到的目的是c1,c2本身值不变,c3为他们的和,只需将add()改为
CFushu add(CFushu fushu)// 实现当前复数和参数复数fushu的加法
{
return new CFushu(fushu.m_Real+m_Real,fushu.m_Image+m_Image);
}
这样没有改变传进去的对象值,返回出的新对像的值是c1和c2的和。
累!
Q3: java 开发 怎么实现用户添加功能?
1:在网页中编写一个按钮,比如添加用户
form action="xxx.jsp"/
input type="submit" value="添加用户"/
/form
2:添加mysql的jdbc驱动jar包
3:编写一个数据库操作辅助类,使用的sql语句,差不多如下
insert into tb_user values(?,?,?)等,你写的太简单,详细点的要求可以发邮件到lixihara@126.com,我会帮你完成哦
Q4: 请给我的JAVA程序添加一个自动播放图片功能
//其余java添加功能代码的类在qq上给你..
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.util.Properties;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.LookAndFeel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.filechooser.FileNameExtensionFilter;
public class ImageFrame extends JFrame implements ActionListener, MouseListener {
/**
*
*/
private static final long serialVersionUID = -5241799989073556019L;
static Properties property = new Properties();
static {
Class iClass;
try {
iClass = Class.forName("sun.awt.im.InputMethodContext");
Field field = iClass.getDeclaredField("belowTheSpotInputRequested");
AccessibleObject.setAccessible(new AccessibleObject[] { field },
true);
field.setBoolean(null, false);
} catch (Exception e) {
}
}
public JPanel imagePanel;
public JPanel buttonPanel;
public JPanel statePanel;
public JLabel imageLabel;
public JLabel stateLabel;
public JButton before;
public JButton play;
public JButton stop;
public JButton next;
public JButton bigger;
public JButton smaller;
static JTextArea area = new JTextArea(5, 60);
JScrollPane scroll = new JScrollPane(area);
public File picFile;
public Image img;
public ImageIcon imageIcon;
public String fileParent;
FileNameExtensionFilter imageFilter = new FileNameExtensionFilter(null,
"jpeg", "jpg", "png", "gif");
JFileChooser imageChooser = new JFileChooser();
public File filePath[];
public File imagePath[];
public static Picture images[];
public int imageFileNumber = 0;
public static int locationImage = 0;
public static ImageFrame myImageFrame;
private PlayTimer playTimer;
public Image[] imageOffer;
public int scale = 8;
public JScrollPane imageScrollPane;
public ImageFrame(String picPath) {
picFile = new File(picPath);
fileParent = picFile.getParent();
filePath = (new File(fileParent)).listFiles();
try {
img = javax.imageio.ImageIO.read(picFile);
} catch (IOException ex) {
ex.printStackTrace();
}
imageIcon = new ImageIcon(img);
imageLabel = new JLabel(imageIcon);
imagePanel = new JPanel(new BorderLayout());
imagePanel.setBackground(Color.black);
//R:238 G:243 B:250
//imagePanel.setBackground(new Color(238,243,250));
stateLabel = new JLabel();
final String windows = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
try {
UIManager.setLookAndFeel(windows);
SwingUtilities.updateComponentTreeUI(this);// 更新控件的外观
} catch (Exception e) {
e.printStackTrace();
}
setLayout(new BorderLayout());
setTitle("PowerSee 图片查看器");
setIconImage(new ImageIcon("icon/powerSee.png").getImage());
setSize(800, 600);
this.setMinimumSize(new Dimension(600, 400));
setVisible(true);
setExtendedState(JFrame.MAXIMIZED_BOTH);
buttonPanel = makeButtonPanel();
area.setLineWrap(true);
// area.setWrapStyleWord(true);
area.addMouseListener(this);
// area.setFont(new Font(area.getFont().getFamily(), Font.PLAIN,
// 18));
LookAndFeel.installColorsAndFont(area, "Label.background",
"Label.foreground", "TextArea.font");
area.setBorder(BorderFactory.createTitledBorder("此处可添加照片描述:"));
area.setLineWrap(true);
area.setWrapStyleWord(true);
area.getDocument().addDocumentListener(new SWING_OnValueChanged());
/*
area.setText("");
area.setText("像素大小java添加功能代码: " + imageIcon.getIconWidth() + "*"
+ imageIcon.getIconHeight() + " 文件位置: " + picFile.toString()
+ " 文件大小: " + picFile.length() / 1024 + "KB");
*/
/*
area.getDocument()
.addDocumentListener(new SWING_OnValueChanged());
*/
// area.setBackground(new Color() );
Font font = new Font("宋体", Font.PLAIN, 17);
area.setFont( font);
// area.setEditable(false);
statePanel = new JPanel();
//:36 G:53 B:71R:192 G:192 B:196
statePanel.setBackground(new Color(192,192,196));
stateLabel.setText("像素大小: " + imageIcon.getIconWidth() + "*"
+ imageIcon.getIconHeight() + " 文件位置: " + picFile.toString()
+ " 文件大小: " + picFile.length() / 1024 + "KB");
//stateLabel;
imagePanel.add(imageLabel, BorderLayout.CENTER);
imageScrollPane = new JScrollPane(imagePanel);
add(imageScrollPane, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.NORTH);
//statePanel.add(stateLabel);
//statePanel.setBackground(Color.black);
statePanel.add(area, BorderLayout.CENTER);//scroll
add(statePanel, BorderLayout.SOUTH);
imagePanel.repaint();
imagePanel.validate();
Load();
}
//对于此法可借鉴,返回一个panel竟然......
public JPanel makeButtonPanel() {
JPanel aButtonPanel = new JPanel();
before = new JButton("前一张");
next = new JButton("下一张");
play = new JButton("自动播放");
stop = new JButton("停止");
bigger = new JButton("放大");
smaller = new JButton("缩小");
stop.setEnabled(false);
before.addActionListener(this);
next.addActionListener(this);
play.addActionListener(this);
stop.addActionListener(this);
bigger.addActionListener(this);
smaller.addActionListener(this);
aButtonPanel.add(before);
aButtonPanel.add(play);
aButtonPanel.add(stop);
aButtonPanel.add(next);
aButtonPanel.add(smaller);
aButtonPanel.add(bigger);
return aButtonPanel;
}
/*
* //imageFileNumber 个图像文件
//filePath.length 个文件
//filePath 以某个文件夹的所有文件为元素的数组, 以File类型为元素
//imagePath 以所有图像文件为元素的数组,仍然以File类型为元素
*/
public void Load() {
imageChooser.setFileFilter(imageFilter);
//System.out.println(filePath.length+"----");
//filePath.length,也就是Image这个文件夹里有多少个文件.......所有种类的文件....!!
for (int i = 0; i filePath.length; i++) {
if (!filePath[i].isDirectory() imageFilter.accept(filePath[i])) {
imageFileNumber++;
} else {
filePath[i] = null;
}
}
//imageFileNumber 个图像文件
//filePath.length 个文件
//filePath 以某个文件夹的所有文件为元素的数组, 以File类型为元素
//imagePath 以所有图像文件为元素的数组,仍然以File类型为元素
imagePath = new File[imageFileNumber];
images = new Picture[imageFileNumber];
imageFileNumber = 0;
for (int i = 0; i filePath.length; i++) {
if (filePath[i] != null) {
imagePath[imageFileNumber++] = filePath[i];
}
}
imageFileNumber--;
for (int i = 0; i imagePath.length; i++) {
if (imagePath[i] == picFile) {
locationImage = i;
}
}
for (int i = 0; i imagePath.length; i++) {
initTxt(imagePath[i], i);
}
}
public void initTxt(File picFile, int i) {
try {
property.load(new FileInputStream("a.properties"));
/*
String txt = property.getProperty(picFile.getParent() + "."
+ picFile.getName());
*/
images[i] = new Picture(imagePath[i].getParent() + "."
+ imagePath[i].getName(), "");
} catch (Exception e) {
}
//初始化图片的时候,要从资源文件里读取所有文件的txt信息,可能花费时间较长
}
/*
*
//imageFileNumber 个图像文件
//filePath.length 个文件
//filePath 以某个文件夹的所有文件为元素的数组, 以File类型为元素
//imagePath 以所有图像文件为元素的数组,仍然以File类型为元素
*/
public void Before() {
scale = 8;
if (--locationImage 0) {
locationImage = imageFileNumber;
}
try {
img = javax.imageio.ImageIO.read(imagePath[locationImage]);
} catch (IOException ex) {
ex.printStackTrace();
}
imageIcon.setImage(img);
imageLabel.setIcon(imageIcon);
picFile = imagePath[locationImage];
/*
String text = "像素大小: " + imageIcon.getIconWidth() + "*"
+ imageIcon.getIconHeight() + " 文件位置: " + picFile.toString()
+ " 文件大小: " + picFile.length() / 1024 + "KB";
*/
//System.out.println(picFile.toString());
//Picture p = new Picture(picFile, text);
try {
Properties property = new Properties();
property.load(new FileInputStream("a.properties"));
String areaTxt = "";
areaTxt = property.getProperty(picFile.getParent() + "."
+ picFile.getName());
area.setText("");
area.setText(areaTxt);
} catch (Exception e) {
}
imagePanel.repaint();
imagePanel.validate();
imageScrollPane.repaint();
imageScrollPane.validate();
}
/*
* //imageFileNumber 个图像文件
//filePath.length 个文件
//filePath 以某个文件夹的所有文件为元素的数组, 以File类型为元素
//imagePath 以所有图像文件为元素的数组,仍然以File类型为元素
*/
public void Next() {
scale = 8;
if (++locationImage imageFileNumber) {
locationImage = 0;
}
try {
img = javax.imageio.ImageIO.read(imagePath[locationImage]);
} catch (IOException ex) {
ex.printStackTrace();
}
imageIcon.setImage(img);
imageLabel.setIcon(imageIcon);
picFile = imagePath[locationImage];
try {
Properties property = new Properties();
property.load(new FileInputStream("a.properties"));
String areaTxt = "";
areaTxt = property.getProperty(picFile.getParent() + "."
+ picFile.getName());
area.setText("");
area.setText(areaTxt);
} catch (Exception e) {
}
imagePanel.repaint();
imagePanel.validate();
imageScrollPane.repaint();
imageScrollPane.validate();
}
public void Bigger() {
ImageIcon icon = imageIcon;
if (scale = 17) {
ImageIcon tmpicon = new ImageIcon(new DrawImage(icon.getImage(),
icon.getIconWidth() / 8, icon.getIconHeight() / 8, ++scale)
.getImage());
imageLabel.setIcon(tmpicon);
} else {
return;
}
}
public void Smaller() {
ImageIcon icon = imageIcon;
if (scale 1) {
ImageIcon tmpicon = new ImageIcon(new DrawImage(icon.getImage(),
icon.getIconWidth() / 8, icon.getIconHeight() / 8, --scale)
.getImage());
imageLabel.setIcon(tmpicon);
} else {
return;
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(before)) {
Before();
}
if (e.getSource().equals(next)) {
Next();
}
if (e.getSource().equals(play)) {
stop.setEnabled(true);
before.setEnabled(false);
next.setEnabled(false);
bigger.setEnabled(false);
smaller.setEnabled(false);
play.setEnabled(false);
playTimer = new PlayTimer(this);
playTimer.start();
}
if (e.getSource().equals(stop)) {
stop.setEnabled(false);
before.setEnabled(true);
next.setEnabled(true);
bigger.setEnabled(true);
smaller.setEnabled(true);
play.setEnabled(true);
playTimer.cancel();
}
if (e.getSource().equals(bigger)) {
Bigger();
}
if (e.getSource().equals(smaller)) {
Smaller();
}
}
public void mouseClicked(MouseEvent arg0) {
// 鼠标点击
area.setEditable(true);
// pane.add(scroll, BorderLayout.CENTER);
}
public void mouseExited(MouseEvent arg0) {
// 鼠标离开区域
area.setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); //鼠标离开Text区后恢复默认形态
area.setBackground(null);
//保存信息
//area.setEditable(false);
}
public void mousePressed(MouseEvent arg0) {
}
public void mouseReleased(MouseEvent arg0) {
// 鼠标释放
}
public void mouseEntered(MouseEvent arg0) {
// 鼠标释进入区域
area.setCursor(new Cursor(Cursor.TEXT_CURSOR)); //鼠标进入Text区后变为文本输入指针
area.setBackground(new Color(255, 255, 170));
}
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
try {
} catch (Exception ex) {
}
System.exit(0);
}
}
public static void main(String a[]) {
new ImageFrame("Image/p1.jpg");
}
}
class Picture {
String text;
String picFile;
Picture(String string, String text) {
this.picFile = string;
this.text = text;
}
String getText() {
return this.text;
}
String getFilePath() {
return this.picFile;
}
void setText(String text) {
this.text = text;
}
}
class SWING_OnValueChanged implements DocumentListener {
public void changedUpdate(DocumentEvent e) {
textValueChanged(e);
}
public void insertUpdate(DocumentEvent e) {
textValueChanged(e);
}
public void removeUpdate(DocumentEvent e) {
textValueChanged(e);
}
public void textValueChanged(DocumentEvent evt) {
//images
int i = 0;
System.out.println("Swing文本框的内容改变java添加功能代码了!" + ImageFrame.locationImage);
i = ImageFrame.locationImage;
//System.out.println("图片!" + ImageFrame.images[i].getFilePath());
try {
ImageFrame.property.setProperty(ImageFrame.images[i].getFilePath(),
ImageFrame.area.getText());
// ImageFrame.images[i].setText(ImageFrame.area.getText());
ImageFrame.property.store(new FileOutputStream("a.properties"),
"a.properties");
} catch (Exception ex) {
}
}
}
关于java添加功能代码和java添加语句的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。





