java help  | |
January 12th, 2006, 11:59 PM
|
#1 (permalink)
| | Member
Join Date: May 2005 Location: NYC
Posts: 370
|
I wrote a simple instant messaging program with no gui. I designed the gui seperately. Now i want to run the program with the gui, how do i do that?
Heres the code:
Socket Server-
import java.util.*;
import java.io.*;
import java.net.*;
class MessageServer {
private static ServerSocket servSock;
private static final int PORT = 1234; // connect on port 1234
public static void main(String[] args) {
System.out.println("Opening port... \n");
try {
servSock = new ServerSocket(PORT);
}
catch(IOException e) {
System.out.println("Unable to set up port!");
System.exit(1); // exit if there is a connection problem
}
do {
run();
} while (true); // infinite loop
}
private static void run() {
Socket link = null;
try {
link = servSock.accept(); // wait on server until message is recieved
Scanner input = new Scanner(link.getInputStream());
PrintWriter out = new PrintWriter(
link.getOutputStream(),true); // output
int numMessages = 0;
String message = input.nextLine();
while (!message.equals("***CLOSE***")) {
System.out.println("Message Recieved.");
numMessages++;
out.println("Message " + numMessages
+ ": " + message);
message = input.nextLine();
}
out.println(numMessages + " messages recieved.");
input.close();
link.close();
}
catch (IOException e) {
e.printStackTrace();
}
finally {
try {
System.out.println("\n* Closing connection...*");
link.close();
}
catch (IOException e) {
System.out.println("Unable to disconnect!");
System.exit(1);
}
}
}
}
client -
import java.util.*;
import java.io.*;
import java.net.*;
class MessageClient {
private static InetAddress host;
private static final int PORT = 1234; // connect to server on port 1234
public static void main(String args[]) {
try {
host = InetAddress.getLocalHost();
}
catch (UnknownHostException e) {
System.out.println("Host not found!");
System.exit(1);
}
run();
}
private static void run() {
Socket link = null;
try {
link = new Socket(host,PORT);
Scanner networkInput =
new Scanner(link.getInputStream());
PrintWriter out = new PrintWriter(
link.getOutputStream(),true);
//Set up scanner for keyboard entry
Scanner userEntry = new Scanner(System.in);
String message="", response;
do {
System.out.print("Enter message: ");
message = userEntry.nextLine();
out.println(message);
response = networkInput.nextLine();
System.out.println("\nType> " + response);
} while (!message.equals("***CLOSE***"));
}
catch (IOException e) {
e.printStackTrace();
}
finally {
try {
System.out.println("\n* Closing connection...*");
link.close();
}
catch (IOException e) {
System.out.println("Unable to disconnect!");
System.exit(1);
}
}
}
}
gui -
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
import java.net.*;
public class Simple extends JFrame implements ActionListener {
private static Simple frame;
private static JButton button;
private static JButton button1;
private static JButton button2;
private static JButton button3;
private static JLabel label;
private static JTextField line;
private static JTextField text;
public static void main(String[] args) {
Simple frame = new Simple();
frame.setTitle("Chatroom");
frame.setSize(500,500);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setVisible(true);
}
public Simple() {
setLayout(new FlowLayout());
JTextArea text = new JTextArea(24,40);
text.setWrapStyleWord(true);
text.setLineWrap(true);
add(new JScrollPane(text));
button1 = new JButton("Submit");
button = new JButton("Quit");
JLabel label = new JLabel();
JTextField line = new JTextField(40);
add(line);
add(label);
add(button1);
add(button);
button1.addActionListener(this);
button.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button1)
if (e.getSource() == button)
System.exit(0);
}
}
sorry for the long post
__________________
MSI K8N Neo4 Platinum SLI Socket 939 nForce4
Athlon 64 3200+ Venice Socket 939
CORSAIR XMS 2GB 184-Pin DDR 400 (PC 3200)
ATI x800xl PCI Express
|
| |
January 13th, 2006, 12:17 AM
|
#2 (permalink)
| | Banned
Join Date: Oct 2005
Posts: 752
|
did u write them from scratch or you get that from the net? |
| |
January 13th, 2006, 12:28 AM
|
#3 (permalink)
| | Member
Join Date: May 2005 Location: NYC
Posts: 370
|
the server and client r part me part a tutorial and the gui is from scratch
none of it is overwhelmingly complicated, i just don't know how to combine the client and gui lol |
| |
January 13th, 2006, 03:13 PM
|
#4 (permalink)
| | Banned
Join Date: Oct 2005
Posts: 752
|
maybe ask whoever wrote the tutorial...or published it. |
| |
January 13th, 2006, 03:22 PM
|
#5 (permalink)
| | Perfetc Member
Join Date: Jan 2003 Location: Maryland Suburbia
Posts: 4,334
|
You need to setup your action performed method to handle the input from a JTextBox. Then you should be able to feed that back into your non-gui class.
You'll want the main class of your non-gui class to create and instance of the GUI. |
| |
January 13th, 2006, 03:25 PM
|
#6 (permalink)
| | Member
Join Date: May 2005 Location: NYC
Posts: 370
|
how would i tell my "submit" button to send the typing to the server.
I dont know of any acrtion listner methods to do it |
| |
January 13th, 2006, 04:13 PM
|
#7 (permalink)
| | Perfetc Member
Join Date: Jan 2003 Location: Maryland Suburbia
Posts: 4,334
|
Create a method in the client like "public void sendMessage(String msg)", which handles sending the message to the server of course.
Then have your action listener take the text from JTextBox when the user hits submit and send it to the sendMessage method.
Here's a link to a directory of code from my computer science 2 projects, which was an instant messaging client. http://www.andrewpangborn.com/files/.../CS2/project2/
The server however was already written for us, but you may want to look at "ChatWindowGUI.java" for reference on actionPerformed stuff. Code: //Creates the input box and puts it in a scroll pane
textBox = new JTextArea(3,0);
//Adds a keybinding to the enter key to send messages.
textBox.getInputMap().put(KeyStroke.getKeyStroke("ENTER"),"enter");
textBox.getActionMap().put("enter",new AbstractAction() {
public void actionPerformed(ActionEvent e) {
if(textBox.getText().length() > 0) {
sendMsg(textBox.getText());
}
}
}); Its setting up a keybinding to the enter key and defining an action for it. It's probably a bit confusing if you are new to java
alternatively, just do something like this Code: // misc code
JButton submit = new JButtion("submit);
JTextArea text = new JTextArea(3,0);
submit.addActionListener(this);
// misc code
public void actionPerformed(ActionEvent e) {
if(e.getSource() == submit) {
theClient.sendMessage(text.getText()); //assumes you have a reference to the client object called "theClient"
}
}
Last edited by VHockey86 : January 13th, 2006 at 04:24 PM.
|
| | | Thread Tools | Search this Thread | | | |
Similar Threads | | Thread | Thread Starter | Forum | Replies | Last Post | | java hw help | sr71000 | Webmastering and Programming | 2 | November 2nd, 2005 09:27 PM | | Java | Rand Dusing | Webmastering and Programming | 3 | August 17th, 2004 08:42 AM | | JAVA | 1firecapt | General Tech Discussion | 0 | April 2nd, 2004 09:48 PM | | C++ or Java? | shawshank62 | General Tech Discussion | 17 | February 25th, 2004 01:04 PM | | Java for XP??!??!?!?! | skybolt_1 | Applications and Operating Systems | 4 | July 15th, 2002 11:45 AM | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |