Hi everyone, I'm trying to create a program that you can use to communicate to other people with the same program. It connects to a Server program that gets your message an send it to everyone. I found 2 problems that I haven't been able to solve.
The first one is that,in NetBeans, the GUI looks OK, but when you run the program outside it, the GUI becomes horrible. (Screenshots are on the attachments)
The other, more important problem, is that I seem to be only able to connect to localhost, even if I put my IP (I looked it on whatismyip.com , is that okay? ), it apparently connects, but it doesn't work. What should I do?
If you need any more info, just ask.
Thanks in advance.
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.event.*;
/**
*
* @author Yan Couto
*/
public class AMB {
JFrame f;
JPanel p;
JButton send;
JTextArea log,text;
JOptionPane opt;
Tell tell;
Thread listen;
getMessage g = new getMessage();
public static void main(String[] args) {
new AMB();
}
public AMB () {
GUI();
String name = JOptionPane.showInputDialog("Enter your name:");
try {
Socket s = getSocket(4567);
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
Scanner in = new Scanner(s.getInputStream());;
listen = new Thread(new Listen(in, name));
listen.start();
tell = new Tell(out, name);
}
catch (Exception e ) {
System.out.println("Oh fuck!\n");
}
}
private void GUI ()
{
f = new JFrame ();
f.setTitle("Amebensseger");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p = new JPanel (new GridBagLayout());
p.setSize(300,300);
log = new JTextArea(20,50);
log.setText("Welcome to AMB!\n");
log.append(" :)\n");
log.setEditable(false);
log.setLineWrap(true);
log.setWrapStyleWord(true);
JScrollPane logscroll = new JScrollPane(log,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
setItem(p,logscroll,0,0,4,3,new Insets(2,2,3,2),GridBagConstraints.NORTHWEST,GridBagConstraints.BOTH);
text = new JTextArea(8,40);
text.setLineWrap(true);
text.setWrapStyleWord(true);
text.addKeyListener(g);
setItem(p,text,0,3,3,1,new Insets(2,2,2,2),GridBagConstraints.SOUTH,GridBagConstraints.NONE);
send = new JButton("Send");
send.addActionListener(g);
setItem(p,send,3,3,1,1,new Insets(1,1,1,1),GridBagConstraints.CENTER,GridBagConstraints.BOTH);
f.add(p);
f.setSize(570,480);
//f.pack();
f.setVisible(true);
}
private void setItem (JPanel p, JComponent c, int x, int y,int width, int height, Insets insets, int align,int fill )
{
GridBagConstraints gc = new GridBagConstraints ();
gc.gridx = x;
gc.gridy = y;
gc.gridwidth = width;
gc.gridheight = height;
gc.insets = insets;
gc.anchor = align;
gc.fill = fill;
p.add(c,gc);
}
private void setItem(JPanel p, JComponent c, int x, int y,int width, int height) {
setItem(p,c,x,y,width,height,new Insets(0,0,0,0),GridBagConstraints.CENTER,GridBagConstraints.NONE);
}
private void setItem(JPanel p, JComponent c, int x, int y,int width, int height,int fill) {
setItem(p,c,x,y,width,height,new Insets(0,0,0,0),GridBagConstraints.CENTER,fill);
}
private Socket getSocket(int port)
{
Socket s;
String host;
InetAddress ip;
while(true)
{
host = JOptionPane.showInputDialog("What server do you want to connect to?");
try{
ip = InetAddress.getByName(host);
System.out.println(ip);
System.out.println(ip.getCanonicalHostName());
System.out.println(ip.isReachable(1000));
s = new Socket(ip, port);
return s;
}
catch (UnknownHostException e){
System.out.println("The host is unknown");
}
catch (IOException e){
System.out.println("Network error");
}
}
}
public class Listen implements Runnable {
Scanner in;
String name;
public Listen (Scanner in,String name)
{
this.in = in;
this.name = name;
}
public void run ()
{
while(true)
{
String n = in.nextLine();
String s = in.nextLine();
log.append(n + ":" + s + "\n");
}
}
}
class Tell {
PrintWriter out;
String name;
public Tell (PrintWriter out, String name)
{
this.out = out;
this.name = name;
}
public void sendText(String s) {
out.println(name);
out.println(s);
out.flush();
}
}
public class getMessage implements ActionListener, KeyListener {
public void actionPerformed(ActionEvent e) {
if(e.getSource() == send) {
String aux = text.getText();
text.setText(null);
tell.sendText(aux);
text.requestFocus();
}
}
public void keyTyped(KeyEvent e) {}
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == e.VK_ENTER) {
send.doClick();
try {
Robot r = new Robot();
r.keyPress(e.VK_BACK_SPACE);
} catch (Exception ex) {}
}
}
public void keyReleased(KeyEvent e) {}
}
}