有人说swing编写程序可再用性比较差,通过学习我觉得也是这样,因为界面部分代码实在是很乱。
程序界面:
程序代码:
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import java.net.*;
- import java.io.IOException;
- import java.io.*;
- public class chat {
- public static void main(String[] args) {
- new UDPMessage();
- }
- }
- class UDPMessage extends JFrame implements ActionListener {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- private JTextArea recordText = new JTextArea();
- private JTextArea sendText = new JTextArea();
- private DatagramSocket ds;
- private JButton btnSend = new JButton("发送");
- private JButton btnExit = new JButton("关闭");
- private JLabel otherTips = new JLabel("对方IP");
- private JTextField otherIp = new JTextField();
- private JLabel myTips = new JLabel("我的IP");
- private JTextArea myIp = new JTextArea();
- InputStreamReader in; //创建一个文件输入流
- OutputStreamWriter out; //创建一个文件输出流
- Container p;
- JScrollPane jspRecord;
- JMenuBar mb = new JMenuBar();
- JMenu[] m = {new JMenu("文件"),
- new JMenu("好友"),
- new JMenu("背景")};
- JMenuItem[][] mi = {{new JMenuItem("传送文件")},
- {new JMenuItem("Regina"),new JMenuItem("Taotao"),new JMenuItem("LiShuo")},
- {new JMenuItem("默 认"),new JMenuItem("浅灰色"),new JMenuItem("淡紫色"),new JMenuItem("黄绿色")}};
- public UDPMessage() {
- super("HiChat");
- p = getContentPane();
- setSize(550, 475);
- centerOnScreen();
- setResizable(false);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- for(int i=0; i<m.length; i++) {
- mb.add(m[i]);
- for(int j=0; j<mi[i].length; j++) {
- m[i].add(mi[i][j]);
- mi[i][j].addActionListener(this);
- }
- }
- setJMenuBar(mb);
- p.setBackground(new Color(218, 112, 214));
- p.setLayout(null);
- recordText.setEditable(false);
- recordText.setLineWrap(true);
- jspRecord = new JScrollPane(recordText);
- jspRecord.setBounds(2, 5, 350, 270);
- p.add(jspRecord);
- sendText.setLineWrap(true);
- JScrollPane jspSend = new JScrollPane(sendText);
- jspSend.setBounds(2, 285, 350,100);
- p.add(jspSend);
- btnExit.setBounds(184,390,80,25);
- btnExit.setMargin(new Insets(0, 0, 0, 0));
- btnExit.addActionListener(this);
- p.add(btnExit);
- btnSend.setBounds(271,390,80,25);
- btnSend.setMargin(new Insets(0, 0, 0, 0));
- btnSend.addActionListener(this);
- p.add(btnSend);
- GridLayout gl = new GridLayout(2, 0);
- gl.setVgap(15);
- JPanel infoPane = new JPanel(gl);
- infoPane.setOpaque(false);
- JPanel otherPane = new JPanel(null);
- otherTips.setBounds(10, 10, 80, 20);
- otherTips.setForeground(Color.red);
- otherIp.setBounds(35, 35, 105,20);
- JLabel otherPhoto = new JLabel(new ImageIcon("kabb.jpg"));
- otherPhoto.setBounds(30, 65, 120, 120);
- otherPane.add(otherPhoto);
- otherPane.add(otherTips);
- otherPane.add(otherIp);
- infoPane.add(otherPane);
- JPanel myPane = new JPanel(null);
- myTips.setBounds(10, 10, 80, 20);
- myTips.setForeground(Color.red);
- myIp.setBounds(35, 35, 105,20);
- try{
- myIp.setText(" "+InetAddress.getLocalHost().getHostAddress());
- } catch(UnknownHostException e) {
- e.printStackTrace();
- }
- myIp.setEditable(false);
- JLabel myPhoto = new JLabel(new ImageIcon("kabbn.jpg"));
- myPhoto.setBounds(30, 65, 120, 120);
- myPane.add(myPhoto);
- myPane.add(myTips);
- myPane.add(myIp);
- infoPane.add(myPane);
- infoPane.setBounds(357, 5, 185, 410);
- p.add(infoPane);
- server();
- setVisible(true);
- }
- private void server() {
- try {
- ds = new DatagramSocket(9527);
- byte[] buf = new byte[1024];
- final DatagramPacket dp = new DatagramPacket(buf, buf.length);
- Runnable run = new Runnable() {
- public void run() {
- while(true) {
- try {
- Thread.sleep(100);
- ds.receive(dp);
- int length = dp.getLength();
- String message = new String(dp.getData(), 0, length);
- String ip = dp.getAddress().getHostAddress();
- if(!InetAddress.getLocalHost().getHostAddress().equals(ip)) {
- recordText.append(dp.getAddress().getHostName()+":/n "+message+"/n");
- }
- recordText.setCaretPosition(recordText.getText().length());
- } catch(IOException e) {
- e.printStackTrace();
- } catch(InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- };
- new Thread(run).start();
- } catch(SocketException e) {
- e.printStackTrace();
- }
- }
- public void actionPerformed(ActionEvent e) {
- if(e.getActionCommand() == "默 认") {
- p.setBackground(new Color(240, 255, 255));
- }
- if(e.getActionCommand() == "浅灰色") {
- p.setBackground(new Color(192, 192, 192));
- }
- if(e.getActionCommand() == "淡紫色") {
- p.setBackground(new Color(218, 112, 214));
- }
- if(e.getActionCommand() == "黄绿色") {
- p.setBackground(new Color(127, 255, 0));
- }
- if(e.getActionCommand() == "传送文件") { //功能尚未实现,一下代码无法使用
- /*try {
- ds = new DatagramSocket(9527);
- byte[] buf = new byte[1024];
- File f = new File("Test4.java");
- FileInputStream fis = new FileInputStream(f);
- //FileOutputStream fos = new FileOutputStream(f);
- while(fis.read(buf)>0) {
- final DatagramPacket dp = new DatagramPacket(buf, buf.length);
- ds.receive(dp);
- }
- } catch(IOException ee) {
- ee.printStackTrace();
- }*/
- }
- if(e.getActionCommand() == "Regina") {
- otherIp.setText("对方ip地址");
- }
- if(e.getActionCommand() == "Taotao") {
- otherIp.setText("对方ip地址");
- }
- if(e.getActionCommand() == "LiShuo") {
- otherIp.setText("对方ip地址");
- }
- if(e.getSource() == btnSend) {
- try {
- String ip = otherIp.getText();
- InetAddress addr = InetAddress.getByName(ip);
- byte[] data = sendText.getText().getBytes();
- DatagramPacket dp = new DatagramPacket(data, data.length, addr, 9527);
- String myName = InetAddress.getLocalHost().getHostName();
- recordText.append(myName+":/n "+sendText.getText()+"/n");
- ds.send(dp);
- sendText.setText(null);
- } catch(UnknownHostException ee) {
- ee.printStackTrace();
- } catch(IOException ee) {
- ee.printStackTrace();
- }
- }
- if(e.getSource() == btnExit) {
- System.exit(0);
- }
- }
- public void centerOnScreen() { //使窗体被创建后在屏幕中间显示
- Dimension displaySize = getToolkit().getScreenSize();
- Dimension winSize = getSize();
- int x = (displaySize.width - winSize.width) / 2;
- int y = (displaySize.height - winSize.height) / 2;
- if(x < 0) {
- x = 0;
- }
- if(y < 0) {
- y = 0;
- }
- setLocation(x, y);
- }
- }
swing本来就很庞杂,所以自学习中明白那种最适合自己就选用那种,因为要实现相同效果的程序可以用很多种不同的swing组件来实现,所以我一般是想到那里就学习那个的用法,然后组在一起构成程序。