就这么一点东西花了我俩小时。。。嘤!
注释晚点补上
需要用到eclipse里面的window builder插件(market里面大概率会卡,最后官网URL下载
首先我们需要一个GUI panel:
package clientserver;
public class GUI {
static window w;
static window2 w2;
static server ss;
public GUI() {
ss =new server();
ss.setLocationRelativeTo(null);
ss.setVisible(true);
}
public static void create() {
w=new window();
w2=new window2();
w.setLocation(500,200);
w2.setLocation(900,200);
w.setVisible(true);
w2.setVisible(true);
ss.setVisible(false);
}
public static void main(String[] args) {
GUI gui = new GUI();
}
}
然后是server
package clientserver;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JLabel;
public class server extends JFrame {
private JPanel contentPane;
private JTextField name2;
private JTextField name1;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
server frame = new server();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public server() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnNewButton = new JButton("START");
btnNewButton.setBounds(151, 214, 130, 29);
contentPane.add(btnNewButton);
name2 = new JTextField();
name2.setBounds(151, 149, 130, 26);
contentPane.add(name2);
name2.setColumns(10);
name1 = new JTextField();
name1.setBounds(151, 80, 130, 26);
contentPane.add(name1);
name1.setColumns(10);
JLabel lblEnterFirstName = new JLabel("Enter First Name");
lblEnterFirstName.setBounds(151, 52, 159, 16);
contentPane.add(lblEnterFirstName);
JLabel lblEnterSecondName = new JLabel("Enter Second Name");
lblEnterSecondName.setBounds(151, 121, 144, 16);
contentPane.add(lblEnterSecondName);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
create();
}
});
name2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
create();
}
});
name1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
create();
}
});
}
private void create() {
window.username1 = name1.getText();
window2.username2=name2.getText();
GUI.create();
}
}
然后是Client Side, 可以有很多个,但至少两个
package clientserver;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JLabel;
public class window extends JFrame {
private JPanel contentPane;
static String username1;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
window frame = new window();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public window() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
display1 = new JTextArea();
display1.setBounds(33, 33, 367, 171);
contentPane.add(display1);
text1 = new JTextArea();
text1.setBounds(33, 222, 236, 32);
contentPane.add(text1);
send1 = new JButton("SEND");
send1.setBounds(283, 225, 117, 29);
contentPane.add(send1);
JLabel lblNewLabel = new JLabel(username1);
lblNewLabel.setBounds(33, 6, 61, 16);
contentPane.add(lblNewLabel);
JButton clear1 = new JButton("CLEAR");
clear1.setBounds(283, 1, 117, 29);
contentPane.add(clear1);
send1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String s =text1.getText();
if(s.contentEquals("")) {
return;
}
display1.append(username1 + ": " + s +"\n");
window2.sendText();
text1.setText("");
}
});
clear1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
display1.setText("");
}
});
}
public static void sendText() {
String s = window2.text2.getText();
if(s.equals("")) {
return;
}
display1.append(window2.username2+":" + s + "\n");
}
private javax.swing.JLabel label1;
private static javax.swing.JTextArea display1;
private javax.swing.JButton send1;
public static javax.swing.JTextArea text1;
}