网络相册
登陆注册界面
太久太久没有在c站作总结了,今天来这里补一篇开学老师给我们布置的小任务吧
这里我完成了登录和注册的功能,美中不足是我暂时只学到了IO流,没有办法用数据库来进行存储,这里面无论是关于
图片文件,亦或者照片存储与上传,皆用到的是gui和io流的知识,惭愧,不过后面我还会发表一篇五子棋,用Javafx
写的,也会设计网络编程的知识,用到一些算法
这里我用到了1一张图片作为背景,注意路径
package com01;
import javax.swing.*;
import javax.swing.border.MatteBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Map;
public class Text extends JFrame {
public static void main(String[] args) {
new Text();
}
public Text() {
JPanel jp1,jp2,jp3;
JLabel jlb1,jlb2;
JTextField jt1;
JPasswordField jt2;
JButton jb1,jb2;
JFrame t=new JFrame();
t.setSize(400,250);
t.setLocationRelativeTo(null);
t.setDefaultCloseOperation(3);
t.setTitle("网络相册");
t.setLayout(new GridLayout(3,1));
t.setIconImage(new ImageIcon("src\\com02\\bg1.jpg").getImage());
jp1=new JPanel();
jp2=new JPanel();
jp3=new JPanel();
t.add(jp1);
t.add(jp2);
t.add(jp3);
jlb1=new JLabel("用户名");
jt1=new JTextField(10);
jt2=new JPasswordField(10);
jlb2=new JLabel("密码");
jb1=new JButton("登录");
jb2=new JButton("注册");
jp1.add(jlb1);
jp1.add(jt1);
jp2.add(jlb2);
jp2.add(jt2);
jp3.add(jb1);
jp3.add(jb2);
t.setVisible(true);
Map<String,String>map=new HashMap<>();
String line;
File file=new File("src\\com03");
try {
BufferedReader br=new BufferedReader(new FileReader(file));
while ((line=br.readLine())!=null){
String[]str=line.split(",");
map.put(str[0],str[1]);
}
} catch (Exception e) {
e.printStackTrace();
}
jb1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
String name=jt1.getText().trim();
String password=new String(jt2.getPassword()).trim();
String password2=map.get(name);
if(jt1.getText().trim().length()==0||new String(jt2.getPassword()).trim().length()==0){
JOptionPane.showMessageDialog(null,"用户名密码不允许为空","message",JOptionPane.ERROR_MESSAGE);
}
else if(jt1.getText().trim().length() > 11||new String(jt2.getPassword()).trim().length() > 8){
JOptionPane.showMessageDialog(null, "用户名或密码超出指定位数","message",JOptionPane.WARNING_MESSAGE);
}
else if(map.containsKey(name)&&password.equals(password2)){
JOptionPane.showMessageDialog(null, "登录成功");
new Main().run( );
t.setVisible(false);
}
else{
JOptionPane.showMessageDialog(null, "用户名或密码错误","message",JOptionPane.QUESTION_MESSAGE);
}
}
});
jb2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new AdminRegister();
t.setVisible(false);
}
});
}
}
上面是登陆,下面这个是注册界面
package com01;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class AdminRegister extends JFrame {
public static void main(String[] args) {
new AdminRegister();
}
JTextField jt;
JPasswordField jw, jw1;
Font fw = new Font("宋体", Font.BOLD, 20);
public AdminRegister() {
setTitle("注册窗口");
setBounds(500, 100, 607, 469);
setLayout(null);
setDefaultCloseOperation(2);
setLocationRelativeTo(null);
setIconImage(new ImageIcon("src\\com02\\bg1.jpg").getImage());
JLabel jl1 = new JLabel(new ImageIcon("src\\com02\\bg1.jpg"));
jl1.setBounds(0, 0, 607, 409);
add(jl1);
this.getLayeredPane().add(jl1, new Integer(Integer.MIN_VALUE));
((JPanel) this.getContentPane()).setOpaque(false);
JPanel jp1 = new JPanel();
JLabel jl = new JLabel(" 用 户 名: ");
jl.setFont(fw);
jl.setBounds(130, 195, 120, 35);
add(jl);
jt = new JTextField();
jt.setBounds(230, 195, 200, 35);
add(jt);
JLabel jl2 = new JLabel("密 码:");
jl2.setFont(fw);
jl2.setBounds(130, 245, 120, 35);
add(jl2);
jw = new JPasswordField();
jw.setBounds(230, 245, 200, 35);
add(jw);
JLabel jl3 = new JLabel("确认密码:");
jl3.setFont(fw);
jl3.setBounds(130, 295, 120, 25);
add(jl3);
jw1 = new JPasswordField();
jw1.setBounds(230, 295, 200, 35);
add(jw1);
JButton ju = new JButton("提交");
ju.setBounds(180, 370, 80, 30);
ju.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String buttonname = e.getActionCommand();
if (buttonname.equals("提交")) {
String str = String.valueOf(jw.getPassword());
String str1 = String.valueOf(jw1.getPassword());
if (jt.getText().trim().length() == 0 || new String(jw1.getPassword()).trim().length() == 0) {
JOptionPane.showMessageDialog(null, "用户名密码不允许为空", "message", JOptionPane.ERROR_MESSAGE);
} else if (jt.getText().trim().length() > 11 || new String(jw1.getPassword()).trim().length() > 8) {
JOptionPane.showMessageDialog(null, "用户名或密码超出指定位数", "message", JOptionPane.WARNING_MESSAGE);
} else {
if (str.equals(str1)) {
String str2 = jt.getText() + "," + str;
File f = new File("src\\com03");
try {
if (!f.exists()) {
f.createNewFile();
}
FileWriter hu = new FileWriter(f, true);
BufferedWriter buff = new BufferedWriter(hu);
buff.write(str2);
buff.newLine();
buff.flush();
buff.close();
JOptionPane.showMessageDialog(null, "写入成功");
new Text();
dispose();
} catch (Exception e1) {
e1.printStackTrace();
}
} else {
JOptionPane.showMessageDialog(null, "两次密码不相同,请重新输入密码");
jw.setText("");
jw1.setText("");
}
}
}
}
});
add(ju);
JButton ju1 = new JButton("取消");
ju1.setBounds(300, 370, 80, 30);
ju1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
jt.setText("");
jw.setText("");
jw1.setText("");
}
});
add(ju1);
this.setVisible(true);
}
}
关于gui及图片的上传,下载,删除功能,还有一个界面设计
啊这里提醒一下,我的所有图片文件放到src下的com02里
至于账号和密码我存储到com03里了
package com01;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import com01.BackgroundPanel;
public class Main extends JFrame {
List<BufferedImage> images=new ArrayList<>();
public void run() {
BackgroundPanel bp=new BackgroundPanel();
JFrame frame=new JFrame("管理窗口");
frame.setSize(500,800);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(3);
frame.setLayout(new BorderLayout());
frame.getContentPane().add(bp);
JMenuBar menuBar=new JMenuBar();
JMenu fileMenu=new JMenu("文件");
JMenu viewMenu=new JMenu("浏览");
menuBar.add(fileMenu);
menuBar.add(viewMenu);
JMenuItem upload =new JMenuItem("上传");
JMenuItem download =new JMenuItem("下载");
JMenuItem delet=new JMenuItem("删除");
JMenuItem vv =new JMenuItem("看一看");
fileMenu.add(upload);
fileMenu.add(download);
fileMenu.add(delet);
viewMenu.add(vv);
delet.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
});
vv.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new ImageChanges();
}
});
upload.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Image image=Jc();
}
});
download.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Image image1=Jk();
}
});
delet.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jp();
}
});
frame.setJMenuBar(menuBar);
frame.setVisible(true);
}
int m;
public Image Jc(){
BufferedImage image=null;
JFileChooser jFileChooser=new JFileChooser();
FileNameExtensionFilter filter=new FileNameExtensionFilter("图片文件","jpg","png");
jFileChooser.setFileFilter(filter);
int ret=jFileChooser.showOpenDialog(this);
if (ret==JFileChooser.APPROVE_OPTION){
File file=jFileChooser.getSelectedFile();
System.out.println(file);
try {
image=ImageIO.read(new FileInputStream(String.valueOf(file)));
File outputfile = new File("D:\\apple\\day_17 network album\\src\\com02\\"+m+".jpg");
m++;
ImageIO.write(image,"jpg",outputfile.getAbsoluteFile());
} catch (IOException e) {
e.printStackTrace();
}
}
return image;
}
public Image Jk(){
BufferedImage image=null;
JFileChooser jFileChooser=new JFileChooser("D:\\apple\\day_17 network album\\src\\com02\\");
int ret2=jFileChooser.showOpenDialog(this);
if (ret2==JFileChooser.APPROVE_OPTION){
File file=jFileChooser.getSelectedFile();
JFileChooser jf=new JFileChooser();
int ret3=jf.showOpenDialog(this);
if (ret3==JFileChooser.APPROVE_OPTION){
File file1=jf.getSelectedFile();
try {
image=ImageIO.read(new FileInputStream(String.valueOf(file)));
ImageIO.write(image,null,file1);
} catch (IOException e) {
e.printStackTrace();
}
}
}
return image;
}
public void jp(){
JFileChooser jFileChooser=new JFileChooser("D:\\apple\\day_17 network album\\src\\com02\\");
jFileChooser.setApproveButtonText("删除");
int ret=jFileChooser.showOpenDialog(this);
if (ret==JFileChooser.APPROVE_OPTION){
File file=jFileChooser.getSelectedFile();
file.delete();
}
}
}
package com01;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class BackgroundPanel extends JPanel implements ActionListener {
ImageIcon icon = null;
JButton b1 = null;
JFileChooser fileChooser = null;
String str = null;
Graphics g = this.getGraphics();
public BackgroundPanel() {
b1 = new JButton("打开文件");
b1.addActionListener(this);
this.add(b1);
fileChooser = new JFileChooser("src\\com02");
}
public void actionPerformed(ActionEvent e)
{
File file = null;
int result;
if (e.getActionCommand().equals("打开文件"))
{
fileChooser.setApproveButtonText("打开吗");
fileChooser.setDialogTitle("确定");
result = fileChooser.showOpenDialog(this);
if (result == JFileChooser.APPROVE_OPTION)
{
str = fileChooser.getSelectedFile().getAbsolutePath();
System.out.println(str);
icon = new ImageIcon(str);
repaint();
}
}
}
public void paint(Graphics g) {
if (icon!=null){
Image image = icon.getImage();
g.drawImage(image, 0, 0, icon.getImageObserver());
}
}
}
class my extends JFrame {
BackgroundPanel bp;
public my() {
bp = new BackgroundPanel();
this.getContentPane().add(bp);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.setSize(500,500);
this.setVisible(true);
}
public static void main(String[] args) {
new my();
}
}
关于图片的浏览
package com01;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
public class ImageChanges extends JFrame{
private JFrame jf;
private JButton button1;
private JButton button2;
private JLabel label;
private int i=(int)SelectFile.picCount();
ImageChanges(){
jf = new JFrame("图片变换");
label = new JLabel("");
label.setIcon(new ImageIcon("src\\com02\\bg1.jpg"));
label.setBounds(0, 0, 800, 800);
jf.setBounds(0, 0, 500, 860);
jf.setLocationRelativeTo(null);
jf.add(label,BorderLayout.CENTER);
File file=new File("src\\com02");
File[] files=file.listFiles();
button1 = new JButton("上一张");
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
button2.setEnabled(true);
if (i>1)
i--;
ImageIcon icon=new ImageIcon(files[i-1].getPath());
label.setIcon(icon);
if (i == 1){
button1.setEnabled(false);
JOptionPane.showMessageDialog(ImageChanges.this, "当前为第一张图片");
}
}
});
button1.setBounds(0, 780, 93, 30);
jf.add(button1);
button2 = new JButton("下一张");
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
button1.setEnabled(true);
if (i<SelectFile.picCount())
i++;
ImageIcon icon=new ImageIcon(files[i-1].getPath());
label.setIcon(icon);
if (i == SelectFile.picCount()){
button2.setEnabled(false);
JOptionPane.showMessageDialog(ImageChanges.this, "当前浏览相册至底部");
}
}
});
button2.setBounds(150, 780, 93, 30);
jf.add(button2);
jf.getContentPane().add(label);
jf.setVisible(true);
}
public static void main(String[] args) {
new ImageChanges();
System.out.println(SelectFile.picCount());
}
}
com02
怎么说呢,感觉自己的美感还需要提升,因为写的仓促,这篇我后面学完别的东西会重新写的 就像余老师说的,提高自己能力的一种方法,就是在自己以往写过的代码上,做出改进和提升
|