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

📄 loginframe.java

📁 java chat java chat简易聊天室程序源代码,有很高的参考价值,能在它的基础上进行2次开发.
💻 JAVA
字号:
/* * LoginFrame.java * * Created on 2008年4月21日, 上午9:27 */package chat.client;import chat.common.ChatModelListener;import chat.common.LoginMessage;import chat.common.LogoutMessage;import chat.common.TextMessage;import chat.common.UpdateUserMessage;import chat.common.User;import chat.common.UserListMessage;import java.net.InetAddress;import java.net.InetSocketAddress;import java.net.UnknownHostException;import java.util.logging.Level;import java.util.logging.Logger;import javax.swing.JOptionPane;/** * * @author  Administrator */public class LoginFrame extends javax.swing.JFrame implements ChatModelListener {    private ChatFrame f;    private DefaultClientModel model;    /** Creates new form LoginFrame */    public LoginFrame() {        initComponents();        model = DefaultClientModel.getInstance();        f = new ChatFrame();        model.addChatModelListener(this);    }    /** This method is called from within the constructor to     * initialize the form.     * WARNING: Do NOT modify this code. The content of this method is     * always regenerated by the Form Editor.     */    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents    private void initComponents() {        jLabel1 = new javax.swing.JLabel();        server = new javax.swing.JTextField();        jLabel2 = new javax.swing.JLabel();        port = new javax.swing.JTextField();        jLabel3 = new javax.swing.JLabel();        user = new javax.swing.JTextField();        jLabel4 = new javax.swing.JLabel();        password = new javax.swing.JTextField();        login = new javax.swing.JButton();        exit = new javax.swing.JButton();        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);        jLabel1.setText("服务器:");        server.setText("127.0.0.1");        jLabel2.setText("端口号:");        port.setText("3000");        jLabel3.setText("用户:");        user.setText("tom");        jLabel4.setText("密码:");        login.setText("登录");        login.addActionListener(new java.awt.event.ActionListener() {            public void actionPerformed(java.awt.event.ActionEvent evt) {                loginActionPerformed(evt);            }        });        exit.setText("退出");        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());        getContentPane().setLayout(layout);        layout.setHorizontalGroup(            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)            .addGroup(layout.createSequentialGroup()                .addContainerGap()                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)                    .addGroup(layout.createSequentialGroup()                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)                                .addGroup(layout.createSequentialGroup()                                    .addComponent(jLabel1)                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)                                    .addComponent(server, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE))                                .addGroup(layout.createSequentialGroup()                                    .addComponent(jLabel2)                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)                                    .addComponent(port)))                            .addGroup(layout.createSequentialGroup()                                .addComponent(jLabel3)                                .addGap(18, 18, 18)                                .addComponent(user, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE))                            .addGroup(layout.createSequentialGroup()                                .addComponent(jLabel4)                                .addGap(18, 18, 18)                                .addComponent(password, javax.swing.GroupLayout.DEFAULT_SIZE, 165, Short.MAX_VALUE)))                        .addContainerGap())                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()                        .addComponent(login)                        .addGap(18, 18, 18)                        .addComponent(exit)                        .addGap(52, 52, 52))))        );        layout.setVerticalGroup(            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)            .addGroup(layout.createSequentialGroup()                .addContainerGap()                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)                    .addComponent(jLabel1)                    .addComponent(server, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)                    .addComponent(jLabel2)                    .addComponent(port, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)                    .addComponent(jLabel3)                    .addComponent(user, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)                    .addComponent(jLabel4)                    .addComponent(password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 25, Short.MAX_VALUE)                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)                    .addComponent(exit)                    .addComponent(login))                .addContainerGap())        );        pack();    }// </editor-fold>//GEN-END:initComponents    private void loginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loginActionPerformed        try {            //链接服务器            model.connect(new InetSocketAddress(InetAddress.getByName(server.getText()), Integer.parseInt(port.getText())));            //启动接受线程            model.startRecvWorker();            //发送登录消息            LoginMessage message = new LoginMessage();            User loginUser = new User(user.getText(), password.getText());            model.user = loginUser;            message.setUser(loginUser);            model.sendMsg(message);//GEN-LAST:event_loginActionPerformed        } catch (UnknownHostException ex) {            Logger.getLogger(LoginFrame.class.getName()).log(Level.SEVERE, null, ex);        }    }    /**     * @param args the command line arguments     */    public static void main(String args[]) {        java.awt.EventQueue.invokeLater(new Runnable() {            public void run() {                new LoginFrame().setVisible(true);            }        });    }    // Variables declaration - do not modify//GEN-BEGIN:variables    private javax.swing.JButton exit;    private javax.swing.JLabel jLabel1;    private javax.swing.JLabel jLabel2;    private javax.swing.JLabel jLabel3;    private javax.swing.JLabel jLabel4;    private javax.swing.JButton login;    private javax.swing.JTextField password;    private javax.swing.JTextField port;    private javax.swing.JTextField server;    private javax.swing.JTextField user;    // End of variables declaration//GEN-END:variables    public void doLogin(LoginMessage message) {        switch (message.getStatus()) {            case LoginMessage.LOGIN_OK:                this.setVisible(false);                f.setTitle("["+message.getUser()+"]");                f.setVisible(true);                model.addChatModelListener(f);                break;            case LoginMessage.LOGIN_FAILED:                JOptionPane.showMessageDialog(this, message.getMessage());        }    }    public void doLogout(LogoutMessage message) {        throw new UnsupportedOperationException("Not supported yet.");    }    public void doUpdateUser(UpdateUserMessage message) {        throw new UnsupportedOperationException("Not supported yet.");    }    public void doUserList(UserListMessage message) {        throw new UnsupportedOperationException("Not supported yet.");    }    public void doTextMessage(TextMessage message) {        throw new UnsupportedOperationException("Not supported yet.");    }}

⌨️ 快捷键说明

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