Java聊天室 IO流 Socket流 GUI界面 客户端WenJianconpoent界面详解
效果图
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GH41Z7w1-1599556700250)(https://pic.downk.cc/item/5f572fa2160a154a67987205.png)]
- 发送文件对象 界面显示您正在给xxx发送文件
- 对方接受或者拒绝
- 接受显示对方已成功接受
- 拒绝显示对方已拒绝 请重新发送
JButton checkButton2 = new JButton("发送文件");
checkButton2.setPreferredSize(new Dimension(85, 39));
checkButton2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String receiver = infortextArea22.getText();
String path = infortextArea21.getText();
if (!Objects.equals(path, "")) {
if (!Objects.equals(receiver, "")) {
File file = new File(path);
if (file.exists()) {
byte[] bytes = FileUtils.readFile(path);
User user = new User(receiver, MainView.ss, MsgTypeConfig.WENJIAN, System.currentTimeMillis(), file.getName(), file.length(), bytes);
try {
MainView.out.writeObject(user);
infortextArea1.append("您正在给"+receiver+"用户发送文件!等待对方接受或拒绝!"+"\n");
} catch (IOException ioException) {
ioException.printStackTrace();
}
} else {
JOptionPane.showMessageDialog(null, "文件找不到!请重新输入路径名!");
}
} else {
JOptionPane.showMessageDialog(null, "接收者为空!请输入接收者!");
}
} else {
JOptionPane.showMessageDialog(null, "路径为空!请输入路径名!");
}
}
});
代码
package SanWa.UI;
import SanWa.Net.MsgTypeConfig;
import SanWa.Net.User;
import SanWa.Util.FileUtils;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.Objects;
public class WenJianconpoent extends Box {
public static int i;
public static JTextArea infortextArea;
public static JTextArea infortextArea1;
public static JTextArea infortextArea21;
public static JTextArea infortextArea22;
public static JButton checkButton;
public static JButton checkButtonxx;
private JPanel c2;
private JPanel c21;
private JPanel c22;
public WenJianconpoent() {
//垂直布局
super(BoxLayout.Y_AXIS);
Font fon = new Font("黑体", 1, 12);
this.setBackground(new Color(208,208,208));
JLabel d1Label = new JLabel("文件发送界面界面");//组装视图
d1Label.setBackground(new Color(50,50,50));
d1Label.setForeground(Color.white);
JPanel btnPanel = new JPanel();
Color color = new Color(208,208,208);
d1Label.setFont(new java.awt.Font("黑体", 1, 25));
d1Label.setBounds(500, 500, 1500, 600);
btnPanel.setBackground(new Color(50,50,50));
btnPanel.setMaximumSize(new Dimension(2000, 40));
btnPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
btnPanel.add(d1Label);
this.add(btnPanel);
Box u3Box = Box.createHorizontalBox();
Box u4Box = Box.createHorizontalBox();
JScrollPane scrollPane_8 = new JScrollPane();
JScrollPane scrollPane_9 = new JScrollPane();
JScrollPane scrollPane_7 = new JScrollPane();
JScrollPane scrollPane_6 = new JScrollPane();
JLabel pLabel1 = new JLabel("请在此输入发送文件路径:");
pLabel1.setFont(new Font("黑体", 1, 16));
pLabel1.setPreferredSize(new Dimension(200, 30));
c21 = new JPanel();
c21.setLayout(new FlowLayout(FlowLayout.LEFT));
c21.setBackground(new Color(208,208,208));
c21.setPreferredSize(new Dimension(600, 30));
JPanel btn1Panel1 = new JPanel();
Color color1 = new Color(208,208,208);
btn1Panel1.setBackground(color1);
btn1Panel1.add(pLabel1);
JPanel btn11Panel11 = new JPanel();
Color color11 = new Color(208,208,208);
infortextArea21 = new JTextArea();
infortextArea21.setFont(new Font("黑体", 1, 18));
infortextArea21.setPreferredSize(new Dimension(400, 29));
infortextArea21.setBackground(new Color(208,208,208));
infortextArea21.append("");
btn11Panel11.setLayout(new FlowLayout(FlowLayout.RIGHT));
btn11Panel11.setBackground(color11);
btn11Panel11.add(infortextArea21);
scrollPane_8.setViewportView(infortextArea21);
c21.add(btn1Panel1);
c21.add(scrollPane_8);
u3Box.add(c21);
scrollPane_7.setViewportView(u3Box);
this.add(scrollPane_7);
JLabel pLabel2 = new JLabel("请在此输入文件的接收者:");
pLabel2.setFont(new Font("黑体", 1, 16));
pLabel2.setPreferredSize(new Dimension(200, 30));
c22 = new JPanel();
c22.setLayout(new FlowLayout(FlowLayout.LEFT));
c22.setBackground(new Color(208,208,208));
c22.setPreferredSize(new Dimension(600, 30));
JPanel btn1Panel2 = new JPanel();
btn1Panel2.setBackground(color1);
btn1Panel2.add(pLabel2);
JPanel btn11Panel22 = new JPanel();
infortextArea22 = new JTextArea();
infortextArea22.setFont(new Font("黑体", 1, 18));
infortextArea22.setPreferredSize(new Dimension(400, 29));
infortextArea22.setBackground(new Color(208,208,208));
infortextArea22.append("");
JButton checkButton2 = new JButton("发送文件");
checkButton2.setPreferredSize(new Dimension(85, 39));
checkButton2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String receiver = infortextArea22.getText();
String path = infortextArea21.getText();
if (!Objects.equals(path, "")) {
if (!Objects.equals(receiver, "")) {
File file = new File(path);
if (file.exists()) {
byte[] bytes = FileUtils.readFile(path);
User user = new User(receiver, MainView.ss, MsgTypeConfig.WENJIAN, System.currentTimeMillis(), file.getName(), file.length(), bytes);
try {
MainView.out.writeObject(user);
infortextArea1.append("您正在给"+receiver+"用户发送文件!等待对方接受或拒绝!"+"\n");
} catch (IOException ioException) {
ioException.printStackTrace();
}
} else {
JOptionPane.showMessageDialog(null, "文件找不到!请重新输入路径名!");
}
} else {
JOptionPane.showMessageDialog(null, "接收者为空!请输入接收者!");
}
} else {
JOptionPane.showMessageDialog(null, "路径为空!请输入路径名!");
}
}
});
btn11Panel22.setLayout(new FlowLayout(FlowLayout.RIGHT));
btn11Panel22.setBackground(color11);
btn11Panel22.add(infortextArea22);
scrollPane_9.setViewportView(infortextArea22);
c22.add(btn1Panel2);
c22.add(scrollPane_9);
c22.add(checkButton2);
u4Box.add(c22);
scrollPane_6.setViewportView(u4Box);
this.add(scrollPane_6);
Box u1Box = Box.createHorizontalBox();
u1Box.setPreferredSize(new Dimension(850, 190));
JLabel uLabel = new JLabel(" ");
infortextArea1 = new JTextArea();
infortextArea1.setEditable(false);
infortextArea1.setFont(new Font("黑体", 1, 18));
infortextArea1.setBounds(0, 0, 850, 200);
infortextArea1.setBackground(new Color(208,208,208));
JScrollPane scrollPane21 = new JScrollPane();
scrollPane21.setBounds(0, 0, 850, 300);
scrollPane21.setViewportView(infortextArea1);
u1Box.add(scrollPane21);
this.add(u1Box);
Box u5Box = Box.createHorizontalBox();
JScrollPane scrollPane_5 = new JScrollPane();
JLabel pLabel = new JLabel("请在此选择是否接受文件!");
pLabel.setFont(new Font("黑体", 1, 16));
pLabel.setPreferredSize(new Dimension(210, 30));
c2 = new JPanel();
c2.setLayout(new FlowLayout(FlowLayout.LEFT));
c2.setBackground(new Color(208,208,208));
c2.setPreferredSize(new Dimension(600, 30));
JPanel btn1Panel = new JPanel();
btn1Panel.setBackground(color1);
btn1Panel.add(pLabel);
checkButton = new JButton("接受文件");
checkButtonxx = new JButton("拒绝文件");
checkButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ClientReadMsgThread.isSave = false;
}
});
checkButtonxx.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ClientReadMsgThread.isClose = true;
}
});
c2.add(btn1Panel);
c2.add(checkButton);
c2.add(checkButtonxx);
u5Box.add(c2);
scrollPane_5.setViewportView(u5Box);
this.add(scrollPane_5);
Box uBox = Box.createHorizontalBox();
uBox.setPreferredSize(new Dimension(850, 190));
infortextArea = new JTextArea();
infortextArea.setFont(new Font("黑体", 1, 18));
infortextArea.setEditable(false);
infortextArea.setBackground(new Color(208,208,208));
JScrollPane scrollPane_1 = new JScrollPane();
uBox.add(scrollPane_1);
scrollPane_1.setViewportView(infortextArea);
this.add(uBox);
JPanel btnPanel1 = new JPanel();
Color color12 = new Color(208,208,208);
btnPanel1.setBackground(color12);
btnPanel1.setMaximumSize(new Dimension(1500, 40));
btnPanel1.setLayout(new FlowLayout(FlowLayout.RIGHT));
JButton getButton1 = new JButton("打开文件夹");
getButton1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
File file = new File("E:\\WenJian");
if (!file.exists()) {
file.mkdirs();
}
try {
Desktop.getDesktop().open(file);
} catch (IOException e12) {
e12.printStackTrace();
}
}
});
btnPanel1.add(getButton1);
this.add(btnPanel1);
}
}