给个applet 的public class TextAreaDemo extends Applet { String msg; public void init(){ setBackground(Color.cyan ); setForeground(Color.blue); String str = "I am ChengHuaFang,\n" + "a student of centre south university.\n" + "Now I am tweenty-one.\n"; TextArea text = new TextArea(str,10,33); add(text); addMouseListener(new MyMouseAdapter(this)); }
class MyMouseAdapter extends MouseAdapter{ TextAreaDemo adapterDemo; public MyMouseAdapter(TextAreaDemo adapterDemo){ this.adapterDemo = adapterDemo; }
public void mouseClicked(MouseEvent me){ adapterDemo.showStatus("mouse clicked") ; } }
public void paint(Graphics g ){ msg = "now is other"; g.drawString(msg,24,190); } }
首先你需要有一个窗体JFrame jf
然后你需要一个文本控件JTextArea jta
接着在这个窗体上面添加这个控件
jf.getContentPane().add(jta);
这样就OK了文本区中显示文本需要指定文本所在的txt文件,需要用FeleStream将文本内容读出来,然后追加到JTextArea控件去
jta.append(String s);
private JTextArea textArea = new JTextArea();
private JScrollPane scrollPane = new JScrollPane();
private JScrollBar hScrollBar;
InfoFrame(String name) {
hScrollBar=scrollPane.getHorizontalScrollBar();
this.setTitle(name);
this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
scrollPane.getViewport().add(textArea);
this.add(scrollPane, java.awt.BorderLayout.CENTER);
this.setSize(500, 450);
this.setVisible(true);
}
}
String msg;
public void init(){
setBackground(Color.cyan );
setForeground(Color.blue);
String str = "I am ChengHuaFang,\n" +
"a student of centre south university.\n" +
"Now I am tweenty-one.\n";
TextArea text = new TextArea(str,10,33);
add(text);
addMouseListener(new MyMouseAdapter(this));
}
class MyMouseAdapter extends MouseAdapter{
TextAreaDemo adapterDemo;
public MyMouseAdapter(TextAreaDemo adapterDemo){
this.adapterDemo = adapterDemo;
}
public void mouseClicked(MouseEvent me){
adapterDemo.showStatus("mouse clicked") ;
}
}
public void paint(Graphics g ){
msg = "now is other";
g.drawString(msg,24,190);
}
}