用Socket打开JFrame的问题 [英] problem in opening JFrame with Socket
本文介绍了用Socket打开JFrame的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下是我的代码,当我让客户端发送一些数据时,它将显示给我,但是当我单击addd按钮时,它不会打开新的JFrame ...
任何人都可以解决我的问题吗.
Here following is my code and as i make a client send some data it will show me But as i click on addd button it will not open up a new JFrame...
Can any one solve my problem..
import java.net.*;
import java.io.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.sql.*;
class Cli extends JFrame
{
ServerSocket ss;
Socket soc;
InputStream in;
OutputStream out;
DataInputStream sin;
DataOutputStream sout;
JLabel l1;
//JTextField txt1;
JButton addd;
Cli() throws Exception
{
Class.forName("com.mysql.jdbc.Driver");
Connection cn=DriverManager.getConnection("jdbc:mysql://localhost:3306/nirav","root","root");
JPanel pan=new JPanel(new GridLayout(2,1));
ss=new ServerSocket(8888);
//ss.setSOLimit(10000);
l1=new JLabel("waiting for client");
txt1=new JTextField(40);
addd=new JButton("click here to send");
soc=ss.accept();
in=soc.getInputStream();
out=soc.getOutputStream();
sin=new DataInputStream(in);
sout=new DataOutputStream(out);
String s="";
try{
s=sin.readUTF();
}catch(Exception e)
{
}
l1.setText("client :"+s);
addd.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{ try{
l1.setText("waiting");
Recieve c=new Recieve();
c.setSize(300,300);
c.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
c.setVisible(true);
//This above is for opening new JFrame which is not working and also not throwing any error
}catch(Exception e)
{
System.out.println(e.toString());
}
}
});
pan.add(l1);
pan.add(txt1);
pan.add(addd);
add(pan);
}
class Recieve extends JFrame
{
JLabel msg;
JTextField t1;
JButton a1;
Recieve() throws Exception
{
JPanel pan=new JPanel(new GridLayout(2,1));
t1=new JTextField(40);
a1=new JButton("click here to send");
//msg=new JLabel("waiting...");
a1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{ try{
String s;
String str="Server :";
sout.writeUTF(t1.getText());
//msg.setText(str);
sout.flush();
}catch(Exception e)
{
System.out.println(e.toString());
}
}
});
pan.add(t1);
pan.add(a1);
add(pan);
}
}
}
class Server
{
public static void main(String args[]) throws Exception
{
try{
Cli c=new Cli();
c.setSize(300,300);
c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c.setVisible(true);
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
}
推荐答案
那应该是什么?
What shall that be?
Class.forName("com.mysql.jdbc.Driver");
那要进口什么吗?它引发了未捕获的ClassNotFound异常.
将其注释掉,代码将起作用.
Shall that import something? It''s throwing an uncaptured ClassNotFound Exception.
Comment it out and the code will work.
这篇关于用Socket打开JFrame的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文