chatpanel.java

来自「jxme的一些相关程序,主要是手机上程序开发以及手机和计算机通信的一些程序资料,」· Java 代码 · 共 1,285 行 · 第 1/3 页

JAVA
1,285
字号
        Button buddiesButton = new Button(BUTTON_BUDDIES);
        Button settingsButton = new Button(BUTTON_SETTINGS);

        BuddyPanel() {
            GridBagLayout gridbag = new GridBagLayout();
            setLayout(gridbag);

            GridBagConstraints c = new GridBagConstraints();
            c.insets = new Insets(4, 4, 4, 4);

            c.gridx = 0;
            c.gridwidth = 2;
            c.weightx = 1.0;
            c.weighty = 1.0;
            c.fill = GridBagConstraints.BOTH;
            gridbag.setConstraints(buddyList, c);

            c.gridwidth = 1;
            c.fill = GridBagConstraints.NONE;
            gridbag.setConstraints(buddiesButton, c);

            c.gridx = 1;
            gridbag.setConstraints(settingsButton, c);

            add(buddyList);
            add(buddiesButton);
            add(settingsButton);

            if (DEFAULT_TEXTCOMPONENT_BG != null) {
                buddyList.setBackground(DEFAULT_TEXTCOMPONENT_BG);
            }

            if (DEFAULT_BUTTON_BG != null) {
                buddiesButton.setBackground(DEFAULT_BUTTON_BG);
                settingsButton.setBackground(DEFAULT_BUTTON_BG);
            }
        }
    }

    static class BuddyAdminPanel extends Panel {
        List buddyList = new List(2, true);
        Button removeButton = new Button(BUTTON_REMOVE);
        Button addButton = new Button(BUTTON_ADDING);
        Button doneButton = new Button(BUTTON_DONE);

        BuddyAdminPanel() {
            GridBagLayout gridbag = new GridBagLayout();
            setLayout(gridbag);

            GridBagConstraints c = new GridBagConstraints();
            c.insets = new Insets(4, 4, 4, 4);

            c.gridx = 0;
            c.gridwidth = 3;
            c.weightx = 1.0;
            c.weighty = 1.0;
            c.fill = GridBagConstraints.BOTH;
            gridbag.setConstraints(buddyList, c);

            c.gridwidth = 1;
            c.fill = GridBagConstraints.NONE;
            gridbag.setConstraints(removeButton, c);

            c.gridx = 1;
            gridbag.setConstraints(addButton, c);

            c.gridx = 2;
            gridbag.setConstraints(doneButton, c);

            add(buddyList);
            add(removeButton);
            add(addButton);
            add(doneButton);

            if (DEFAULT_TEXTCOMPONENT_BG != null) {
                buddyList.setBackground(DEFAULT_TEXTCOMPONENT_BG);
            }

            if (DEFAULT_BUTTON_BG != null) {
                removeButton.setBackground(DEFAULT_BUTTON_BG);
                addButton.setBackground(DEFAULT_BUTTON_BG);
                doneButton.setBackground(DEFAULT_BUTTON_BG);
            }
        }
    }

    static class BuddyAddPanel extends Panel {
        TextField queryField = new TextField();
        Button searchButton = new Button(BUTTON_SEARCH);
        List resultList = new List(2, true);
        Button cancelButton = new Button(BUTTON_CANCEL);
        Button addButton = new Button(BUTTON_ADD);

        BuddyAddPanel() {
            GridBagLayout gridbag = new GridBagLayout();
            setLayout(gridbag);

            GridBagConstraints c = new GridBagConstraints();
            c.insets = new Insets(4, 4, 4, 4);

            c.gridx = 0;
            c.gridwidth = 2;
            c.weightx = 1.0;
            c.fill = GridBagConstraints.HORIZONTAL;
            gridbag.setConstraints(queryField, c);

            c.gridwidth = 2;
            c.fill = GridBagConstraints.NONE;
            gridbag.setConstraints(searchButton, c);

            c.weighty = 1.0;
            c.fill = GridBagConstraints.BOTH;
            gridbag.setConstraints(resultList, c);

            c.gridwidth = 1;
            c.fill = GridBagConstraints.NONE;
            gridbag.setConstraints(cancelButton, c);

            c.gridx = 1;
            gridbag.setConstraints(addButton, c);

            add(queryField);
            add(searchButton);
            add(resultList);
            add(cancelButton);
            add(addButton);

            if (DEFAULT_TEXTCOMPONENT_BG != null) {
                queryField.setBackground(DEFAULT_TEXTCOMPONENT_BG);
                resultList.setBackground(DEFAULT_TEXTCOMPONENT_BG);
            }

            if (DEFAULT_BUTTON_BG != null) {
                searchButton.setBackground(DEFAULT_BUTTON_BG);
                cancelButton.setBackground(DEFAULT_BUTTON_BG);
                addButton.setBackground(DEFAULT_BUTTON_BG);
            }
        }
    }

    static class MessengerPanel extends Panel implements ItemListener {
        Choice textChoice = new Choice();
        TextArea logArea = new TextArea("", 4, 10, TextArea.SCROLLBARS_VERTICAL_ONLY);
        TextField inputField = new TextField("", 10);
        Button sendButton = new Button(BUTTON_SEND);
        Button imageButton = new Button(BUTTON_IMAGE);
        Button byeButton = new Button(BUTTON_BYE);

        MessengerPanel() {
            GridBagLayout gridbag = new GridBagLayout();
            setLayout(gridbag);

            GridBagConstraints c = new GridBagConstraints();
            c.insets = new Insets(4, 4, 4, 4);

            c.gridx = 0;
            c.gridwidth = 3;
            c.weightx = 1.0;
            c.weighty = 1.0;
            c.fill = GridBagConstraints.BOTH;
            gridbag.setConstraints(logArea, c);

            c.weighty = 0.0;
            c.fill = GridBagConstraints.HORIZONTAL;
            gridbag.setConstraints(textChoice, c);
            gridbag.setConstraints(inputField, c);

            c.gridwidth = 1;
            c.fill = GridBagConstraints.NONE;
            gridbag.setConstraints(sendButton, c);

            c.gridx = 1;
            gridbag.setConstraints(imageButton, c);

            c.gridx = 2;
            gridbag.setConstraints(byeButton, c);

            add(logArea);
            add(textChoice);
            add(inputField);
            add(sendButton);
            add(imageButton);
            add(byeButton);

            logArea.setEditable(false);
            textChoice.addItemListener(this);

            for (int i = 0; i < CANNED_PHRASES.length; i++) {
                textChoice.add(CANNED_PHRASES[i]);
            }

            if (DEFAULT_TEXTCOMPONENT_BG != null) {
                textChoice.setBackground(DEFAULT_TEXTCOMPONENT_BG);
                logArea.setBackground(DEFAULT_TEXTCOMPONENT_BG);
                inputField.setBackground(DEFAULT_TEXTCOMPONENT_BG);
            }

            if (DEFAULT_BUTTON_BG != null) {
                sendButton.setBackground(DEFAULT_BUTTON_BG);
                imageButton.setBackground(DEFAULT_BUTTON_BG);
                byeButton.setBackground(DEFAULT_BUTTON_BG);
            }
        }

        public void itemStateChanged(ItemEvent evt) {
            if (DEBUG) {
                System.out.println("itemStateChanged " + evt);
            }

            if (evt.getSource() == textChoice) {
                int index = textChoice.getSelectedIndex();

                if (index < NUMBER_PHRASES) {
                    String text = inputField.getText();
                    int start = inputField.getSelectionStart();
                    int end = inputField.getSelectionEnd();

                    if (DEBUG) {
                        System.out.println("start=" + start + " end=" + end);
                        System.out.println("start=" + text.substring(0, start) + 
                                           " end=" + text.substring(end));
                    }

                    inputField.setText(text.substring(0, start) + 
                                       textChoice.getSelectedItem() +
                                       text.substring(end));
                }
            }
        }
    }

    static class SettingsPanel extends Panel implements TextListener {
        TextField userField = new TextField();
        Label userLabel = new Label("username");
        TextField relayField = new TextField();
        Label relayLabel = new Label("relay");
        Button doneButton = new Button(BUTTON_DONE);

        SettingsPanel() {
            GridBagLayout gridbag = new GridBagLayout();
            setLayout(gridbag);

            GridBagConstraints c = new GridBagConstraints();
            c.insets = new Insets(4, 4, 4, 4);

            c.gridx = 0;
            c.gridwidth = 1;
            gridbag.setConstraints(userLabel, c);

            c.gridx = 1;
            c.gridwidth = GridBagConstraints.REMAINDER;
            c.weightx = 1.0;
            c.fill = GridBagConstraints.HORIZONTAL;
            gridbag.setConstraints(userField, c);

            c.gridx = 0;
            c.gridwidth = 1;
            c.weightx = 0.0;
            c.fill = GridBagConstraints.NONE;
            gridbag.setConstraints(relayLabel, c);

            c.gridx = 1;
            c.gridwidth = GridBagConstraints.REMAINDER;
            c.weightx = 1.0;
            c.fill = GridBagConstraints.HORIZONTAL;
            gridbag.setConstraints(relayField, c);

            c.gridwidth = 1;
            c.weightx = 0.0;
            c.fill = GridBagConstraints.NONE;
            gridbag.setConstraints(doneButton, c);

            add(userField);
            add(userLabel);
            add(relayField);
            add(relayLabel);
            add(doneButton);

            userField.addTextListener(this);

            if (DEFAULT_TEXTCOMPONENT_BG != null) {
                userField.setBackground(DEFAULT_TEXTCOMPONENT_BG);
                relayField.setBackground(DEFAULT_TEXTCOMPONENT_BG);
            }

            if (DEFAULT_BUTTON_BG != null) {
                doneButton.setBackground(DEFAULT_BUTTON_BG);
            }
        }

        public void textValueChanged(TextEvent evt) {
            if (userField.getText().length() > 0) {
                if (!doneButton.isEnabled()) {
                    doneButton.setEnabled(true);
                }
            } else {
                if (doneButton.isEnabled()) {
                    doneButton.setEnabled(false);
                }
            }
        }
    }

    static class ImageViewPanel extends Panel {
        ImagePanel imagePanel = new ImagePanel(this);
        Label imageLabel = new Label();
        Button okButton = new Button(BUTTON_OK);

        ImageViewPanel() {
            GridBagLayout gridbag = new GridBagLayout();
            setLayout(gridbag);

            GridBagConstraints c = new GridBagConstraints();
            c.insets = new Insets(4, 4, 4, 4);

            c.gridx = 0;
            c.gridwidth = 1;
            c.fill = GridBagConstraints.HORIZONTAL;
            gridbag.setConstraints(imageLabel, c);

            c.gridy = 1;
            c.weightx = 1.0;
            c.weighty = 1.0;
            c.fill = GridBagConstraints.BOTH;
            gridbag.setConstraints(imagePanel, c);

            c.gridy = 2;
            c.gridwidth = 1;
            c.weightx = 0.0;
            c.weighty = 0.0;
            c.fill = GridBagConstraints.NONE;
            gridbag.setConstraints(okButton, c);

            add(imageLabel);
            add(imagePanel);
            add(okButton);

            if (DEFAULT_TEXTCOMPONENT_BG != null) {
                imageLabel.setBackground(DEFAULT_TEXTCOMPONENT_BG);
                imagePanel.setBackground(DEFAULT_TEXTCOMPONENT_BG);
            }

            if (DEFAULT_BUTTON_BG != null) {
                okButton.setBackground(DEFAULT_BUTTON_BG);
            }
        }

        public void setImage(byte[] data, String name) {
            if (data != null) {
                imagePanel.setImage(data);
                imageLabel.setText(name);
            }
        }
    }

    static class ImagePanel extends Panel implements ImageObserver {

        Panel parent = null;
        Image image = null;
        int width = 0;
        int height = 0;

        public ImagePanel(Panel parent) {
            this.parent = parent;
        }

        public void setImage(byte[] data) {
            image = java.awt.Toolkit.getDefaultToolkit().createImage(data);
            width = 0;
            height = 0;

            if (parent != null) {
                parent.repaint();
            }
            repaint();
        }

        public void paint(java.awt.Graphics g) {
            if (image != null) {
                Dimension d = getSize();

                g.drawImage(image, 
                            (d.width - width)/2, 
                            (d.height - height)/2, 
                            this);
            }
        }

        public boolean imageUpdate(java.awt.Image img,
                                   int infoflags,
                                   int x,
                                   int y,
                                   int width,
                                   int height) {
            this.width = width;
            this.height = height;

            if (parent != null) {
                parent.repaint();
            }
            repaint();

            return true;
        }
    }

    public int getBuddyCount() {
        return buddyList.size();
    }

    public Destination getBuddy(int index) {
        return (Destination)buddyList.elementAt(index);
    }

    protected void addBuddy(Destination buddy) {
        if (!"".equals(buddy.name) && !buddyList.contains(buddy)) {
            buddyList.addElement(buddy);
            buddyPanel.buddyList.add(buddy.name);
            buddyAdminPanel.buddyList.add(buddy.name);
        }
    }

    public Destination getUser() {
        if (user == null) {
            return new Destination(null, null, PIPE_TYPE_UNICAST, "");
        } else {
            return user;
        }
    }

⌨️ 快捷键说明

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