源代码如下:只需要修改界面段(谢谢)
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Dimension;
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.*;
//import javax.swing.border.
import javax.swing.event.*;class JabberClient1 extends JPanel{
//private JScrollPane scrollPane=new JScrollPane();//定义滚动条,用于文本域
TextField tf=null;
TextArea ta=null;
JLabel label=null;
JList list=null;
JButton but1=null;
JPanel p1=null;
JPanel p2=null;
private Socket socket;
private BufferedReader in;
private PrintWriter out;
String[] s={"美国","中国","英国","法国","意大利","澳洲","韩国"};
public boolean action(Event e,Object o) {
String outStr=tf.getText();
tf.setText(null);
out.println(outStr);
ta.append("I say:"+outStr);
ta.append("\n");
return true;
}
public JabberClient1(InetAddress addr) {
super();
JFrame f=new JFrame();
Container contentPane=f.getContentPane();
contentPane.setLayout(new BorderLayout());
//GridBagConstraints c = new GridBagConstraints();
tf=new TextField(20);
ta=new TextArea();
tf.setFocusable(true);
ta.setEditable(false);//不能被编辑
ta.setFocusable(false);//设置文本域得不到焦点
ta.setSize(200,150);
label=new JLabel("我说");
list=new JList(s);
list.setSize(250,250);
but1=new JButton();
but1.setText("说");
p1=new JPanel();
p2=new JPanel();
list.setBorder(BorderFactory.createTitledBorder("我的好友?"));
contentPane.add(new JScrollPane(list),BorderLayout.EAST);//设置list的滚动条
//添加文本域\下拉列表 p1.setLayout(new GridBagLayout());
p1.add(ta);
p1.add(list);
contentPane.add(p1,BorderLayout.NORTH);
//挂标签\文本框\及按钮
p2.add(label);
p2.add(tf);
p2.add(but1);
contentPane.add(p2,BorderLayout.SOUTH);
f.setLocationRelativeTo(null);//
f.show();
f.setSize(500,200);
try {
socket = new Socket(addr, 9999);
} catch(IOException e) {}
ta.append("Client host:"+socket.getInetAddress().getHostName()+"\n\n");
try{
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
out.println("Start");
String str = in.readLine();
System.out.println(str);
ta.append(str);
ta.append("\n");
while(true){
try{
Thread.sleep(100);
} catch (InterruptedException e){};
str = in.readLine();
ta.append(str);
ta.append("\n");
System.out.println(str);
if ( str.equals("Disconnect from Server") ){
break;
}
}
} catch(IOException e) {
try {
socket.close();
} catch(IOException e2) {}
} finally {
try {
socket.close();
} catch(IOException e) {}
}
}
}
public class Client1 {
public static void main(String[] args)throws IOException, InterruptedException{
InetAddress addr = InetAddress.getByName(null);
new JabberClient1(addr);
}
}
无论用什么布局管理器都行(要求如下):
TextArea 放在左上角
JList 紧靠TextArea,放在右面
然后换一行在放:
JLabel .....TextField .....JButton
如果最大化,要使其控件自动合理的增长.
如果笔者用GridBagLayout最好,只需要修改界面段(谢谢)
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Dimension;
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.*;
//import javax.swing.border.
import javax.swing.event.*;class JabberClient1 extends JPanel{
//private JScrollPane scrollPane=new JScrollPane();//定义滚动条,用于文本域
TextField tf=null;
TextArea ta=null;
JLabel label=null;
JList list=null;
JButton but1=null;
JPanel p1=null;
JPanel p2=null;
private Socket socket;
private BufferedReader in;
private PrintWriter out;
String[] s={"美国","中国","英国","法国","意大利","澳洲","韩国"};
public boolean action(Event e,Object o) {
String outStr=tf.getText();
tf.setText(null);
out.println(outStr);
ta.append("I say:"+outStr);
ta.append("\n");
return true;
}
public JabberClient1(InetAddress addr) {
super();
JFrame f=new JFrame();
Container contentPane=f.getContentPane();
contentPane.setLayout(new BorderLayout());
//GridBagConstraints c = new GridBagConstraints();
tf=new TextField(20);
ta=new TextArea();
tf.setFocusable(true);
ta.setEditable(false);//不能被编辑
ta.setFocusable(false);//设置文本域得不到焦点
ta.setSize(200,150);
label=new JLabel("我说");
list=new JList(s);
list.setSize(250,250);
but1=new JButton();
but1.setText("说");
p1=new JPanel();
p2=new JPanel();
list.setBorder(BorderFactory.createTitledBorder("我的好友?"));
contentPane.add(new JScrollPane(list),BorderLayout.EAST);//设置list的滚动条
//添加文本域\下拉列表 p1.setLayout(new GridBagLayout());
p1.add(ta);
p1.add(list);
contentPane.add(p1,BorderLayout.NORTH);
//挂标签\文本框\及按钮
p2.add(label);
p2.add(tf);
p2.add(but1);
contentPane.add(p2,BorderLayout.SOUTH);
f.setLocationRelativeTo(null);//
f.show();
f.setSize(500,200);
try {
socket = new Socket(addr, 9999);
} catch(IOException e) {}
ta.append("Client host:"+socket.getInetAddress().getHostName()+"\n\n");
try{
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
out.println("Start");
String str = in.readLine();
System.out.println(str);
ta.append(str);
ta.append("\n");
while(true){
try{
Thread.sleep(100);
} catch (InterruptedException e){};
str = in.readLine();
ta.append(str);
ta.append("\n");
System.out.println(str);
if ( str.equals("Disconnect from Server") ){
break;
}
}
} catch(IOException e) {
try {
socket.close();
} catch(IOException e2) {}
} finally {
try {
socket.close();
} catch(IOException e) {}
}
}
}
public class Client1 {
public static void main(String[] args)throws IOException, InterruptedException{
InetAddress addr = InetAddress.getByName(null);
new JabberClient1(addr);
}
}
无论用什么布局管理器都行(要求如下):
TextArea 放在左上角
JList 紧靠TextArea,放在右面
然后换一行在放:
JLabel .....TextField .....JButton
如果最大化,要使其控件自动合理的增长.
如果笔者用GridBagLayout最好,只需要修改界面段(谢谢)
f.setResizable(false);
就可以了.
// contentPane.add(new JScrollPane(list),BorderLayout.EAST);//设置list的滚动条
//添加文本域\下拉列表 p1.setLayout(new GridBagLayout());// p1.add(ta);
// p1.add(list);
p1.add(ta, new GridBagConstraints(0, 0, 2, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(10, 10, 10, 10), 0, 0)); p1.add(new JScrollPane(list), new GridBagConstraints(2, 0, 2, 1, 1.0, 1.0
,GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(10, 0, 10, 10), 0, 0)); p1.add(label, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE,
new Insets(0, 10, 10, 10), 0, 0));
//
p1.add(tf, new GridBagConstraints(1, 1, 1, 1, 1.0, 1.0
,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
new Insets(0, 0, 10, 10), 0, 0));
//
p1.add(but1, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
new Insets(0, 0, 10, 10), 0, 0));
contentPane.add(p1,BorderLayout.NORTH);
// //挂标签\文本框\及按钮
// p2.add(label);
// p2.add(tf);
// p2.add(but1);
// contentPane.add(p2,BorderLayout.SOUTH); f.setLocationRelativeTo(null);//
f.setSize(500,200);
f.show();