// Конструктор класса public Log() { text = new JTextArea(); // Добавляем текст на панель text.setAlignmentX(LEFT_ALIGNMENT); // Позиционируем её налево text.setAlignmentY(TOP_ALIGNMENT); // и прижимаем к верху text.setBackground(Color.BLACK); // Задний фон - чёрный text.setForeground(Color.WHITE); // Цвет текста - белый text.setFont(References.LOG_FONT); // Стиль шрифта из файла References text.setLineWrap(true); // Устанавливаем перенос строк text.setWrapStyleWord(true); // И слов JScrollPane scrollPane = new JScrollPane(text); // Создаём ScrollBar scrollPane.setPreferredSize(new Dimension(500, 200)); // Ограничиваем размеры области вывода this.add(scrollPane); // Добавляем к нашей импровизированной консоли this.setVisible(true); // Отрисовываем }
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); }