📄 mainframe.java
字号:
if(jTabbedPane.getComponentAt(i) instanceof JScrollPaneWithText){
current = (JScrollPaneWithText)jTabbedPane.getComponentAt(i);
}
break;
}
}
Style newStyle = current.getJTextPane().addStyle("newStyle", def);
StyleConstants.setAlignment(newStyle, StyleConstants.ALIGN_RIGHT);
StyleConstants.setIcon(newStyle,new ImageIcon(input.getBytes()));
Document doc = current.getJTextPane().getDocument();
try{
doc.insertString(doc.getLength(),input.getFromName().concat(GetTime.currentTime().concat(new String("\n"))), sys);
doc.insertString(doc.getLength(),"\n",newStyle);
current.getJTextPane().setCaretPosition(current.getJTextPane().getDocument().getLength());
}catch(BadLocationException ble){
System.err.println("Couldn't insert initial text");
}
//*************************************
}//图片信息部分结束
else if(input.getDataType().equals(new String("FILE"))){//得到的是文件信息
// ***********************************得到面板并输出
JScrollPaneWithText current = null;
String fileType = null;
for(int i=1;i<jTabbedPane.getTabCount();i++){
if(jTabbedPane.getTitleAt(i).equals(input.fromName)){
if(jTabbedPane.getComponentAt(i) instanceof JScrollPaneWithText){
current = (JScrollPaneWithText)jTabbedPane.getComponentAt(i);
}
break;
}
}
Document doc = current.getJTextPane().getDocument();
try{
doc.insertString(doc.getLength(),input.getFromName().concat(" 要发送文件 ").concat(input.getFontFamily().concat(" 给您\n")),sys3);
current.getJTextPane().setCaretPosition(current.getJTextPane().getDocument().getLength());
}catch(BadLocationException ble){
System.err.println("Couldn't insert initial text");
}
JFileChooser chooser = new JFileChooser();
ExampleFileFilter filter = new ExampleFileFilter();
String fileName = input.getFontFamily();
int index = fileName.lastIndexOf('.');
if(index!=-1){
fileType = fileName.substring(index+1,fileName.length());
filter.addExtension(fileType);
filter.setDescription(fileType);
chooser.setFileFilter(filter);
}
int returnVal=chooser.showSaveDialog(new JFrame());
if(returnVal == JFileChooser.APPROVE_OPTION) {
String saveFileName = chooser.getSelectedFile().getPath().concat(".").concat(fileType);
FileOutputStream fileOutputStream = new FileOutputStream(saveFileName);
fileOutputStream.write(input.getBytes());
fileOutputStream.close();
doc.insertString(doc.getLength(),new String("文件已保存在 ").concat(saveFileName).concat(" 下\n"),sys3);
}
else
doc.insertString(doc.getLength(),"您放弃了保存文件\n",sys3);
}
////***************************************************************
}
}
}catch(Exception e){
e.printStackTrace();
JOptionPane.showMessageDialog(null, "失去连接!", "ERROR", JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
}
/**
* This method initializes jPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel() {
if (jPanel == null) {
jLabel5 = new JLabel();
jLabel5.setBounds(new Rectangle(10, 380, 50, 23));
jLabel5.setText("Server:");
jPanel = new JPanel();
jPanel.setLayout(null);
jPanel.add(getJToolBar(), null);
jPanel.add(getJScrollPane1(), null);
jPanel.add(getJTabbedPane(), null);
jPanel.add(getJButton(), null);
jPanel.add(getJButton1(), null);
jPanel.add(getJDesktopPane(), null);
jPanel.add(getJSplitPane(), null);
jPanel.add(getJDesktopPane1(), null);
jPanel.add(getJTextField2(), null);
jPanel.add(jLabel5, null);
}
return jPanel;
}
/**
* This method initializes jToolBar
*
* @return javax.swing.JToolBar
*/
private JToolBar getJToolBar() {
if (jToolBar == null) {
jToolBar = new JToolBar();
jToolBar.setBounds(new Rectangle(1, 269, 390, 23));
jToolBar.add(getJButton2());
jToolBar.add(getJButton8());
jToolBar.add(getJButton3());
jToolBar.add(getJButton9());
jToolBar.add(getJButton6());
jToolBar.add(getJButton7());
jToolBar.add(getJButton10());
jToolBar.add(getJButton11());
jToolBar.addSeparator();
jToolBar.add(getJButton12());
}
return jToolBar;
}
/**
* This method initializes jScrollPane1
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJScrollPane1() {
if (jScrollPane1 == null) {
jScrollPane1 = new JScrollPane();
jScrollPane1.setBounds(new Rectangle(1, 298, 393, 78));
jScrollPane1.setViewportView(getJTextArea());
}
return jScrollPane1;
}
/**
* This method initializes jTextArea
*
* @return javax.swing.JTextArea
*/
private JTextArea getJTextArea() {
if (jTextArea == null) {
jTextArea = new JTextArea();
jTextArea.setBounds(new Rectangle(0, 0, 391, 75));
jTextArea.setFont(new Font("Dialog", Font.PLAIN, 14));
}
return jTextArea;
}
/**
* This method initializes jTabbedPane
*
* @return javax.swing.JTabbedPane
*/
private JTabbedPane getJTabbedPane() {
if (jTabbedPane == null) {
jTabbedPane = new JTabbedPane();
jTabbedPane.setBounds(new Rectangle(0, 0, 391, 263));
jTabbedPane.addTab("NOTICE", null, getJScrollPane(), null);
}
return jTabbedPane;
}
/**
* This method initializes jScrollPane
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJScrollPane() {
if (jScrollPane == null) {
jScrollPane = new JScrollPane();
jScrollPane.setViewportView(getJTextPane());
}
return jScrollPane;
}
/**
* This method initializes jTextPane
*
* @return javax.swing.JTextPane
*/
private JTextPane getJTextPane() {
if (jTextPane == null) {
jTextPane = new JTextPane();
//File file= new File(".");
/*try{
jTextPane.setPage(new URL("file:/"+file.getAbsolutePath()+"/copyright.txt"));
}catch(Exception e){
e.printStackTrace();
}*/
jTextPane.setEditable(false);
/*try{
jTextPane.setPage("file:/d:/java_xml.txt");
}catch(Exception e){
e.printStackTrace();
}*/
}
return jTextPane;
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(264, 380, 60, 23));
jButton.setText("关闭");
jButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
if(jTabbedPane.getSelectedIndex()!=0){
jTabbedPane.remove(jTabbedPane.getSelectedIndex());
}
else{
JOptionPane.showMessageDialog(null, "不能关闭该窗口!", "ERROR", JOptionPane.ERROR_MESSAGE);
}
//System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return jButton;
}
/**
* This method initializes jButton1
*
* @return javax.swing.JButton
*/
private JButton getJButton1() {
if (jButton1 == null) {
jButton1 = new JButton();
jButton1.setBounds(new Rectangle(330, 380, 60, 23));
jButton1.setText("发送");
//jButton1.setEnabled(false);
jButton1.setMnemonic(KeyEvent.VK_ENTER);
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
if(login == false){//尚未登陆
JOptionPane.showMessageDialog(null, "请先登陆", "ERROR", JOptionPane.ERROR_MESSAGE);
return;
}
if(toName==null){//没有接收方
JOptionPane.showMessageDialog(null, "您未选择聊天对象,或对方已下线", "ERROR", JOptionPane.ERROR_MESSAGE);
return;
}
if(jTextArea.getText().length()==0){
JOptionPane.showMessageDialog(null, "不能发送空消息", "ERROR", JOptionPane.ERROR_MESSAGE);
return;
}
try{
String send = jTextArea.getText();
data2 = new StreamData(jTextField.getText(),toName,"TEXT",fontFamily,fontSize,myTextColor.getRGB(),send.getBytes(),(send.getBytes()).length);
// *******************************************
//在自己的输出框显示
JScrollPaneWithText current = null;
for(int i=1;i<jTabbedPane.getTabCount();i++){
if(jTabbedPane.getTitleAt(i).equals(toName)){
if(jTabbedPane.getComponentAt(i) instanceof JScrollPaneWithText){
current = (JScrollPaneWithText)jTabbedPane.getComponentAt(i);
}
break;
}
}
//********************************************************
if(current==null){
JScrollPaneWithText jSText = new JScrollPaneWithText();
jTabbedPane.addTab(toName, null, jSText, null);
jTabbedPane.setSelectedComponent(jSText);
current = jSText;
}
//*********************************************************
String[] show = new String(data2.getBytes()).split("<~>");
Style newStyle = current.getJTextPane().addStyle("newStyle", def);
StyleConstants.setForeground(newStyle,new Color(myTextColor.getRGB()));
StyleConstants.setFontFamily(newStyle, fontFamily);
StyleConstants.setFontSize(newStyle, fontSize);
Document doc = current.getJTextPane().getDocument();
try{
doc.insertString(doc.getLength(),data2.getFromName().concat(GetTime.currentTime().concat(new String("\n"))), sys2);
//doc.insertString(doc.getLength(), new String(data2.getBytes()).concat(new String("\n")),newStyle);
//***************************************************************
for(int i=0;i<show.length;i++){
if(show[i].length()==2&&show[i].charAt(0)=='F'&&show[i].charAt(1)>='0'&&show[i].charAt(1)<='9'){
int index = Integer.parseInt(Character.toString(show[i].charAt(1)));
//System.out.println(index);
Style newImageStyle = current.getJTextPane().addStyle("newImageStyle", def);
StyleConstants.setIcon(newImageStyle, new ImageIcon("Face\\"+Integer.toString(index)+".gif"));
StyleConstants.setAlignment(newImageStyle,StyleConstants.ALIGN_LEFT);
doc.insertString(doc.getLength()," ",newImageStyle);
}
else if(show[i].length()==3&&show[i].charAt(0)=='F'&&show[i].charAt(1)>='0'&&show[i].charAt(1)<='9'&&show[i].charAt(2)>='0'&&show[i].charAt(2)<='9'){
int tempIndex = Integer.parseInt(Character.toString(show[i].charAt(1)));
int tempIndex2 = Integer.parseInt(Character.toString(show[i].charAt(2)));
int index = 10*tempIndex+tempIndex2;
//System.out.println(index);
Style newImageStyle = current.getJTextPane().addStyle("newImageStyle", def);
StyleConstants.setIcon(newImageStyle, new ImageIcon("Face\\"+Integer.toString(index)+".gif"));
StyleConstants.setAlignment(newImageStyle,StyleConstants.ALIGN_LEFT);
doc.insertString(doc.getLength()," ",newImageStyle);
}
else{
doc.insertString(doc.getLength(), show[i], newStyle);
}
}
doc.insertString(doc.getLength(),"\n", newStyle);
//***************************************************************
current.getJTextPane().setCaretPosition(current.getJTextPane().getDocument().getLength());
}catch(BadLocationException ble){
System.err.println("Couldn't insert initial text");
}
objectOutputStream.writeObject(data2);//发出消息
jTextArea.setText(null);//清空输入框
jTextArea.requestFocus();//得到焦点
}catch(Exception ioe){
ioe.printStackTrace();
}
//System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return jButton1;
}
/**
* This method initializes jPanel1
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -