Code:
package zad41;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.*;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class SimpleClient {
String action=null;
String lastname=null;
String number_with=null;
PhoneDirectory phonedirectory = new PhoneDirectory("numbers.txt");
public SimpleClient(){
search.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Get");
action="get";
lastname=(String)namesList1.getSelectedItem();
}});
add.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Add");
action="add";
lastname=add_name.getText();
}});
replace.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Replace");
action="replace";
lastname=(String)namesList2.getSelectedItem();
number_with=replace_with.getText();
}});
bye.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Bye");
action="Bye";
}});
}
JButton search = new JButton("Wyszukaj");
JButton add = new JButton("Dodaj");
JButton replace = new JButton("Zastap");
JButton bye = new JButton("Bye");
JTextField add_name = new JTextField(20);
JTextField replace_with = new JTextField(20);
JTextField output = new JTextField(20);
JComboBox namesList1 = new JComboBox(phonedirectory.getNames());
JComboBox namesList2 = new JComboBox(phonedirectory.getNames());
Socket kkSocket = null;
PrintWriter out = null;
BufferedReader in = null;
public JPanel createPanel(){
JPanel pane = new JPanel();
JLabel label1 = new JLabel("Wyszukaj");
JLabel label2 = new JLabel("Dodaj");
JLabel label3 = new JLabel("Zastap");
pane.setBorder(BorderFactory.createTitledBorder("Client"));
pane.setLayout(new BoxLayout(pane, BoxLayout.X_AXIS));
pane.add(label1);
pane.add(namesList1);
pane.add(search);
pane.add(label2);
pane.add(add_name);
pane.add(add);
pane.add(label3);
pane.add(replace_with);
pane.add(namesList2);
pane.add(replace);
pane.add(output);
pane.add(bye);
output.setEditable(false);
return pane;
}
public void connect(){
try {
kkSocket = new Socket("localhost", 4445);
out = new PrintWriter(kkSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: localhost.");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to: localhost.");
System.exit(1);
}
}
public void communicate(){
///BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String fromServer;
try {
while ((fromServer = in.readLine()) != null) {
System.out.println("Server: " + fromServer);
output.setText(fromServer);
if (fromServer.equals("Bye."))
break;
////fromUser = stdIn.readLine();
if (action != null) {
System.out.println("Client: " + action);
///out.println(action);
if(lastname!=null){
out.println(action+":"+lastname);
}
if(number_with!=null){
String info[]=number_with.split(" ");
out.println(action+":"+info[0]+":"+info[1]);
}
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void close() throws IOException{
out.close();
in.close();
///stdIn.close();
kkSocket.close();}
public static void main(String[] args) throws IOException {
SimpleClient simpleclient = new SimpleClient();
JFrame frame = new JFrame("Client");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(simpleclient.createPanel());
frame.pack();
frame.setVisible(true);
simpleclient.connect();
simpleclient.communicate();
simpleclient.close();
}
}
but it doesnt send the messages to server
i want when a button is clicked send an action to the server
Comment