大家好,我正在写一个类似聊天程序,我在里面输内容后发送,没有反应,是不是线程错了?我现在在显示区域也能输内容,怎样才能让显示区域不能输内容呢我的代码:
public class Server extends JFrame implements Runnable//服务器
{
private JTextArea jta1 = new JTextArea(10, 25);
private JTextArea jta2 = new JTextArea(10, 25);
private JButton sent = new JButton("确定"); public Server(String title)
{
super(title);
this.Window();
} public void Window()
{
this.setSize(400, 500);
Dimension d = this.getToolkit().getScreenSize();
this.setLocation((d.width - this.getWidth())/2, (d.height - this.getHeight())/2);
this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
this.add(complent());
} public JPanel complent()
{
JPanel jp = new JPanel();
jp.setLayout(null); JScrollPane jsp1=new JScrollPane(jta1,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
jsp1.setBounds(10, 10, 370, 300);
jp.add(jsp1); JScrollPane jsp2=new JScrollPane(jta2,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
jsp2.setBounds(10, 320, 370, 70);
jp.add(jsp2); sent.setBounds(300, 400, 80, 20);
jp.add(sent); return jp;
} public void run()
{
ServerSocket listener = null;
Socket socket = null;
BufferedReader br = null;
BufferedWriter bw = null;
try
{
listener = new ServerSocket(8888);
socket = listener.accept();
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (IOException e)
{
e.printStackTrace();
}
} class MyWindowListener extends WindowAdapter
{
public void windowClosing(WindowEvent arg0)
{
int choice = JOptionPane.showConfirmDialog(null, "真的要退出吗","确认退出",JOptionPane.YES_NO_OPTION);
if(choice == JOptionPane.YES_OPTION)
{
Server.this.dispose();
System.exit(0);
}
}
} public static void main(String[] args)
{
Server s = new Server("Server");
s.setVisible(true);
s.run();
}
}public class Client extends JFrame implements Runnable//客户端
{
private JTextArea jta1 = new JTextArea(10, 25);
private JTextArea jta2 = new JTextArea(10, 25);
private JButton sent = new JButton("确定"); public Client(String title)
{
super(title);
this.Window();
Socket socket = null;
try
{
socket = new Socket(InetAddress.getByName("127.0.0.1"),8888);
new Thread(this).start();
} catch (UnknownHostException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
} public void Window()
{
this.setSize(400, 500);
Dimension d = this.getToolkit().getScreenSize();
this.setLocation((d.width - this.getWidth())/2, (d.height - this.getHeight())/2);
this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
this.add(complent());
} public JPanel complent()
{
JPanel jp = new JPanel();
jp.setLayout(null); JScrollPane jsp1=new JScrollPane(jta1,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
jsp1.setBounds(10, 10, 370, 300);
jp.add(jsp1); JScrollPane jsp2=new JScrollPane(jta2,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
jsp2.setBounds(10, 320, 370, 70);
jp.add(jsp2); sent.setBounds(300, 400, 80, 20);
jp.add(sent); return jp;
} public void run()
{
Socket socket = null;
try
{
socket = new Socket(InetAddress.getByName("127.0.0.1"),8888);
} catch (IOException e)
{
e.printStackTrace();
}
} class MyWindowListener extends WindowAdapter
{
public void windowClosing(WindowEvent arg0)
{
int choice = JOptionPane.showConfirmDialog(null, "真的要退出吗","确认退出",JOptionPane.YES_NO_OPTION);
if(choice == JOptionPane.YES_OPTION)
{
Client.this.dispose();
System.exit(0);
}
}
} public static void main(String[] args)
{
Client c = new Client("Client");
c.setVisible(true);
}
}
public class Server extends JFrame implements Runnable//服务器
{
private JTextArea jta1 = new JTextArea(10, 25);
private JTextArea jta2 = new JTextArea(10, 25);
private JButton sent = new JButton("确定"); public Server(String title)
{
super(title);
this.Window();
} public void Window()
{
this.setSize(400, 500);
Dimension d = this.getToolkit().getScreenSize();
this.setLocation((d.width - this.getWidth())/2, (d.height - this.getHeight())/2);
this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
this.add(complent());
} public JPanel complent()
{
JPanel jp = new JPanel();
jp.setLayout(null); JScrollPane jsp1=new JScrollPane(jta1,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
jsp1.setBounds(10, 10, 370, 300);
jp.add(jsp1); JScrollPane jsp2=new JScrollPane(jta2,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
jsp2.setBounds(10, 320, 370, 70);
jp.add(jsp2); sent.setBounds(300, 400, 80, 20);
jp.add(sent); return jp;
} public void run()
{
ServerSocket listener = null;
Socket socket = null;
BufferedReader br = null;
BufferedWriter bw = null;
try
{
listener = new ServerSocket(8888);
socket = listener.accept();
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (IOException e)
{
e.printStackTrace();
}
} class MyWindowListener extends WindowAdapter
{
public void windowClosing(WindowEvent arg0)
{
int choice = JOptionPane.showConfirmDialog(null, "真的要退出吗","确认退出",JOptionPane.YES_NO_OPTION);
if(choice == JOptionPane.YES_OPTION)
{
Server.this.dispose();
System.exit(0);
}
}
} public static void main(String[] args)
{
Server s = new Server("Server");
s.setVisible(true);
s.run();
}
}public class Client extends JFrame implements Runnable//客户端
{
private JTextArea jta1 = new JTextArea(10, 25);
private JTextArea jta2 = new JTextArea(10, 25);
private JButton sent = new JButton("确定"); public Client(String title)
{
super(title);
this.Window();
Socket socket = null;
try
{
socket = new Socket(InetAddress.getByName("127.0.0.1"),8888);
new Thread(this).start();
} catch (UnknownHostException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
} public void Window()
{
this.setSize(400, 500);
Dimension d = this.getToolkit().getScreenSize();
this.setLocation((d.width - this.getWidth())/2, (d.height - this.getHeight())/2);
this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
this.add(complent());
} public JPanel complent()
{
JPanel jp = new JPanel();
jp.setLayout(null); JScrollPane jsp1=new JScrollPane(jta1,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
jsp1.setBounds(10, 10, 370, 300);
jp.add(jsp1); JScrollPane jsp2=new JScrollPane(jta2,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
jsp2.setBounds(10, 320, 370, 70);
jp.add(jsp2); sent.setBounds(300, 400, 80, 20);
jp.add(sent); return jp;
} public void run()
{
Socket socket = null;
try
{
socket = new Socket(InetAddress.getByName("127.0.0.1"),8888);
} catch (IOException e)
{
e.printStackTrace();
}
} class MyWindowListener extends WindowAdapter
{
public void windowClosing(WindowEvent arg0)
{
int choice = JOptionPane.showConfirmDialog(null, "真的要退出吗","确认退出",JOptionPane.YES_NO_OPTION);
if(choice == JOptionPane.YES_OPTION)
{
Client.this.dispose();
System.exit(0);
}
}
} public static void main(String[] args)
{
Client c = new Client("Client");
c.setVisible(true);
}
}
{
ServerSocket listener = null;
Socket socket = null;
BufferedReader br = null;
BufferedWriter bw = null;
try
{
listener = new ServerSocket(8888);
socket = listener.accept();
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (IOException e)
{
e.printStackTrace();
}
}public void run()//客户端
{
Socket socket = null;
try
{
socket = new Socket(InetAddress.getByName("127.0.0.1"),8888);
} catch (IOException e)
{
e.printStackTrace();
}
}
run里面写发送逻辑