この小さな例を試してみてください。
public class Example {
private static int MAX = 7;
static public void main(String[] s) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
JFrame frame = new JFrame();
frame.setBounds(50, 50, 200, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JTextPane pane = new JTextPane();
pane.setText("1\n2\n3\n4");
JPanel pnl = new JPanel(new BorderLayout());
pnl.add(pane, BorderLayout.CENTER);
pane.getDocument().addDocumentListener(new DocumentListener() {
public void removeUpdate(DocumentEvent e) {
}
public void insertUpdate(DocumentEvent e) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
View baseView = pane.getUI().getRootView(pane);
View root = baseView.getView(0);
for(int i = 0; i < root.getViewCount()-MAX; i++) {
int line = root.getViewIndex(i, Bias.Forward);
View lineview = root.getView(line);
pane.getDocument().remove(lineview.getStartOffset(), lineview.getEndOffset());
}
} catch(BadLocationException e1) {
e1.printStackTrace();
}
}
});
}
public void changedUpdate(DocumentEvent e) {
}
});
pnl.add(new JButton(new AbstractAction("Delete") {
public void actionPerformed(ActionEvent e) {
try {
View baseView = pane.getUI().getRootView(pane);
View root = baseView.getView(0);
int line = root.getViewIndex(0, Bias.Forward);
View lineview = root.getView(line);
pane.getDocument().remove(lineview.getStartOffset(), lineview.getEndOffset());
} catch(BadLocationException e1) {
e1.printStackTrace();
}
}
}), BorderLayout.SOUTH);
pnl.add(new JButton(new AbstractAction("Add") {
@Override
public void actionPerformed(ActionEvent e) {
try {
pane.getDocument().insertString(pane.getDocument().getEndPosition().getOffset(), new SimpleDateFormat("ss").format(new Date())+": This is a new line\n", null);
} catch(BadLocationException e1) {
e1.printStackTrace();
}
}
}), BorderLayout.NORTH);
frame.setContentPane(pnl);
frame.setVisible(true);
}
}
このhttp://stackoverflow.com/q/102171/307767を見ます – oliholz
私はすでにそのポストを見て、それは私の質問を解決しません。ログ全体が複数の行を超えたら、行を削除する必要があります。 – Dave
私の答え[ここ](http://stackoverflow.com/questions/6316272/)を参照してください。私はMessage Consoleがあなたの要求に合っていると思います。 –