IM项目进程(一)

IM需求

今天我们开始了IM项目的开发,以此作为我们近阶段学习的一次综合性的实践。

项目需求如下:

IM项目进程(一)_第1张图片

由于之前SVN项目我是负责项目后台逻辑处理,于是这次我想尝试前台UI设计。在经过今晚近三个多小时的奋斗,我也查漏补缺了不少知识点,在此总结一下:


知识点总结

1.

//去掉窗口装饰
this.setUndecorated(true);

2.

JComboBox jcb = new JComboBox();
jcb.setEditable(true);
jcb.setBounds(new Rectangle(260, 75, 240, 35));

这是JComboBox


getSelectedItem()

setSelectedItem(String str)


3.

JLabel jl3 = new JLabel("注册");
		jl3.setBounds(new Rectangle(100, 188, 190, 35));
		jl3.setFont(new Font("Dialog", 1, 15));
		jl3.setForeground(Color.blue);

4.

设置窗口最小化

setState(Frame.ICONIFIED);
设置程序关闭

System.exit(0);
用于自定义最小化和关闭按钮,DIY是件很酷的事情哦!


5.

		// 将图片加载到JLabel上
		JLabel label_background = new JLabel(new ImageIcon("image/2.jpg"));
		label_background.setBounds(0, 0, this.getWidth(), this.getHeight());
		// 相当于将JLabel贴在第二层面板的底层
		this.getLayeredPane().add(label_background, new Integer(Integer.MIN_VALUE));
		// JComponent是JPanel的父类,用于拿到窗口的第一层面板
		JComponent jc = (JComponent) this.getContentPane();
		// 设置第一层面板透明化
		jc.setOpaque(false);

源代码分享

view包结构

这是我的view包的结构,其中chat是聊天界面类,listener是监听器类,main是主窗口类,也就是类似QQ的好友列表窗口,start是一开始的登录和注册界面。(希望能学到更规范的命名方法)

IM项目进程(一)_第2张图片

StartFrame

package view.start;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import view.listener.MyActionListener;
import view.listener.MyMouseAdapter;

public class StartFrame extends JFrame {
	public static StartFrame sf;

	public static void main(String[] args) {
		sf = new StartFrame();
	}

	public StartFrame() {
		initStartFrame();
	}

	private void initStartFrame() {
		this.setSize(550, 280);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		this.setLayout(null);
		this.setDefaultCloseOperation(3);
		// 去掉窗口装饰
		this.setUndecorated(true);

		JLabel jl1 = new JLabel("用户名");
		jl1.setBounds(new Rectangle(200, 65, 50, 50));

		JComboBox jcb = new JComboBox();
		jcb.setEditable(true);
		jcb.setBounds(new Rectangle(260, 75, 240, 35));

		JLabel jl2 = new JLabel("密     码");
		jl2.setBounds(new Rectangle(200, 120, 50, 50));

		JPasswordField jpf = new JPasswordField(18);
		jpf.setBounds(new Rectangle(260, 130, 240, 35));

		JButton jb1 = new JButton("登录");
		jb1.setBounds(new Rectangle(260, 187, 240, 35));

		JLabel jl3 = new JLabel("注册");
		jl3.setBounds(new Rectangle(100, 188, 190, 35));
		jl3.setFont(new Font("Dialog", 1, 15));
		jl3.setForeground(Color.blue);
		MyMouseAdapter mma = new MyMouseAdapter(jcb, jpf);
		jl3.addMouseListener(mma);

		// 添加图片
		JPanel jp = new JPanel();
		jp.setBounds(new Rectangle(70, 75, 88, 88));
		jp.setBackground(Color.white);

		// 缩小按钮
		JButton button_narrow = new JButton("narrow");
		button_narrow.setBounds(new Rectangle(450, 0, 50, 35));

		// 关闭按钮
		JButton button_close = new JButton("close");
		button_close.setBounds(new Rectangle(500, 0, 50, 35));

		MyActionListener mal = new MyActionListener(jcb, jpf);
		jb1.addActionListener(mal);
		button_close.addActionListener(mal);
		button_narrow.addActionListener(mal);

		// 将图片加载到JLabel上
		JLabel label_background = new JLabel(new ImageIcon("image/2.jpg"));
		label_background.setBounds(0, 0, this.getWidth(), this.getHeight());
		// 相当于将JLabel贴在第二层面板的底层
		this.getLayeredPane().add(label_background, new Integer(Integer.MIN_VALUE));
		// JComponent是JPanel的父类,用于拿到窗口的第一层面板
		JComponent jc = (JComponent) this.getContentPane();
		// 设置第一层面板透明化
		jc.setOpaque(false);

		this.add(button_narrow);
		this.add(button_close);
		this.add(jp);
		this.add(jl1);
		this.add(jcb);
		this.add(jl2);
		this.add(jpf);
		this.add(jb1);
		this.add(jl3);

		this.setVisible(true);
	}
}


MyActionListener

package view.listener;

import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import view.start.StartFrame;

public class MyActionListener implements ActionListener {
	private JComboBox jcb;
	private JPasswordField jpf;

	public MyActionListener(JComboBox jcb, JPasswordField jpf) {
		this.jcb = jcb;
		this.jpf = jpf;
	}

	public void actionPerformed(ActionEvent e) {
		JButton jb = (JButton) e.getSource();
		String type = jb.getText();
		// 获得用户名和密码
		String name = (String) jcb.getSelectedItem();
		String psd = jpf.getText();
		switch (type) {
		case "登录":
			// TODO 登录操作
			System.out.println("login" + name + psd);
//			StartFrame.sf.setVisible(false);
			break;
		case"narrow":
			StartFrame.sf.setState(Frame.ICONIFIED);
			break;
		case"close":
			System.exit(0);
			break;
		}
		jcb.setSelectedItem("");
		jpf.setText("");
	}

}


MyMouseListener

package view.listener;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JComboBox;
import javax.swing.JPasswordField;

import view.start.StartFrame;

public class MyMouseAdapter extends MouseAdapter {
	private JComboBox jcb;
	private JPasswordField jpf;

	public MyMouseAdapter(JComboBox jcb, JPasswordField jpf) {
		this.jcb = jcb;
		this.jpf = jpf;
	}

	public void mouseClicked(MouseEvent e) {
		// 获得用户名和密码
		String name = (String) jcb.getSelectedItem();
		String psd = jpf.getText();
		// TODO 注册操作
		System.out.println("register" + name + psd);
		// StartFrame.sf.setVisible(false);
	}
}

运行结果(登录和注册界面)

IM项目进程(一)_第3张图片

自我感觉

界面还是个雏形,肯定需要更进一步的完善(swing原配太丑了。。。)随着项目的推进,最后希望能够呈现一个比较好的效果,今晚出了做这个界面,还有复习了以前的一些知识点。三个小时如白马过隙,总算明白程序员为什么总是熬夜,原来是代码敲着敲着,兴趣来了,停不下来了吧。(听说大多数情况是为了赶项目。。。我毕竟太年轻。。。)



你可能感兴趣的:(IM项目进程(一))