I need to make a client with GUI. Here is what the teacher told us to do.
Structure of your class:
And here is what I did. Please help?
Structure of your class:
//many import statements public class ChatClient { public class ChatClient() { //Initialize the GUI components and other data. } public void launchFrame() { //make the GUI //eastablish the socket connection //make a thread and start it with an instance of RemoteReader //create the streams from the socket connection //make gui visible } public class SendListener implements ActionListener { public void actionPerformed(ActionEvent e) { //get info from text field //send it to the server //clear the text field } } private class RemoteReader implements Runnable { public void run() { while (true) { //do stuff here to continously read from the server using the input stream //put the received text into the text area } } }
And here is what I did. Please help?
import java.net.*; import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class BasicChatCLient extends JFrame { private JTextArea ta; private JScrollPane sbrText; PrintWriter writer; String strClientName; private JTextField tf; private JLabel Label; private JButton btnQuit,btnSend; Socket clientSocket = null; ServerSocket serverSocket=null ; PrintWriter out = null; BufferedReader in = null; public BasicChatCLient(Socket clientSocket) { super("BasicChatCLient"); this.clientSocket = clientSocket; tf = new JTextField(50); ta = new JTextArea(10,50); sbrText= new JScrollPane(ta); Label = new JLabel("Chat Program", JLabel.CENTER); btnQuit = new JButton("Quit"); btnSend = new JButton("Send "); } public BasicChatCLient() { tf = new JTextField(50); ta = new JTextArea(10,50); sbrText= new JScrollPane(ta); Label = new JLabel("Chat Program", JLabel.CENTER); btnQuit = new JButton("Quit"); btnSend = new JButton("Send "); } public void launchFrame() { Thread t1 = new Thread(RemoteReader(tf)); t1.start(); btnSend.addActionListener(new SendButtonListener()); btnQuit.addActionListener(new ExitButtonListener()); this.setLayout(new BorderLayout(1,1)); sbrText.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); sbrText.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); Label.setFont(new Font("Serif", Font.BOLD, 18)); Label.setOpaque(true); Label.setBackground(Color.white); Label.setOpaque(true); ta.setEditable(false); tf.setBackground(Color.white); ta.setBackground(Color.white); JPanel p3= new JPanel(); p3.setLayout(new BorderLayout()); p3.add(Label,BorderLayout.NORTH); JPanel p1 = new JPanel(); p1.setLayout(new BorderLayout()); p1.add(sbrText, BorderLayout.CENTER); p1.add(tf, BorderLayout.SOUTH); JPanel p2 = new JPanel(new GridLayout(2,1)); p2.add(btnSend,BorderLayout.NORTH); p2.add(btnQuit,BorderLayout.SOUTH); this.add(p1,BorderLayout.WEST); this.add(p2, BorderLayout.EAST); this.add(p3, BorderLayout.NORTH); this.setTitle("Chat Client"); } public class ExitButtonListener implements ActionListener { public void actionPerformed(ActionEvent ev) { System.exit(0); } } public class SendButtonListener implements ActionListener { public void actionPerformed(ActionEvent ev) { try { writer.println(tf.getText()); writer.flush(); } catch(Exception ex) { ex.printStackTrace(); } tf.setText(""); tf.requestFocus(); } } private class RemoteReader implements Runnable { public void run() { try { InputStream is = clientSocket.getInputStream(); ObjectInputStream ois = new ObjectInputStream(is); while (true) { String strMsg = (String) ois.readObject(); ta.append("\n" + strClientName + ":" + strMsg); } } catch (Exception e) { e.printStackTrace(); } } } public static void main (String []args) throws IOException { Socket kkSocket = null; PrintWriter out = null; BufferedReader in = null; try { kkSocket = new Socket("127.0.0.1", 4444); out = new PrintWriter(kkSocket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream())); } catch (UnknownHostException e) { System.err.println("Could not connect to 127.0.0.1."); System.exit(1); } catch (IOException e) { System.err.println("Couldn't get I/O for the connection to 127.0.0.1."); System.exit(1); } BasicChatCLient gui = new BasicChatCLient(); gui.launchFrame(); gui.setVisible(true); gui.pack(); gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }