java学习应用:可视化窗口模拟基础聊天窗口(模仿QQ)

最近在由于学校有个机器人大赛,所以举办方进行了几节java的教学,接下来算是我对最近一些学习的总结,记录一下,有待提高!

一、java可视化窗口JFrame的简单运用,先看看一些简单的效果(真的很基础):

1、调出登入界面

                                  java学习应用:可视化窗口模拟基础聊天窗口(模仿QQ)_第1张图片

2、输入账号密码(没有数据库,只好自己定了)、

2.1、密码错误

                         java学习应用:可视化窗口模拟基础聊天窗口(模仿QQ)_第2张图片 

2.2、密码正确

         java学习应用:可视化窗口模拟基础聊天窗口(模仿QQ)_第3张图片 

3、点开聊天界面

         java学习应用:可视化窗口模拟基础聊天窗口(模仿QQ)_第4张图片

4、发送

                                         java学习应用:可视化窗口模拟基础聊天窗口(模仿QQ)_第5张图片


5、下面是代码,以后加油完善,先记录下

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class myChat
{
	public void loginUI()
	{
		//creat a new frame for loginUI
		JFrame login_frame = new JFrame();
		login_frame.setSize(300,200);			// we must set size of frame,otherwise ,the frame will be 0 x 0
		login_frame.setTitle("myChat");			//Title
		login_frame.setLocationRelativeTo(null);//locate the frame to the mid of screen 
		login_frame.setLayout(new FlowLayout());//Set linear distribution
		JLabel usrname = new JLabel("usrename");
		JLabel psw = new JLabel("password");
		JTextField f1 = new JTextField(18);
		JPasswordField f2 = new JPasswordField(18);
		JButton login = new JButton("login"); // set a button called "login"
		JButton reset = new JButton("reset"); // set a button called "reset"

		//Adding components,flow the list
		login_frame.add(usrname);
		login_frame.add(f1);
		login_frame.add(psw);
		login_frame.add(f2);
		login_frame.add(login);
		login_frame.add(reset);
		//Listener-click
		ActionListener listener = new ActionListener()
		{
			//the abstract method of ActionListener
			public void actionPerformed(ActionEvent event)
			{
				String textName = f1.getText();
				String textPassword = f2.getText();
				//if we want to go to next step ,we must close.
				login_frame.setVisible(false);
				//check the password and username
				if(textName.equals("admin") && textPassword.equals("123"))
				{
					mainUI(); // right -> goto next step
				}
				// error -> point error
				else
				{
					JFrame login_Error = new JFrame();//set a frame to point the error message
					login_Error.setSize(200,100);
					login_Error.setLocationRelativeTo(null);
					JLabel reLoginMessage = new JLabel("username or password error !"); //add the message
					JButton sureButton = new JButton("sure");
					login_Error.add(reLoginMessage);
					login_Error.add(sureButton);
					ActionListener errorListener = new ActionListener()
					{ 
						public void actionPerformed(ActionEvent event)
						{
							login_Error.setVisible(false); //let the error frame hide
							loginUI();					   //let the login frame dispaly;
						}
					};
					//set the listener for sureButton
					sureButton.addActionListener(errorListener);
					login_Error.setVisible(true);
				}
			}
		};
		//set an ActionListener for the button
		login.addActionListener(listener);
		login_frame.setVisible(true);

	}
	//mainUI
	public void mainUI()
	{
		//name list
		String [] nameStd = {"one","two","three","four","five","six","seven","eight","nine","ten"};

		JFrame main_frame = new JFrame();
		main_frame.setTitle("main_frame");
		main_frame.setLayout(new FlowLayout());
		main_frame.setSize(260,600);
		main_frame.setLocation(900,50);
		for(int i=0;i<10;i++)
		{
			String friend_name=nameStd[i];
			JButton now = new JButton(friend_name);
			now.setPreferredSize(new Dimension(200,50));
			main_frame.add(now);
			// chose someone to talk
			ActionListener listener = new ActionListener()
			{
				public void actionPerformed(ActionEvent event)
				{
					chatUI(friend_name);
				}
			};
			now.addActionListener(listener);
		}
		main_frame.setVisible(true);
	}
	//chatUI
	public void chatUI(String talkWith){
		JFrame chat_frame = new JFrame();
		chat_frame.setTitle(talkWith);
		chat_frame.setSize(450,480);
		chat_frame.setLocationRelativeTo(null);
		chat_frame.setLayout(new FlowLayout());

		JTextArea show_area = new JTextArea(15,35);
		JTextArea input_area = new JTextArea(7,35);
		JButton send = new JButton("send");
		// if we click "send" ,the imformation of input_are will turn to show_area 
		ActionListener listener = new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				show_area.append(input_area.getText()+"\r\n");
				show_area.setLineWrap(true);
				input_area.setText("");
			}
		};
		// send imformation
		send.addActionListener(listener);

		chat_frame.add(show_area);
		chat_frame.add(input_area);
		chat_frame.add(send);


		chat_frame.setVisible(true);

	}
	//main method
	public static void main(String[] agrs)
	{
		myChat qq = new myChat();
		qq.loginUI();
	}
}

英语比较差,还在练习中………


你可能感兴趣的:(java学习应用:可视化窗口模拟基础聊天窗口(模仿QQ))