make arraylist< jtextarea>到jscrollpane

bfhwhh0e  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(392)

我有一个数组列表的'n'不同的聊天,是 Package 与宽度下面是一个textarea块的代码

JTextArea chat2 = new JTextArea();
 chat2.setText("this is chat 2"); 
 chat2.setWrapStyleWord(true);
 chat2.setLineWrap(true);
 chat.setColumns(20); 
 chat2.setEditable(false);
 chat2.setBorder(borderLine);   
 chat2.setVisible(true);

以及一个数组列表

ArrayList<JTextArea> c = new ArrayList<JTextArea>();
c.add(chat2)

已经试过了

Vector itemsVector = new Vector(c);
JList chats = new JList(itemsVector);
 //CHAT PANEL SCROLLABLE
 JScrollPane scrollChats = new JScrollPane(chats);
 scrollChats.setBounds(0,60,1100,600);
 main.add(scrollChats);

它没有工作,而是得到了这个输出:

2jcobegt

2jcobegt1#

因此,假设一个chat类包含一个字符串,该字符串表示该用户正在发送的chatter(或者更好地说,是一个定义良好的用户类型对象)以及文本:

public class Chat {
    String name;
    String text;

    public Chat(String name, String text) {
        super();
        this.name = name;
        this.text = text;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

}

然后,您可以创建一个列表模型和一个jlist来保存这种类型的对象:

private DefaultListModel<Chat> chatListModel = new DefaultListModel<Chat>();
private JList<Chat> chatList = new JList<Chat>(chatListModel);

然后,可以创建一个渲染器,将chatter的文本显示为默认渲染器文本,并将chatter的名称显示在单个渲染器周围的有标题的边框中(默认渲染器是jlabel):

private class ChatRenderer extends DefaultListCellRenderer {
    @Override
    public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected,
            boolean cellHasFocus) {
        Chat chat = (Chat) value;
        String text = chat.getText();
        String name = chat.getName();
        JLabel rendererComponent = (JLabel) super.getListCellRendererComponent(list, text, index, isSelected, cellHasFocus);
        rendererComponent.setBorder(BorderFactory.createTitledBorder(name));
        return rendererComponent;
    }
}

整个事情可能看起来像:

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.*;

@SuppressWarnings("serial")
public class FooGui01 extends JPanel {
    private DefaultListModel<Chat> chatListModel = new DefaultListModel<Chat>();
    private JList<Chat> chatList = new JList<Chat>(chatListModel);
    private JTextField inputField = new JTextField(20);
    private JButton submitButton = new JButton("Submit");

    public FooGui01() {
        chatList.setCellRenderer(new ChatRenderer());
        chatList.setPrototypeCellValue(new Chat("John", "abc def ghi jkl abc def ghi jkl abc def ghi jkl abc def ghi jkl abc def ghi jkl abc def ghi jkl "));
        chatList.setVisibleRowCount(10);
        JScrollPane scrollPane = new JScrollPane(chatList);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

        ActionListener listener = new SubmitListener();
        inputField.addActionListener(listener);
        submitButton.addActionListener(listener);
        submitButton.setMnemonic(KeyEvent.VK_S);

        JPanel submitPanel = new JPanel();
        submitPanel.setLayout(new BoxLayout(submitPanel, BoxLayout.LINE_AXIS));
        submitPanel.add(inputField);
        submitPanel.add(submitButton);

        setLayout(new BorderLayout());
        add(scrollPane);
        add(submitPanel, BorderLayout.PAGE_END);
    }

    // public method called by outside code that is wanting to pass chat to you
    public void submitChat(Chat chat) {
        if (SwingUtilities.isEventDispatchThread()) {
            chatListModel.addElement(chat);
        } else {
            SwingUtilities.invokeLater(() -> chatListModel.addElement(chat));
        }
    }

    private class SubmitListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            String text = inputField.getText();
            chatListModel.addElement(new Chat("Me", text));
            inputField.selectAll();
            inputField.requestFocusInWindow();

            // here you would send the chat text or object to a socket's output stream
        }
    }

    private class ChatRenderer extends DefaultListCellRenderer {
        @Override
        public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected,
                boolean cellHasFocus) {
            Chat chat = (Chat) value;
            String text = chat.getText();
            String name = chat.getName();
            JLabel rendererComponent = (JLabel) super.getListCellRendererComponent(list, text, index, isSelected, cellHasFocus);
            rendererComponent.setBorder(BorderFactory.createTitledBorder(name));
            return rendererComponent;
        }
    }

    public static void main(String[] args) {        
        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame("Foo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            FooGui01 gui = new FooGui01();
            frame.add(gui);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        });
    }
}

相关问题