带滚动条和默认文本的JTextArea,怎么显示前面部分的文本?默认是显示最后部分。

ljooo 2007-06-08 01:09:41
JTextArea设置最大显示3行,而默认文本有5行。但是显示时显示最后的3行,我要一开始就显示前面的3行,怎么做?
...全文
526 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
ljooo 2007-06-11
  • 打赏
  • 举报
回复
很奇怪,现在默认显示是最前面几行了.
谢谢楼上的两位.
Inhibitory 2007-06-08
  • 打赏
  • 举报
回复
利用javax.swing.text.JTextComponent的public void setCaretPosition(int position);以前用过,可以实现你的功能。

public void setCaretPosition(int position)
Sets the position of the text insertion caret for the TextComponent. Note that the caret tracks change, so this may move if the underlying text of the component is changed. If the document is null, does nothing. The position must be between 0 and the length of the component's text or else an exception is thrown.

Parameters:
position - the position
Throws:
IllegalArgumentException - if the value supplied for position is less than zero or greater than the component's text length
吴恒 2007-06-08
  • 打赏
  • 举报
回复
我试了下没有你说的这种情,一开始就在上面了。你运一下我的这个代码
/**
@version 1.32 2004-05-05
@author Cay Horstmann
*/

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TextEditTest
{
public static void main(String[] args)
{
TextEditFrame frame = new TextEditFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

/**
A frame with a text area and components for search/replace.
*/
class TextEditFrame extends JFrame
{
public TextEditFrame()
{
setTitle("TextEditTest");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

JPanel panel = new JPanel();

// add button, text fields and labels

JButton replaceButton = new JButton("Replace");
panel.add(replaceButton);
replaceButton.addActionListener(new ReplaceAction());

from = new JTextField("brown", 8);
panel.add(from);

panel.add(new JLabel("with"));

to = new JTextField("purple", 8);
panel.add(to);
add(panel, BorderLayout.SOUTH);

// add text area with scroll bars
String text ="775555555555555555555555555555555555555555555555555555555555555555555555555555llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll55555555555555555555555555555555555551111";
textArea = new JTextArea(text,3,20);
// textArea.setText
// ("The quick brown fox jumps over the lazy dog.");
//JScrollPane scrollPane = new JScrollPane(textArea);
TextArea.setLineWrap(true);
//add(scrollPane, BorderLayout.CENTER);
add(textArea, BorderLayout.CENTER);
}

public static final int DEFAULT_WIDTH = 200;
public static final int DEFAULT_HEIGHT = 120;

private JTextArea textArea;
private JTextField from;
private JTextField to;

/**
The action listener for the replace button.
*/
private class ReplaceAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String f = from.getText();
int n = textArea.getText().indexOf(f);
if (n >= 0 && f.length() > 0)
textArea.replaceRange(to.getText(), n,
n + f.length());
}
}
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧