高分求助!关于Swing方面的问题

oven 2003-01-07 12:16:01
本人正在做一个聊天室程序
我在JScrollPanel中放置了一个JTextArea来显示聊天内容,我想随着聊天内容的更新,
垂直滚动条自动向下移动,也就是使聊天内容自动向上滚动,使最后一行的文字一直可以看到。
是不是JScrollPanel,JTextArea中有什么专门的方法()或者编程怎么实现呢?
十分感谢!
...全文
65 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
bluesmile979 2003-01-07
  • 打赏
  • 举报
回复
在JTextArea里面有setScrollOffset,或者通过setSelectionStart,setSelectionEnd来实现。我没做过(vc里面这种方法是没有问题的),你可以试一下
funpig 2003-01-07
  • 打赏
  • 举报
回复
append()比较简单
exd2000 2003-01-07
  • 打赏
  • 举报
回复
append()
希偌 2003-01-07
  • 打赏
  • 举报
回复
上面的方法是可行的,这是个例子,你看看
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Frame1 extends JFrame {
private JPanel contentPane;
private JLabel jLabel1 = new JLabel();
private JButton jb=new JButton("click");
private JPanel jp=new JPanel(new FlowLayout());
private JScrollPane jscr=new JScrollPane();
private JTextArea jta=new JTextArea();
public Frame1() {
super("myFrame");
this.setSize(400,300);
this.setResizable(false);
this.setLocation(this.getToolkit().getScreenSize().width/2-this.getWidth()/2,this.getToolkit().getScreenSize().height/2-this.getHeight()/2);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add("South",jp);
contentPane.add("Center",jscr);
jscr.getViewport().add(jta);
jta.setText("start\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nend");
jp.add(jb);
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
click(e);
}
});
this.setSize(new Dimension(400, 300));
this.setTitle("Frame Title");
for(int i=1;i<=9;i++) {
for(int j=1;j<=i;j++) {
if(j!=1)
System.out.print(",");
System.out.print(j+"*"+i+"="+i*j);
}
System.out.println("");
}
this.setVisible(true);
}

public void click(ActionEvent e) {
this.jta.requestFocus();
this.jta.setSelectionStart(this.jta.getText().length());
this.jta.setSelectionEnd(this.jta.getText().length());
}

public static void main(String args[]) {
new Frame1();
}
}
colors 2003-01-07
  • 打赏
  • 举报
回复
up
oven 2003-01-07
  • 打赏
  • 举报
回复
好象上边的方法不行
是不是要将JTextArea1.SetEnable(True)有关
还有是不是和ContentPane的Layout的设置有关我设置的XYLayout
我的部分代码:
contentPane.add(jScrollPane1, null);
jScrollPane1.setAutoscrolls(true);
jScrollPane1.getViewport().add(jTextArea2,null);
jTextArea1.setLineWrap(true);
this.jTextArea1.requestFocus();
this.jTextArea1.setSelectionStart(this.jTextArea1.getText().length());
this.jTextArea1.setSelectionEnd(this.jTextArea1.getText().length());
nullhue 2003-01-07
  • 打赏
  • 举报
回复
请问,为什么不用List?
seawhl 2003-01-07
  • 打赏
  • 举报
回复
up
希偌 2003-01-07
  • 打赏
  • 举报
回复
this.jTextArea1.requestFocus();
this.jTextArea1.setSelectionStart(this.jTextArea1.getText().length());
this.jTextArea1.setSelectionEnd(this.jTextArea1.getText().length());
MagicJohn 2003-01-07
  • 打赏
  • 举报
回复
这里有一个关于scoll的例子
http://javaboutique.internet.com/Scrollup/

62,635

社区成员

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

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