java - Java Chatbox appending additional white space
问题描述
I have a simple Chatbox that has the following code:
public class Chatbox extends JFrame
{
JPanel mainPanel;
JTextArea inputField;
JTextArea chatTextArea;
JLabel chatLabel;
boolean typing;
Timer t;
public Chatbox()
{
createAndShowGUI();
}
private void createAndShowGUI()
{
// Set frame properties
setTitle("Plain Text Editor - <ID>");
setDefaultCloseOperation(EXIT_ON_CLOSE);
// Create a JPanel and set layout
mainPanel=new JPanel();
mainPanel.setLayout(new GridLayout(0,1));
// Create JTextField, add it.
inputField=new JTextArea();
//inputField.setWrapStyleWord(true);
inputField.setLineWrap(true);
JScrollPane sp = new JScrollPane(inputField, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
mainPanel.add(sp);
// Add panel to the south,
add(mainPanel,BorderLayout.SOUTH);
// Add a KeyListener
inputField.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent ke)
{
// If he presses enter, add text to chat textarea
if(ke.getKeyCode()==KeyEvent.VK_ENTER) showLabel(inputField.getText());
}
});
// Create a textarea
chatTextArea=new JTextArea();
// Make it non-editable
chatTextArea.setEditable(false);
// Set some margin, for the text
chatTextArea.setMargin(new Insets(7,7,7,7));
chatTextArea.setLineWrap(true);
chatTextArea.setWrapStyleWord(true);
// Set a scrollpane
JScrollPane js=new JScrollPane(chatTextArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
add(js);
addWindowListener(new WindowAdapter(){
public void windowOpened(WindowEvent we)
{
// Get the focus when window is opened
inputField.requestFocus();
}
});
setSize(400,400);
setLocationRelativeTo(null);
setVisible(true);
}
private void showLabel(String text)
{
// If text is empty return
if(text.trim().isEmpty()) return;
// Otherwise, append text with a new line
chatTextArea.append(text+"\n");
// Set textfield and label text to empty string
inputField.setText("");
}
}
However, there is an additional space being appended when I press enter into the TextArea. How can I make it so that messages are being appended separately without an additional white white space?
I'm also not sure if the JScrollPane is messing up the spacing as this issue was not happening before I implemented the JScrollPane.
解决方案
chatTextArea.append(text+"\n");
每次添加新文本时都会添加一个新行。
相反,您可以考虑在文本前加上一个新行(当文本已经存在于聊天窗口中时),例如...
private void showLabel(String text) {
// If text is empty return
if (text.trim().isEmpty()) {
return;
}
if (!chatTextArea.getText().isEmpty()) {
text = "\n" + text;
}
// Otherwise, append text with a new line
chatTextArea.append(text);
// Set textfield and label text to empty string
inputField.setText("");
}
您的输入也将增加一条新线JTextArea
(这让我花了一些时间才意识到,因为我期待 a JTextField
)
发生这种情况是因为KeyEvent.VK_ENTER
最有可能在 your 之前由JTextArea
(及其底层Document
)处理KeyListener
,这意味着新行现在实际上是文本的一部分。
您可能会考虑在将文本发送到showLabel
方法之前对其进行修剪,例如...
inputField.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent ke) {
// If he presses enter, add text to chat textarea
if (ke.getKeyCode() == KeyEvent.VK_ENTER) {
showLabel(inputField.getText().trim());
}
}
});
但是,更好的解决方案是尝试String
手动trim
删除末尾的空格,以及删除前导和尾随空格,但我会让你弄清楚
推荐阅读
- scripting - 如何为超过 3 个统计数据的排行榜创建保存系统
- python - 如何从 django 的查询集中检索值?
- sql - Teradata - 滞后列和 UNBOUNDED PRECEDING
- wordpress - 如何在 Wordpress 中为自定义帖子类型加载自定义样式表?
- laravel-backpack - 将父数据传递到与 laravel 背包中的嵌套资源关联的列表或 crud
- android - 点击时 Android Spinner 不显示下拉列表
- android - 在按钮上同时使用 setOnTouchListener 和 setOnClickListener
- azure - 如何从配置文件中获取 blob 路径名容器名称
- php - htmlspecialchars($_POST['userinput'], ENT_NOQUOTES) 不忽略引号?
- python - 使用 ZKteco SDK 编写 Python 访问冲突