您好,我正在创建一个简单的 Java 服务器聊天,但我根本无法让 swing 很好地处理长字符串。
.
我不想要水平滚动条,我希望单词在需要时中断并流到另一行。
我用来创建 JTextPane 的代码是:
super("Message Server");
userText = new JTextField();
userText.setEditable(false);
userText.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
sendMessage(event.getActionCommand());
userText.setText("");
}
}
);
getContentPane().add(userText, BorderLayout.SOUTH);
chatWindow = new JTextPane();
JScrollPane scrollPane = new JScrollPane(chatWindow);
getContentPane().add(scrollPane);
setSize(300, 450); //Sets the window size
setVisible(true);
chatWindow.setEditable(false);
最佳答案
您可以使用 JTextArea
和相应的换行相关方法 setLineWrap()
和 setWrapStyleWord()
,而不是使用 JTextPane
> 实现你想要的。
是的,您可以使用JTextArea
设置字体
例如。
JTextArea txtArea = new JTextArea();
Font font = new Font("Verdana", Font.BOLD, 12);
txtArea.setFont(font);
txtArea.setForeground(Color.BLUE);
txtArea.setText("Hellow World!");
关于java - JTextPane 拒绝对长字符串进行自动换行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29192331/