gpt4 book ai didi

java - 对齐 JTextArea 底部

转载 作者:行者123 更新时间:2023-11-30 08:55:13 27 4
gpt4 key购买 nike

我正在用 java 聊天,代码如下:

text += sendField.getText();
messageArea.setText(text);

我得到了这个:
enter image description here

但是我希望文本在 JTextArea 的底部对齐,这可能吗?

我怎样才能把它变成这个?
enter image description here

感谢抽空。

最佳答案

I want the text to be align in the bottom of the JTextArea is it possible?

文本组件不支持这个。您需要编写自定义 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;
}
}
}
}

关于java - 对齐 JTextArea 底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29148464/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com