首页 > > 详细

讲解LogClient Java、Java编程解析、讲解Java LogClient

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ConnectException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class LogClient extends JDialog {
LogClient log;
private final JPanel contentPanel = new JPanel();
private JTextField textField_port;
private JTextField textField_ip;
private JTextField textField_Log;
private JTextArea textArea;

/**
* Launch the application.
*/
public static void main(String[] args) {
try {
LogClient dialog = new LogClient();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the dialog.
*/
public LogClient() {
log=this;
setTitle("\u5BA2\u6237\u7AEF");
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setBounds(100, 100, 450, 457);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(null);
{
JLabel lblNewLabel = new JLabel("\u72B6\u6001\u4FE1\u606F\uFF1A");
lblNewLabel.setBounds(10, 10, 70, 15);
contentPanel.add(lblNewLabel);
}
{
JButton btnNewButton = new JButton("\u6E05\u7A7A\u72B6\u6001\u4FE1\u606F");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textArea.setText("");
}
});
btnNewButton.setBounds(258, 6, 114, 23);
contentPanel.add(btnNewButton);
}
{
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 35, 414, 204);
contentPanel.add(scrollPane);
}
{
JLabel lblNewLabel_1 = new JLabel("\u670D\u52A1\u5668IP\uFF1A");
lblNewLabel_1.setBounds(10, 252, 70, 15);
contentPanel.add(lblNewLabel_1);
}
{
JLabel lblNewLabel_2 = new JLabel("\u7AEF\u53E3\u53F7\uFF1A");
lblNewLabel_2.setBounds(229, 252, 54, 15);
contentPanel.add(lblNewLabel_2);
}
{
JLabel lblNewLabel_3 = new JLabel("\u65E5\u5FD7\u4FE1\u606F\uFF1A");
lblNewLabel_3.setBounds(10, 296, 70, 15);
contentPanel.add(lblNewLabel_3);
}

textField_ip = new JTextField();
textField_ip.setText("127.0.0.1");
textField_ip.setBounds(77, 249, 122, 21);
contentPanel.add(textField_ip);
textField_ip.setColumns(10);

textField_port = new JTextField();
textField_port.setText("9900");
textField_port.setBounds(278, 249, 66, 21);
contentPanel.add(textField_port);
textField_port.setColumns(10);

textField_Log = new JTextField();
textField_Log.setText("ceshi ");
textField_Log.setBounds(74, 293, 186, 21);
contentPanel.add(textField_Log);
textField_Log.setColumns(10);
{
textArea = new JTextArea();
textArea.setBounds(10, 35, 414, 204);
contentPanel.add(textArea);
}
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
{
JButton kButton = new JButton("\u53D1\u9001");
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e1){
System.out.println("....");
textArea.setText("....");
String serverIP=textField_ip.getText();
int serverPort=Integer.parseInt(textField_port.getText());
Socket logClient=null;
OutputStream s=null;
BufferedWriter writer =null;
try{
logClient=new Socket(serverIP,serverPort);
s=logClient.getOutputStream();
writer=new BufferedWriter(new OutputStreamWriter(os,"utf-8"));
writer.write(textField_Log.getText());
writer.flush();
textArea.setText("。"+"\r\n");
textArea.setText(":"+textField_Log.getText()+" "+textField_ip.getText());
logClient.close();
}catch(ConnectException e){
System.out.println(",("+serverIP+":"+serverPort+")。");
}catch(UnknownHostException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally{
try{
if(logClient!=null) logClient.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
});
okButton.setActionCommand("OK");
buttonPane.add(okButton);
getRootPane().setDefaultButton(okButton);
}
{
JButton cancelButton = new JButton("\u9000\u51FA");
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
log.dispose();
}
});
cancelButton.setActionCommand("Cancel");
buttonPane.add(cancelButton);
}
}
}
}
 

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!