我正在用java代码进行聊天:
text += sendField.getText();
messageArea.setText(text);
我得到了这个:
但我希望文本在JTextArea的底部对齐是否可能?
如何将其变成这个?
感谢您的时间。
答案 0 :(得分:2)
我希望文本在JTextArea的底部对齐是否可能?
文本组件不支持此功能。您需要编写一个自定义UI,以便从组件底部而不是组件顶部(高于我的技能级别)进行文本绘制。
但是,您可以利用Swing布局管理器使文本从底部显示:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class TextAreaBottom extends JPanel implements DocumentListener
{
private JTextArea textArea;
public TextAreaBottom(JTextArea textArea)
{
this.textArea = textArea;
setLayout( new BorderLayout() );
setBackground( textArea.getBackground() );
setBorder( textArea.getBorder() );
textArea.getDocument().addDocumentListener(this);
add(textArea, BorderLayout.SOUTH);
}
@Override
public void insertUpdate(DocumentEvent e)
{
adjustHeight();
}
@Override
public void removeUpdate(DocumentEvent e)
{
adjustHeight();
}
@Override
public void changedUpdate(DocumentEvent e) {}
private void adjustHeight()
{
int rows = textArea.getLineCount();
textArea.setRows(rows);
}
private static void createAndShowUI()
{
final JTextArea textArea = new JTextArea(5, 20);
textArea.setEditable( false );
final JTextField textField = new JTextField(20);
JButton send = new JButton( "Send" );
send.addActionListener( new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
if(textArea.getDocument().getLength() > 0)
textArea.append("\n");
textArea.append( textField.getText() );
textField.setText("");
textField.requestFocusInWindow();
}
});
JPanel panel = new JPanel( new BorderLayout() );
panel.add(textField);
panel.add(send, BorderLayout.EAST);
JFrame frame = new JFrame("TextAreaBottom");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JScrollPane( new TextAreaBottom(textArea) ) );
frame.add(panel, BorderLayout.SOUTH );
frame.setLocationByPlatform( true );
frame.pack();
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}
或者为了更好的方法,您可以使用JTextPane。我修改了Center Text Vertically in JTextPane代码以在底部绘制文本。这是一个简单的一线改变,因为困难部分已经完成:
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
public class TextPaneCenter
{
private static void createAndShowUI()
{
JTextPane edit = new JTextPane();
JFrame frame=new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new JScrollPane(edit));
frame.setSize(300,300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
edit.setSelectionColor( Color.GREEN );
try
{
edit.setEditorKit(new MyEditorKit());
SimpleAttributeSet attrs=new SimpleAttributeSet();
StyleConstants.setAlignment(attrs,StyleConstants.ALIGN_CENTER);
StyledDocument doc=(StyledDocument)edit.getDocument();
doc.insertString(0,"111\n2222222\n33333333333333",attrs);
doc.setParagraphAttributes(0,doc.getLength()-1,attrs,false);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
static class MyEditorKit extends StyledEditorKit
{
public ViewFactory getViewFactory()
{
return new StyledViewFactory();
}
static class StyledViewFactory implements ViewFactory
{
public View create(Element elem)
{
String kind = elem.getName();
if (kind != null)
{
if (kind.equals(AbstractDocument.ContentElementName))
{
return new LabelView(elem);
}
else if (kind.equals(AbstractDocument.ParagraphElementName))
{
return new ParagraphView(elem);
}
else if (kind.equals(AbstractDocument.SectionElementName))
{
return new CenteredBoxView(elem, View.Y_AXIS);
}
else if (kind.equals(StyleConstants.ComponentElementName))
{
return new ComponentView(elem);
}
else if (kind.equals(StyleConstants.IconElementName))
{
return new IconView(elem);
}
}
// default to text display
return new LabelView(elem);
}
} // class StyledViewFactory
} // class MyEditorKit
static class CenteredBoxView extends BoxView
{
public CenteredBoxView(Element elem, int axis)
{
super(elem,axis);
}
protected void layoutMajorAxis(int targetSpan, int axis, int[] offsets, int[] spans)
{
super.layoutMajorAxis(targetSpan,axis,offsets,spans);
int textBlockHeight = 0;
int offset = 0;
for (int i = 0; i < spans.length; i++)
{
textBlockHeight += spans[ i ];
}
// display text vertically at the bottom
offset = (targetSpan - textBlockHeight);
// display text vertically centered
//offset = (targetSpan - textBlockHeight) / 2;
for (int i = 0; i < offsets.length; i++)
{
offsets[ i ] += offset;
}
}
}
}