我想在java中實現計算器客戶端服務器程序,我的客戶端有GUI,客戶端將數據發送到服務器並返回到該服務器。 但簡單的問題是無論輸入gui是不會去服務器java客戶端服務器套接字編程實現問題
我正在實施這個問題,但它不會工作... 在這裏,我試圖發送7到服務器。 有沒有人可以給我想法如何操縱代碼
無論我做了什麼。
CalculatorClient.java
package com.example.dca;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.UIManager;
public class CalculatorClient extends JFrame implements ActionListener {
public static Socket s;
public static DataOutputStream d;
public static BufferedReader in;
public static PrintWriter out;
static JPanel[] row = new JPanel[5];
static JButton[] button = new JButton[19];
static String[] buttonString = { "7", "8", "9", "+", "4", "5", "6", "-",
"1", "2", "3", "*", ".", "/", "C", "v", "+/-", "=", "0" };
static int[] dimW = { 300, 45, 100, 90 };
static int[] dimH = { 35, 40 };
static Dimension displayDimension = new Dimension(dimW[0], dimH[0]);
static Dimension regularDimension = new Dimension(dimW[1], dimH[1]);
static Dimension rColumnDimension = new Dimension(dimW[2], dimH[1]);
static Dimension zeroButDimension = new Dimension(dimW[3], dimH[1]);
boolean[] function = new boolean[4];
static double temp0;
static int status;
static double temp1;
static JTextArea display = new JTextArea(1, 20);
public static StringBuffer toSend = new StringBuffer();
static Font font = new Font("Times new Roman", Font.BOLD, 14);
private static void initGUI() throws UnknownHostException, IOException {
JFrame f = new JFrame("Calculator");
setDesign();
f.setSize(380, 250);
f.setResizable(false);
f.setDefaultCloseOperation(EXIT_ON_CLOSE);
GridLayout grid = new GridLayout(5, 5);
f.setLayout(grid);
FlowLayout f1 = new FlowLayout(FlowLayout.CENTER);
FlowLayout f2 = new FlowLayout(FlowLayout.CENTER, 1, 1);
for (int i = 0; i < 5; i++)
row[i] = new JPanel();
row[0].setLayout(f1);
for (int i = 1; i < 5; i++)
row[i].setLayout(f2);
for (int i = 0; i < 19; i++) {
button[i] = new JButton();
button[i].setText(buttonString[i]);
button[i].setFont(font);
}
button[0].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
display.append("7");
String s = display.getText();
System.out.println("inner" + s);
if (out != null) {
out.println(s);
//out.println("jaym");
out.flush();
}
display.setText(" ");
}
});
button[1].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
display.append("8");
}
});
button[2].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
display.append("9");
}
});
button[3].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.out.println("additionn");
temp0 = Double.parseDouble(display.getText());
// function[0] = true;
display.setText("");
System.out.println("temp0" + temp0);
try {
sendTemp0(temp0);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
button[4].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
display.append("4");
}
});
button[5].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
display.append("5");
}
});
button[6].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
display.append("6");
}
});
button[7].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
// temporary[0] = Double.parseDouble(display.getText());
// function[1] = true;
display.setText("");
}
});
button[8].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
display.append("1");
}
});
button[9].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
display.append("2");
}
});
button[10].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
display.append("3");
}
});
button[11].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub multiply
// temporary[0] = Double.parseDouble(display.getText());
// function[2] = true;
display.setText("");
}
});
button[12].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
display.append(".");
}
});
button[13].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub division
}
});
button[18].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
display.append("0");
}
});
display.setFont(font);
display.setEditable(false);
display.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
display.setPreferredSize(displayDimension);
for (int i = 0; i < 14; i++)
button[i].setPreferredSize(regularDimension);
for (int i = 14; i < 18; i++)
button[i].setPreferredSize(rColumnDimension);
button[18].setPreferredSize(zeroButDimension);
row[0].add(display);
f.add(row[0]);
for (int i = 0; i < 4; i++)
row[1].add(button[i]);
row[1].add(button[14]);
f.add(row[1]);
for (int i = 4; i < 8; i++)
row[2].add(button[i]);
row[2].add(button[15]);
f.add(row[2]);
for (int i = 8; i < 12; i++)
row[3].add(button[i]);
row[3].add(button[16]);
f.add(row[3]);
row[4].add(button[18]);
for (int i = 12; i < 14; i++)
row[4].add(button[i]);
row[4].add(button[17]);
f.add(row[4]);
f.setVisible(true);
}
CalculatorClient() throws UnknownHostException, IOException {
}
private static void sendTemp0(double dd) throws IOException {
toSend.append(dd);
}
public static void setDesign() {
// TODO Auto-generated method stub
try {
UIManager
.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception e) { }
}
@Override
public void actionPerformed(ActionEvent ae) {
// TODO Auto-generated method stub
}
public static void main(String[] arguments) throws UnknownHostException, IOException {
initGUI();
s = new Socket("localhost", 1234);
out = new PrintWriter(s.getOutputStream());
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
s.close();
out.close();
in.close();
}
}
CalculatorServer.java
package com.example.dca;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class CalculatorServer {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
ServerSocket ss=new ServerSocket(1234);
Socket s=ss.accept();
System.out.println("conection done bapuu!!!");
//To read file name from the client
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
// to send content of the of file
System.out.println("reader.....");
DataOutputStream out = new DataOutputStream(s.getOutputStream());
// to read file name
System.out.println("writer....");
String sss=in.readLine();
if(sss!=null)
System.out.println(sss);
ss.close();
s.close();
in.close();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
謝謝你...現在它工作 –