本文介绍了如何在JTextArea中更改文本的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
通常,在JTextArea中,文本从左上角开始。我希望它在左下角。你怎么能这样做?
Normally, in a JTextArea, the text starts in the upper left corner. I want it to be in the lower left corner. How can you do this?
(如果我的笔迹不可读,请道歉)
(apologies if my handwriting is unreadable)
推荐答案
你可以将 JTextArea
锚定到容器的 BorderLayout.PAGE_END
位置,并允许文本向上滚动。
You could anchor a JTextArea
to the BorderLayout.PAGE_END
location of a container and allow the text to scroll up.
public class BaseTextAreaDemo {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
final JFrame frame = new JFrame("Base JTextArea App");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
JPanel textAreaPanel = getBaseTextArea();
JScrollPane scrollPane = new JScrollPane(textAreaPanel) {
@Override
public Dimension getPreferredSize() {
return new Dimension(300, 230);
}
};
frame.add(scrollPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel getBaseTextArea() {
JTextArea textArea = new JTextArea();
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.append("bla bla bla\n");
textArea.append("new text here");
JPanel panel = new JPanel(new BorderLayout());
panel.setBackground(textArea.getBackground());
panel.setBorder(textArea.getBorder());
textArea.setBorder(null);
panel.add(textArea, BorderLayout.PAGE_END);
return panel;
}
});
}
}
这篇关于如何在JTextArea中更改文本的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!