当前位置: 技术问答>java相关
Swing的一个问题!
来源: 互联网 发布时间:2015-03-19
本文导语: 我已经实现了JTextArea的JScrollPane, 可是我在实现JTextArea.append(String str)时,不能自动的滚动到最末的一行.怎样才能自动滚动到最末的一行! | 你运行一下下面这段程序: import java.awt.*; import ...
我已经实现了JTextArea的JScrollPane, 可是我在实现JTextArea.append(String str)时,不能自动的滚动到最末的一行.怎样才能自动滚动到最末的一行!
|
你运行一下下面这段程序:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import javax.swing.text.*;
import java.io.FileReader;
public class TextAreaTest extends JApplet {
boolean isStandalone = false;
public JTextArea textArea = new JTextArea();
private Container contentPane = getContentPane();
//Construct the applet
public TextAreaTest() {
}
//Initialize the applet
public void init() {
try {
// To(textArea, "text");
contentPane.add(new ControlPanel(), BorderLayout.NORTH);
contentPane.add(new JScrollPane(textArea), BorderLayout.CENTER);
}
catch(Exception e) {
e.printStackTrace();
}
}
private void readFile(JTextComponent textComponent, String s) {
try {
new DefaultEditorKit().read(new FileReader(s), textComponent.getDocument(), 0);
} catch (Exception ex) {
ex.printStackTrace();
}
}
//Get Applet information
public String getAppletInfo() {
return "Applet Information";
}
//Get parameter info
public String[][] getParameterInfo() {
return null;
}
//Main method
public static void main(String[] args) {
TextAreaTest applet = new TextAreaTest();
applet.isStandalone = true;
JFrame frame = new JFrame();
//EXIT_ON_CLOSE == 3
frame.setDefaultCloseOperation(3);
frame.setTitle("Applet Frame");
frame.getContentPane().add(applet, BorderLayout.CENTER);
applet.init();
applet.start();
frame.setSize(400,320);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
frame.setVisible(true);
}
class ControlPanel extends JPanel {
JRadioButton radioButtons[] = new JRadioButton[] {
new JRadioButton("wrap off"),
new JRadioButton("wrap characters"),
new JRadioButton("wrap words")
};
public ControlPanel() {
ButtonGroup group = new ButtonGroup();
Listener listener = new Listener();
for (int i = 0 ; i
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import javax.swing.text.*;
import java.io.FileReader;
public class TextAreaTest extends JApplet {
boolean isStandalone = false;
public JTextArea textArea = new JTextArea();
private Container contentPane = getContentPane();
//Construct the applet
public TextAreaTest() {
}
//Initialize the applet
public void init() {
try {
// To(textArea, "text");
contentPane.add(new ControlPanel(), BorderLayout.NORTH);
contentPane.add(new JScrollPane(textArea), BorderLayout.CENTER);
}
catch(Exception e) {
e.printStackTrace();
}
}
private void readFile(JTextComponent textComponent, String s) {
try {
new DefaultEditorKit().read(new FileReader(s), textComponent.getDocument(), 0);
} catch (Exception ex) {
ex.printStackTrace();
}
}
//Get Applet information
public String getAppletInfo() {
return "Applet Information";
}
//Get parameter info
public String[][] getParameterInfo() {
return null;
}
//Main method
public static void main(String[] args) {
TextAreaTest applet = new TextAreaTest();
applet.isStandalone = true;
JFrame frame = new JFrame();
//EXIT_ON_CLOSE == 3
frame.setDefaultCloseOperation(3);
frame.setTitle("Applet Frame");
frame.getContentPane().add(applet, BorderLayout.CENTER);
applet.init();
applet.start();
frame.setSize(400,320);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
frame.setVisible(true);
}
class ControlPanel extends JPanel {
JRadioButton radioButtons[] = new JRadioButton[] {
new JRadioButton("wrap off"),
new JRadioButton("wrap characters"),
new JRadioButton("wrap words")
};
public ControlPanel() {
ButtonGroup group = new ButtonGroup();
Listener listener = new Listener();
for (int i = 0 ; i