1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
// import java.io.Console;
import java.io.PrintStream;
import java.awt.BorderLayout;
import java.awt.GridBagLayout;
import java.awt.HeadlessException;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.SwingUtilities;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JButton;
/**
* Created by IntelliJ IDEA.
* User: bebe
* Date: 06-Jun-2006
* Time: 17:50:57
* To change this template use File | Settings | File Templates.
*/
public class ConsoleToJTextArea extends JFrame implements ActionListener {
private JTextArea myTextArea = null;
private JTextField myTextField = null;
public ConsoleToJTextArea() throws HeadlessException {
myTextArea = new JTextArea();
MyOutputStream myOutputStream = new MyOutputStream(myTextArea);
PrintStream out = new PrintStream(myOutputStream);
System.setOut(out);
add(new JScrollPane(myTextArea), BorderLayout.CENTER);
JPanel pnlSouth = new JPanel(new GridBagLayout());
myTextField = new JTextField();
JButton myButton = new JButton("Afficher");
myButton.addActionListener(this);
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5,5,5,5);
gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.HORIZONTAL;
pnlSouth.add(myTextField, gbc);
gbc.weightx = 0.0;
gbc.fill = GridBagConstraints.NONE;
pnlSouth.add(myButton);
add(pnlSouth, BorderLayout.SOUTH);
setPreferredSize(new Dimension(400,300));
pack();
}
public void actionPerformed(ActionEvent e) {
//To change body of implemented methods use File | Settings | File Templates.
System.err.println(myTextField.getText());
System.out.println(myTextField.getText());
myTextField.setText("");
}
public static void main(String[] args) {
Runnable runnable = new Runnable() {
public void run() {
//To change body of implemented methods use File | Settings | File Templates.
// Console c = System.console();
ConsoleToJTextArea c = new ConsoleToJTextArea();
c.setLocationRelativeTo(null);
c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c.setVisible(true);
}
};
SwingUtilities.invokeLater(runnable);
}
} |