package com.haoran.gui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class ChatRoom implements ActionListener {
JFrame jf;
JPanel jpanel;
JButton jb1, jb2, jb3;
JTextArea jta = null;
JScrollPane jscrollPane;
public ChatRoom() {
init();
}
public static void main(String[] args) {
ChatRoom room = new ChatRoom();
final JTextArea text = room.jta;
OutputStream textAreaStream = new OutputStream() {
public void write(int b) throws IOException {
text.append(String.valueOf((char) b));
text.setCaretPosition(text.getText().length());
}
public void write(byte b[]) throws IOException {
text.append(new String(b));
text.setCaretPosition(text.getText().length());
}
public void write(byte b[], int off, int len) throws IOException {
text.append(new String(b, off, len));
text.setCaretPosition(text.getText().length());
}
};
PrintStream myOut = new PrintStream(textAreaStream);
System.setOut(myOut);
System.setErr(myOut);
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
TimeUnit.SECONDS.sleep(2);
System.out.println(new Date().toString());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
System.out.println("测试文");
}
private void init() {
jf = new JFrame("聊天室");
Container contentPane = jf.getContentPane();
contentPane.setLayout(new BorderLayout());
jta = new JTextArea(10, 15);
jta.setEditable(false);
jta.setTabSize(4);
jta.setFont(new Font("标楷体", Font.BOLD, 16));
jta.setLineWrap(true);// 激活自动换行功能
jta.setWrapStyleWord(true);// 激活断行不断字功能
jta.setBackground(Color.orange);
jta.setCaretPosition(jta.getText().length());
jscrollPane = new JScrollPane(jta);
jpanel = new JPanel();
jpanel.setLayout(new GridLayout(1, 3));
jb1 = new JButton("复制");
jb1.addActionListener(this);
jb2 = new JButton("粘贴");
jb2.addActionListener(this);
jb3 = new JButton("剪切");
jb3.addActionListener(this);
jpanel.add(jb1);
jpanel.add(jb2);
jpanel.add(jb3);
contentPane.add(jscrollPane, BorderLayout.CENTER);
contentPane.add(jpanel, BorderLayout.SOUTH);
jf.setSize(400, 300);
jf.setLocation(400, 200);
centerWindow(jf);
jf.setVisible(true);
jf.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public static void centerWindow(Container window) {
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
int w = window.getSize().width;
int h = window.getSize().height;
int x = (dim.width - w) / 2;
int y = (dim.height - h) / 2;
window.setLocation(x, y);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jb1) {
jta.copy();
} else if (e.getSource() == jb2) {
jta.paste();
} else if (e.getSource() == jb3) {
jta.cut();
}
}
}