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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
| /*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.java.Interface;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
/**
*
* @author Hamza Elyoubi
* @Date : 15/11/2017
* @Version 1.0
*/
public class Communica extends JFrame implements WindowListener{
private BufferedWriter writer;
private BufferedReader reader;
private JButton bReceive;
private JButton bSend;
private JLabel lmessrec;
private JLabel lmesssend;
private JTextArea textRec;
private JTextArea textTosend;
public Communica(BufferedReader r,BufferedWriter w){
this.reader = r;
this.writer = w;
initComponents();
}
private void initComponents(){
this.setAlwaysOnTop(true);
setTitle("Communication window");
lmesssend = new JLabel("Message to send : ");
lmessrec = new JLabel("Received message : ");
textTosend = new JTextArea(7, 20);
JScrollPane scrollPaneTosend = new JScrollPane(textTosend);
textRec = new JTextArea(7, 20);
textRec.setEditable(false);
JScrollPane scrollPaneToreceive = new JScrollPane(textRec);
bSend = new JButton("Send");
bReceive = new JButton("Receive");
setSize(500, 300);
//Trois lignes sur deux colonnes avec GridLayout
setLayout(new GridLayout(3, 2));
//On ajoute chaque composant au contentPane
getContentPane().add(lmesssend);
getContentPane().add(scrollPaneTosend);
getContentPane().add(bSend);
getContentPane().add(bReceive);
getContentPane().add(lmessrec);
getContentPane().add(scrollPaneToreceive);
bSend.addActionListener(new ActionListenerSend());
bReceive.addActionListener(new ActionListenerReceive());
// the JFrame is visible now
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public class ActionListenerSend implements ActionListener{
public void actionPerformed(ActionEvent event){
try {
writer.write(textTosend.getText());
System.out.println("ecriture faite");
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public class ActionListenerReceive implements ActionListener{
public void actionPerformed(ActionEvent event){
try {
String text = "";
String line = reader.readLine();
while (line!=null) {
text += line;
line = reader.readLine();
}
textRec.setText(text);
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Override
public void windowClosed(WindowEvent e) {
try {
if (writer != null){
writer.close();
}
if (reader != null){
reader.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
@Override
public void windowOpened(WindowEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void windowClosing(WindowEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void windowIconified(WindowEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void windowDeiconified(WindowEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void windowActivated(WindowEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void windowDeactivated(WindowEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
} |
Partager