Luc*_*lli 0 java swing rows jscrollpane jtextarea
我有一个JTextArea包装在一个JScrollPane中,我用它来记录我的应用程序的输出.当应用程序生成许多行时,文本区域中的行数增长非常快,并且滚动几乎不可见.我想要一个像Eclipse控制台一样的文本区域.我的意思是......带有垂直滚动的文本区域但是当使用滚动时我最多只能显示200行.
这是我正在使用的可运行代码:
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
import javax.swing.text.DefaultCaret;
public class WindowLog extends JFrame {;
private JPanel contentPane;
private static JTextArea textArea = new JTextArea();
public static void run() {
try {
WindowLog frame = new WindowLog();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void writeText(JTextArea ta, String s){
DefaultCaret caret = (DefaultCaret)ta.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
ta.append(s);
}
/**
* Create the panel.
*/
public WindowLog() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 523, 299);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
contentPane.setLayout(null);
JLabel lblLog = new JLabel("Log");
lblLog.setBounds(5, 5, 497, 14);
getContentPane().add(lblLog);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(5, 30, 492, 138);
//scrollPane.getViewport().setPreferredSize(new Dimension(300, 100));
textArea.setBorder(BorderFactory.createEmptyBorder(0, 6, 0, 6));
contentPane.add(scrollPane);
scrollPane.setViewportView(textArea);
Main.setTextProva(textArea);
}
}
Run Code Online (Sandbox Code Playgroud)
这是主类:
public class Main {
static JTextArea textProva;
public static void setTextProva(JTextArea textProva) {
Main.textProva = textProva;
}
public static void main(String[] args) {
WindowLog.run();
System.out.println(textProva);
WindowLog.writeText(textProva, "hello"+"\n");
}
Run Code Online (Sandbox Code Playgroud)
提前致谢.
| 归档时间: |
|
| 查看次数: |
576 次 |
| 最近记录: |