Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- http://seattlecentral.edu/faculty/moneil/A210/L5/Lesson5-7ed.htm
- package com.company;
- import java.net.*;
- import java.io.*;
- import java.awt.EventQueue;
- import java.awt.Font;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- import javax.swing.SwingConstants;
- import javax.swing.border.EmptyBorder;
- class Host {
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- @Override
- public void run() {
- try {
- Sender frame = new Sender();
- frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- ServerSocket s = null;
- try {
- s = new ServerSocket(5555);
- Socket s1 = s.accept();
- OutputStream s1out = s1.getOutputStream();
- BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s1out));
- bw.write("");
- bw.close();
- s1.close();
- }catch (Exception e){
- e.printStackTrace();
- }
- }
- }
- class Sender extends JFrame implements ActionListener {
- private static final long serialVersionUID = 1L;
- private JPanel contentPane;
- private JTextField textField = new JTextField();
- private JButton btnSend = new JButton("Send");
- private String textFieldInput;
- private JButton[] myButtons =
- {btnSend};
- public Sender() {
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(100, 100, 450, 300);
- setSize(500, 400);
- setLocationRelativeTo(null);
- setResizable(false);
- contentPane = new JPanel();
- contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
- setContentPane(contentPane);
- contentPane.setLayout(null);
- positionTextField();
- positionButtons();
- for (JButton jButton : myButtons) {
- jButton.addActionListener(this);
- contentPane.add(jButton);
- }
- }
- public void positionTextField() {
- textField.setFont(new Font("Dialog", Font.PLAIN, 20));
- textField.setHorizontalAlignment(SwingConstants.CENTER);
- textField.setBounds(46, 12, 380, 42);
- contentPane.add(textField);
- textField.setColumns(10);
- }
- public void positionButtons() {
- btnSend.setBounds(180, 292, 150, 48);
- }
- public void setTextTo(String text) {
- textField.setText(textField.getText() + text);
- textField.requestFocus();
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- if (e.getSource() == btnSend) {
- textField.setText(bw.write);
- textField.requestFocus();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement