Ejemplo n.º 1
0
  public JChat() {
    this.setSize(500, 600);
    this.setResizable(false);
    this.setLayout(new BorderLayout());

    JPanel topPanel = new JPanel();
    topPanel.setLayout(new GridLayout(2, 1));

    // set up buttons
    openChat = new JButton("Open to chat");
    openChat.addActionListener(new OpenChat());
    chatWith = new JButton("Chat with");
    chatWith.addActionListener(new ChatWith());
    send = new JButton("send");
    send.addActionListener(new Send());
    send.setEnabled(false);
    InputMap inputMap = send.getInputMap(JButton.WHEN_IN_FOCUSED_WINDOW);
    KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
    inputMap.put(enter, "ENTER");
    send.getActionMap().put("ENTER", new ClickAction(send));

    // set up labels
    pickPort = new JLabel();
    pickPort.setText("Pick your port number:");
    desPort = new JLabel();
    desPort.setText("Or enter a destinaltion port number:");

    // set up text fields
    pickText = new JTextField();
    pickText.setPreferredSize(new Dimension(150, 30));
    desText = new JTextField();
    desText.setPreferredSize(new Dimension(150, 30));
    chatText = new JTextField();
    chatText.setPreferredSize(new Dimension(400, 30));
    chatText.setEnabled(false);

    JPanel top1 = new JPanel();
    top1.add(pickPort);
    top1.add(pickText);
    top1.add(openChat);

    JPanel top2 = new JPanel();
    top2.add(desPort);
    top2.add(desText);
    top2.add(chatWith);

    topPanel.add(top1);
    topPanel.add(top2);

    chatField = new JTextArea();
    chatField.setAutoscrolls(true);
    chatField.setDragEnabled(true);
    chatField.setEditable(false);
    chatField.setAlignmentY(TOP_ALIGNMENT);

    JPanel bottomPanel = new JPanel();
    bottomPanel.add(chatText);
    bottomPanel.add(send);

    this.add(topPanel, BorderLayout.NORTH);
    this.add(chatField, BorderLayout.CENTER);
    this.add(bottomPanel, BorderLayout.SOUTH);

    this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
Ejemplo n.º 2
0
  public ChatClient() {
    super(APPNAME);

    JPanel topPanel = new JPanel();
    JPanel leftPanel = new JPanel();
    JPanel buttomPanel = new JPanel();

    JPanel roomPanel = new JPanel();
    JPanel userPanel = new JPanel();

    roomList = new JList();
    userList = new JList();
    msgTextArea = new JTextArea();
    msgTextField = new JTextField();
    nameTextField = new JTextField();
    submitButton = new JButton("送信");
    renameButton = new JButton("名前の変更");
    addRoomButton = new JButton("部屋を追加");
    enterRoomButton = new JButton("入室");

    submitButton.addActionListener(this);
    submitButton.setActionCommand("submit");

    renameButton.addActionListener(this);
    renameButton.setActionCommand("rename");

    addRoomButton.addActionListener(this);
    addRoomButton.setActionCommand("addRoom");

    enterRoomButton.addActionListener(this);
    enterRoomButton.setActionCommand("enterRoom");

    roomPanel.setLayout(new BorderLayout());
    roomPanel.add(new JLabel("チャットルーム"), BorderLayout.NORTH);
    roomPanel.add(new JScrollPane(roomList), BorderLayout.CENTER);
    roomPanel.add(enterRoomButton, BorderLayout.SOUTH);

    userPanel.setLayout(new BorderLayout());
    userPanel.add(new JLabel("参加ユーザー"), BorderLayout.NORTH);
    userPanel.add(new JScrollPane(userList), BorderLayout.CENTER);

    topPanel.setLayout(new FlowLayout());
    topPanel.add(new JLabel("名前"));
    topPanel.add(nameTextField);
    topPanel.add(renameButton);
    topPanel.add(addRoomButton);

    nameTextField.setPreferredSize(new Dimension(200, nameTextField.getPreferredSize().height));

    leftPanel.setLayout(new GridLayout(2, 1));
    leftPanel.add(roomPanel);
    leftPanel.add(userPanel);

    buttomPanel.setLayout(new BorderLayout());
    buttomPanel.add(msgTextField, BorderLayout.CENTER);
    buttomPanel.add(submitButton, BorderLayout.EAST);

    // テキストエリアはメッセージを表示するだけなので編集不可に設定
    msgTextArea.setEditable(false);

    // コンポーネントの状態を退室状態で初期化
    exitedRoom();

    this.getContentPane().add(new JScrollPane(msgTextArea), BorderLayout.CENTER);
    this.getContentPane().add(topPanel, BorderLayout.NORTH);
    this.getContentPane().add(leftPanel, BorderLayout.WEST);
    this.getContentPane().add(buttomPanel, BorderLayout.SOUTH);

    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.addWindowListener(
        new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
            try {
              close();
            } catch (Exception err) {
            }
          }
        });
    connectServer();

    // メッセージ受信監視用のスレッドを生成してスタートさせる
    thread = new Thread(this);
    thread.start();

    // 現在の部屋を取得する
    sendMessage("getRooms");
  }