⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainframe.java

📁 一个类似QQ的在线通讯聊天软件原码,适合初学者参考学习。
💻 JAVA
字号:
package openicq.gui;import java.awt.*;import java.awt.event.*;import javax.swing.*;import openicq.Start;import openicq.data.Default;import openicq.data.Resource;import openicq.management.Settings;import org.javalib.dynamic.localize.Localizable;import org.javalib.gui.OptionPane;import org.javalib.gui.WindowToolkit;import org.javalib.gui.image.ImageLoader;import org.javalib.io.CryptFile;import org.javalib.sound.MediaPlayer;import org.javalib.util.MessageBuffer;import org.javalib.util.TitledList;/** * The <code>MainFrame</code> class is the main window of this application. * @author Hansgeorg Schwibbe * @copyright 2004 */public class MainFrame extends JFrame implements Localizable, SysTrayAction{  protected TitledList localized = Start.env.getLocalizedList();  private JMenuBar jMenuBar = new JMenuBar();  private JMenu jMenuICQ = new JMenu();  private JMenu jMenuHelp = new JMenu();  private JMenuItem jMenuItemAddContact = new JMenuItem();  private JMenuItem jMenuItemOwnDetails = new JMenuItem();  private JMenuItem jMenuItemSettings = new JMenuItem();  private JMenuItem jMenuItemExit = new JMenuItem();  private JMenuItem jMenuItemInfo = new JMenuItem();  private Dimension frameSize;  private Point frameLocation;  protected JPanel contentPane;  private Object[] frameData;  private int index;  private boolean isBlocked = false;  private MessageBuffer errorMessageSave;  protected MediaPlayer player = new MediaPlayer();  protected SettingsDialog settingsDialog = new SettingsDialog(this);  protected OwnDetailsDialog ownDetailsDialog = new OwnDetailsDialog(this);  protected UserDetailsDialog userDetailsDialog = new UserDetailsDialog(this);  protected AddContactDialog contactDialog = new AddContactDialog(this);  protected InfoDialog infoDialog = new InfoDialog(this);  protected ContactPanel contactPanel = new ContactPanel(this);  protected StatusPanel statusPanel = new StatusPanel(this);  /**   * Initializes a new instance of the class <code>MainFrame</code>.   * @param loginFrame the login frame   */  public MainFrame(LoginFrame loginFrame)  {    ImageIcon imgIcon;    synchronized (Start.env)    {      if (Start.env.isSoundEnabled() && Start.env.getSoundFlags()[0] == true          && !Start.env.getSoundFileNames()[0].equals(""))      {        try        {          player.playSound(Start.env.getSoundFileNames()[0]);        }        catch (Exception ex)        {          System.err.println(this.getClass().getName() + ": " + ex.toString());        }      }    }    boolean visible = loginFrame.isVisible();    if (loginFrame.getExtendedState() == Frame.ICONIFIED)    {      this.setExtendedState(Frame.ICONIFIED);    }    loginFrame.loginPanel.imageRotationPanel.stopImageRotation();    loginFrame.setVisible(false);    loginFrame.dispose();    try    {      this.setIconImage(ImageLoader.loadImage(Resource.ICON_SOURCE_LOGO, this));    }    catch (Exception ex)    {      System.err.println(this.getClass().getName() + ": " + ex.toString());    }    imgIcon = new ImageIcon(Resource.ICON_SOURCE_MENU_ITEMS[1]);    jMenuItemAddContact.setIcon(imgIcon);    jMenuItemAddContact.addActionListener(new ActionListener()    {      public void actionPerformed(ActionEvent e)      {        action_showAddContactWindow();      }    });    imgIcon = new ImageIcon(Resource.ICON_SOURCE_MENU_ITEMS[2]);    jMenuItemOwnDetails.setIcon(imgIcon);    jMenuItemOwnDetails.addActionListener(new ActionListener()    {      public void actionPerformed(ActionEvent e)      {        action_showOwnDetailsWindow();      }    });    imgIcon = new ImageIcon(Resource.ICON_SOURCE_MENU_ITEMS[3]);    jMenuItemSettings.setIcon(imgIcon);    jMenuItemSettings.addActionListener(new ActionListener()    {      public void actionPerformed(ActionEvent e)      {        action_showSettingsWindow();      }    });    imgIcon = new ImageIcon(Resource.ICON_SOURCE_MENU_ITEMS[0]);    jMenuItemExit.setIcon(imgIcon);    jMenuItemExit.addActionListener(new ActionListener()    {      public void actionPerformed(ActionEvent e)      {        action_closeWindow();      }    });    //    imgIcon = new ImageIcon(Resource.ICON_SOURCE_MENU_ITEMS[0]);    //    jMenuItemInfo.setIcon(imgIcon);    jMenuItemInfo.addActionListener(new ActionListener()    {      public void actionPerformed(ActionEvent e)      {        action_showInfoDialog();      }    });    this.setJMenuBar(jMenuBar);    jMenuBar.add(jMenuICQ);    jMenuICQ.add(jMenuItemAddContact);    jMenuICQ.addSeparator();    jMenuICQ.add(jMenuItemOwnDetails);    jMenuICQ.add(jMenuItemSettings);    jMenuICQ.addSeparator();    jMenuICQ.add(jMenuItemExit);    jMenuBar.add(jMenuHelp);    jMenuHelp.add(jMenuItemInfo);    contentPane = (JPanel) this.getContentPane();    contentPane.setLayout(new BorderLayout());    contentPane.add(contactPanel, BorderLayout.CENTER);    contentPane.add(statusPanel, BorderLayout.SOUTH);    synchronized (Start.env)    {      if (Start.env.getConnection() != null          && Start.env.getConnection().isLogged())      {        contactPanel.initAll();        int status;        for (index = 0; index < statusPanel.statusMode.length; index++)        {          status = statusPanel.statusMode[index].getMode();          if (status == Start.env.getLoginStatus())          {            statusPanel.action_setSelectedIndex(index);            statusPanel.imagePanel.setImage(Resource.ICON_SOURCE_STATUS[index],                                            1);            SysTray.setPrimaryIcon(Resource.ICON_SOURCE_TRAY[index]);            break;          }        }      }      this.localize();      try      {        frameData = CryptFile.read(Default.FOLDER_NAME_HOME + "/"                                   + Start.env.getOwnContact().getContactId()                                   + "/" + Default.FILE_NAME_FRAME_DATA);        frameSize = new Dimension(Integer.parseInt(frameData[0].toString()),                                  Integer.parseInt(frameData[1].toString()));        frameLocation = new Point(Integer.parseInt(frameData[2].toString()),                                  Integer.parseInt(frameData[3].toString()));        setSize(frameSize);        setLocation(frameLocation);      }      catch (Exception ex)      {        this.pack();        WindowToolkit.centerWindow(this);        System.err.println(this.getClass().getName() + ": " + ex.toString());      }    }    this.setMinimumWindowSize();    this.setVisible(visible);  }  /**   * (non-Javadoc)   * @see openicq.gui.SysTrayAction#action_closeWindow()   */  public void action_closeWindow()  {    if (!this.isBlocked)    {      synchronized (Start.env)      {        errorMessageSave = new MessageBuffer(false);        try        {          CryptFile.write(Default.FOLDER_NAME_HOME + "/"                          + Start.env.getOwnContact().getContactId() + "/"                          + Default.FILE_NAME_LOGIN_STATUS, new String[]          { String.valueOf(Start.env.getLoginStatus())          });        }        catch (Exception ex)        {          errorMessageSave.append(((String[]) localized.get("errorMessages"))[12]);        }        try        {          Start.env.saveContactList(Default.FOLDER_NAME_HOME + "/"                                    + Start.env.getOwnContact().getContactId()                                    + "/" + Default.FILE_NAME_KNOWN_LIST,                                    Start.env.getKnownList());        }        catch (Exception ex)        {          errorMessageSave.append(((String[]) localized.get("errorMessages"))[9]);        }        try        {          Start.env.saveContactList(Default.FOLDER_NAME_HOME + "/"                                    + Start.env.getOwnContact().getContactId()                                    + "/" + Default.FILE_NAME_UNKNOWN_LIST,                                    Start.env.getUnknownList());        }        catch (Exception ex)        {          errorMessageSave.append(((String[]) localized.get("errorMessages"))[14]);        }        frameSize = this.getSize();        frameLocation = this.getLocation();        try        {          CryptFile.write(Default.FOLDER_NAME_HOME + "/"                          + Start.env.getOwnContact().getContactId() + "/"                          + Default.FILE_NAME_FRAME_DATA, new String[]          {           String.valueOf(frameSize.width),           String.valueOf(frameSize.height),           String.valueOf(frameLocation.x),           String.valueOf(frameLocation.y)          });        }        catch (Exception ex)        {          errorMessageSave.append(((String[]) localized.get("errorMessages"))[7]);        }      }      if (errorMessageSave.getSize() > 0)      {        this.isBlocked = true;        String message, title;        message = ((String[]) localized.get("questionMessages"))[1];        title = ((String[]) localized.get("messageTitles"))[0];        errorMessageSave.append(message);        int option;        option = JOptionPane.showConfirmDialog(this,                                               errorMessageSave.getMessage(),                                               title,                                               JOptionPane.ERROR_MESSAGE,                                               JOptionPane.YES_NO_OPTION);        if (option != JOptionPane.YES_OPTION)        {          this.isBlocked = false;          return;        }      }      dispose();      System.exit(0);    }  }  /**   * (non-Javadoc)   * @see openicq.gui.SysTrayAction#action_maximizeWindow()   */  public void action_maximizeWindow()  {    this.setVisible(true);    if (this.getState() == Frame.ICONIFIED)    {      this.setState(Frame.NORMAL);    }  }  /**   * Action 'Show the add contact dialog'.   */  protected void action_showAddContactWindow()  {    if (showPasswordDialog(((String[]) localized.get("menubar"))[1]) == false)    {      return;    }    this.contactDialog.show();  }  /**   * Action 'Show the info dialog'.   */  protected void action_showInfoDialog()  {    infoDialog.show();  }  /**   * Action 'Show the change details dialog'.   */  protected void action_showOwnDetailsWindow()  {    if (showPasswordDialog(((String[]) localized.get("menubar"))[2]) == false)    {      return;    }    ownDetailsDialog.setVisible(true);  }  /**   * Action 'Show the settings dialog'.   */  protected void action_showSettingsWindow()  {    if (showPasswordDialog(((String[]) localized.get("menubar"))[3]) == false)    {      return;    }    settingsDialog.setVisible(true);  }  /**   * (non-Javadoc)   * @see org.javalib.dynamic.localize.Localizable#localize()   */  public void localize()  {    KeyStroke ks;    char shortkey;    synchronized (Start.env)    {      localized = Start.env.getLocalizedList();      this.setTitle(((String[]) localized.get("mainFrame"))[0] + " ("                    + Start.env.getOwnContact().getContactId() + ")");      // Menu bar      jMenuICQ.setText(((String[]) localized.get("menubar"))[0]);      jMenuICQ.setMnemonic(((char[]) localized.get("menubarShortkeys"))[0]);      jMenuItemAddContact.setText(((String[]) localized.get("menubar"))[1]);      shortkey = ((char[]) localized.get("menubarShortkeys"))[1];      ks = KeyStroke.getKeyStroke(shortkey, Event.CTRL_MASK);      jMenuItemAddContact.setAccelerator(ks);      jMenuItemOwnDetails.setText(((String[]) localized.get("menubar"))[2]);      shortkey = ((char[]) localized.get("menubarShortkeys"))[2];      ks = KeyStroke.getKeyStroke(shortkey, Event.CTRL_MASK);      jMenuItemOwnDetails.setAccelerator(ks);      jMenuItemSettings.setText(((String[]) localized.get("menubar"))[3]);      shortkey = ((char[]) localized.get("menubarShortkeys"))[3];      ks = KeyStroke.getKeyStroke(shortkey, Event.CTRL_MASK);      jMenuItemSettings.setAccelerator(ks);      jMenuItemExit.setText(((String[]) localized.get("menubar"))[4]);      shortkey = ((char[]) localized.get("menubarShortkeys"))[4];      ks = KeyStroke.getKeyStroke(shortkey, Event.CTRL_MASK);      jMenuItemExit.setAccelerator(ks);      jMenuHelp.setText(((String[]) localized.get("menubar"))[5]);      jMenuHelp.setMnemonic(((char[]) localized.get("menubarShortkeys"))[5]);      jMenuItemInfo.setText(((String[]) localized.get("menubar"))[6]);      shortkey = ((char[]) localized.get("menubarShortkeys"))[6];      ks = KeyStroke.getKeyStroke(shortkey, Event.CTRL_MASK);      jMenuItemInfo.setAccelerator(ks);      contactPanel.knownMenu = new ContactPanelPopupMenu(contactPanel);      contactPanel.knownMenu.remove(contactPanel.knownMenu.jMenuItemAdd);      contactPanel.unknownMenu = new ContactPanelPopupMenu(contactPanel);      contactPanel.initContacts();      statusPanel.updateComboBox();      int index = statusPanel.jComboBoxStatus.getSelectedIndex();      String icon = Resource.ICON_SOURCE_TRAY[index];      String tooltip = ((String[]) localized.get("mainFrame"))[0] + " ("                       + Start.env.getOwnContact().getContactId() + ")";      SysTray.createSystemTray(icon, tooltip, this);    }  }  /**   * (non-Javadoc)   * @see java.awt.Window#processWindowEvent(java.awt.event.WindowEvent)   */  protected void processWindowEvent(WindowEvent e)  {    if (e.getID() == WindowEvent.WINDOW_CLOSING && SysTray.hasSysTrayMenu())    {      this.setVisible(false);    }    else if (e.getID() == WindowEvent.WINDOW_CLOSING)    {      this.action_closeWindow();    }  }  /**   * Sets the minimum window size of this frame.   */  public void setMinimumWindowSize()  {    Dimension savedSize = this.getSize();    this.pack();    try    {      org.javalib.gui.WindowToolkit.setMinimumWindowSize(this);    }    catch (Exception ex)    {      System.err.println(this.getClass().getName() + ": " + ex.toString());    }    this.setSize(savedSize);  }  /**   * Shows a password dialog.   * @param title the title of the password dialog   * @return true if the entered password was correct   */  public boolean showPasswordDialog(String title)  {    String ok, cancel;    String msg;    String errtitle;    if (Start.env.getSecurityLevel() > Settings.LOW_SECURITY)    {      ok = ((String[]) localized.get("stdButtons"))[0];      cancel = ((String[]) localized.get("stdButtons"))[1];      msg = ((String[]) localized.get("passwordDialog"))[0];      String result = OptionPane.showPasswordDialog(this, ok, cancel, msg,                                                    title);      if (result == null)      {        return false;      }      else if (!result.equals(Start.env.getPassword()))      {        msg = ((String[]) localized.get("errorMessages"))[5];        errtitle = ((String[]) localized.get("messageTitles"))[0];        JOptionPane.showMessageDialog(this, msg, errtitle,                                      JOptionPane.ERROR_MESSAGE);        return false;      }    }    return true;  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -