📄 gui.java
字号:
package mypkg;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.filechooser.FileFilter;import java.io.File;public class GUI { private static JFrame frame; private static JTextField ipText; private static JTextField portText; private static JTextField favorText; private static JTextField fileText; private static JLabel statusBar; private int side; private static JTextField chatText; private static JTextArea chatTextArea; private FileServer fileServer; private FileClient fileClient; private ChatServer chatServer; private ChatClient chatClient; private static JFileChooser fileChooser; private static JFileChooser fileSaver; private static ActionListener buttonListener; private static JDialog quryDialog; private static JButton jbclose; private static JButton jbconnect; private String saveFilePath; private String fromFilePath; private String fileName; public GUI(int s){ this.side = s; JPanel leftPanel = new JPanel(new BorderLayout()); statusBar = new JLabel(""); buttonListener = new ActionAdapter(){ public void actionPerformed(ActionEvent e){ String btName = e.getActionCommand(); if(btName.equals("CONNECT")&&!validate()){ return; } if(btName.equals("View")){ fileChooser = new JFileChooser(); int i = fileChooser.showOpenDialog(null); if(JFileChooser.APPROVE_OPTION==i){ fileText.setText(fileChooser.getSelectedFile().toString() + "\n"); setFromFilePath(fileChooser.getSelectedFile().toString()); setFileName(fileChooser.getSelectedFile().getName()); } } switch(side){ case Utility.SERVER: if(btName.equals("CONNECT")){ chatServer = new ChatServer(getPort()); fileServer = new FileServer(getPort()+1); chatServer.start(); fileServer.start(); jbclose.setEnabled(true); jbconnect.setEnabled(false); System.out.println("The port is: " + getPort()); } if(btName.equals("DISCONNECT")){ chatServer.send(Utility.MSG,"DISCONNECT"); chatServer.setStatus(Utility.DISCONNECTING); fileServer.send(Utility.MSG,"DISCONNECT"); fileServer.setStatus(Utility.DISCONNECTING); jbclose.setEnabled(false); jbconnect.setEnabled(true); } if(btName.equals("Send")&&fileText.getText()!=""){ //qery oppsite receive file or not and include the size of file //the message type and body is split by "|" and file size is split by "*" StringBuffer buff = new StringBuffer(); buff.append(getFileName()); buff.append("*"); File file = new File(getFromFilePath()); buff.append(file.length()); fileServer.send(Utility.QRY,buff.toString()); } if(btName.equals("Yes")){ fileSaver = new JFileChooser(); int i = fileSaver.showSaveDialog(null); if(JFileChooser.APPROVE_OPTION==i){ quryDialog.setVisible(false); setSaveFilePath(fileSaver.getSelectedFile().getAbsolutePath()); //first set myself status and then reply fileServer.setStatus(Utility.RECVING); fileServer.send(Utility.RPY,Utility.RECV); } } if(btName.equals("No")){ quryDialog.setVisible(false); fileServer.send(Utility.RPY,Utility.REFU); } break; case Utility.CLIENT: if(btName.equals("CONNECT")){ chatClient = new ChatClient(getIP(),getPort()); fileClient = new FileClient(getIP(),getPort()+1); chatClient.start(); fileClient.start(); jbclose.setEnabled(true); jbconnect.setEnabled(false); System.out.println("The port is: " + getPort()); } if(btName.equals("DISCONNECT")){ chatClient.send(Utility.MSG,"DISCONNECT"); chatClient.setStatus(Utility.DISCONNECTING); fileClient.send(Utility.MSG,"DISCONNECT"); fileClient.setStatus(Utility.DISCONNECTING); jbclose.setEnabled(false); jbconnect.setEnabled(true); } if(btName.equals("Send")&&fileText.getText()!=""){ //qery oppsite receive file or not and include the size of file //the message type and body is split by "|" and file size is split by "*" StringBuffer buff = new StringBuffer(); buff.append(getFileName()); buff.append("*"); File file = new File(getFromFilePath()); buff.append(file.length()); fileClient.send(Utility.QRY,buff.toString()); } if(btName.equals("Yes")){ fileSaver = new JFileChooser(); int i = fileSaver.showSaveDialog(null);/* File file = new File(getFileName()); fileSaver.setCurrentDirectory(new File(".")); fileSaver.setFileFilter(new myFilter()); fileSaver.setSelectedFile(file);*/ if(JFileChooser.APPROVE_OPTION==i){ quryDialog.setVisible(false); setSaveFilePath(fileSaver.getSelectedFile().getAbsolutePath());// first set myself status and then reply fileClient.setStatus(Utility.RECVING); fileClient.send(Utility.RPY,Utility.RECV); } } if(btName.equals("No")){ fileClient.send(Utility.RPY,Utility.REFU); } break; } } }; frame = new JFrame(side==Utility.SERVER?"Server":"Client"); JPanel mainPanel = new JPanel(new GridLayout(1,2)); JPanel ipPanel = new JPanel(); ipPanel.add(new JLabel("IP:")); ipText = new JTextField("127.0.0.1",10); ipPanel.add(ipText); JPanel favorPanel = new JPanel(); favorPanel.add(new JLabel("Favor:")); favorText = new JTextField(10); favorPanel.add(favorText); JPanel portPanel = new JPanel(); portPanel.add(new JLabel("Port:")); portText = new JTextField("1234",10); portPanel.add(portText); JPanel filePanel = new JPanel(); fileText = new JTextField(10); filePanel.add(fileText); JButton btView = new JButton("View"); JButton btSend = new JButton("Send"); filePanel.add(fileText); filePanel.add(btView); filePanel.add(btSend); btView.addActionListener(buttonListener); btSend.addActionListener(buttonListener); JPanel buttonPanel = new JPanel(); jbconnect = new JButton("CONNECT"); jbclose = new JButton("DISCONNECT"); jbclose.setEnabled(false); buttonPanel.add(jbconnect); buttonPanel.add(jbclose); jbconnect.addActionListener(buttonListener); jbclose.addActionListener(buttonListener); JPanel fieldPanel = new JPanel(new GridLayout(5,1)); fieldPanel.add(ipPanel); fieldPanel.add(portPanel); fieldPanel.add(favorPanel); fieldPanel.add(filePanel); fieldPanel.add(buttonPanel); leftPanel.add(fieldPanel,BorderLayout.NORTH); leftPanel.add(statusBar,BorderLayout.SOUTH); JPanel rightPanel = new JPanel(new BorderLayout()); chatTextArea = new JTextArea(10, 20); chatTextArea.setLineWrap(true); chatTextArea.setEditable(false); chatTextArea.setForeground(Color.blue); JScrollPane chatTextPane = new JScrollPane(chatTextArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); rightPanel.add(chatTextPane,BorderLayout.NORTH); chatText = new JTextField(); chatText.addActionListener(new ActionAdapter(){ public void actionPerformed(ActionEvent e){ String s = chatText.getText(); StringBuffer sb = new StringBuffer(); if(!s.equals("")){ sb.append(getFavorName()); sb.append(":"); sb.append(s); } addChatContent(sb.toString()); if(!s.equals("")){ switch(side){ case Utility.SERVER: chatServer.send(Utility.MSG,sb.toString()); break; case Utility.CLIENT: chatClient.send(Utility.MSG,sb.toString()); break; } } } }); rightPanel.add(chatText,BorderLayout.SOUTH); mainPanel.add(leftPanel); mainPanel.add(rightPanel); frame.getContentPane().add(mainPanel); frame.setLocation(200,200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } public String getIP(){ return ipText.getText(); } public int getPort(){ int port = 0; try{ port = Integer.parseInt(portText.getText()); }catch(NumberFormatException e){ return port; } return port; } public int getSide() { return side; } public void setSide(int side) { this.side = side; } public void setStatusContent(String text){ statusBar.setText(text); } private void setFromFilePath(String filePath){ this.fromFilePath = filePath; } public String getFromFilePath(){ return this.fromFilePath; } public void setSaveFilePath(String saveFilePath){ this.saveFilePath = saveFilePath; } public String getSaveFilePath(){ return this.saveFilePath; } public void openDialog(String fileName){ quryDialog = new JDialog(frame,"Receive this file?",true); JLabel jTip = new JLabel(fileName); JButton jReceive = new JButton("Yes"); JButton jRefuse = new JButton("No"); jReceive.addActionListener(buttonListener); jRefuse.addActionListener(buttonListener); JPanel fieldPanel = new JPanel(new GridLayout(2,1)); JPanel btPanel = new JPanel(); btPanel.add(jReceive); btPanel.add(jRefuse); fieldPanel.add(jTip); fieldPanel.add(btPanel); quryDialog.getContentPane().add(fieldPanel); quryDialog.setSize(200,150); quryDialog.setLocation(450,450); quryDialog.setVisible(true); } class ActionAdapter implements ActionListener { public void actionPerformed(ActionEvent e) {} } class myFilter extends FileFilter{ public boolean accept(File f) { if(f.toString().equals(getFileName())){ return true; } return false; } public String getDescription() { return new String(""); } } public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public String getFavorName(){ return favorText.getText(); } public void addChatContent(String content){ StringBuffer sb = new StringBuffer(getChatContent()); sb.append(content + "\n"); System.out.println(content + " has add to the chatContent."); chatTextArea.setText(sb.toString()); cleanChatContent(); } public void addChatContentOnly(String content){ StringBuffer sb = new StringBuffer(getChatContent()); sb.append(content + "\n"); System.out.println(content + " has add to the chatContent."); chatTextArea.setText(sb.toString()); } public String getChatContent(){ return chatTextArea.getText(); } public void cleanChatContent(){ chatText.setText(""); } public void reSet(){ jbclose.setEnabled(false); jbconnect.setEnabled(true); } private boolean validate(){ if(getIP().equals("")){ setStatusContent("Please fill in ip.");// ipText.setFocusable(true); return false; } if(getPort()==0){ setStatusContent("Please fill in port.");// portText.setFocusable(true); return false; } if(getFavorName().equals("")){ setStatusContent("Please fill in favor name.");// favorText.setFocusable(true); return false; } return true; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -