JTextPane 拒绝自动换行长字符串
JTextPane refuses to word wrap a long string
您好,我正在创建一个简单的 Java-服务器聊天,但我无法很好地使用长字符串。
.
我不想要水平滚动条,我希望单词在需要时中断并流到另一行。
我用来创建 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 并调用 setWrapStyleWord(true);在它的实例上,这应该可以满足您的需求。
您可以使用 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 的代码是:
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 并调用 setWrapStyleWord(true);在它的实例上,这应该可以满足您的需求。
您可以使用 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!");