`
百合不是茶
  • 浏览: 345175 次
社区版块
存档分类
最新评论

qq简单界面的实现

阅读更多

qq编写简单界面,主要的是将图片添加到界面,在网上观察了别人的写法再加上自己的;

 

编写界面需要用的是:

JFrame 窗体

JPanel 容器

FlowLayout()  流式布局  JPanel默认的布局

BorderLayout ()   JFrame默认的布局

  

Container con =this.getContentPane();  容器

 

Jlabel  标签

ImageIcon images = new ImageIcon("qqbk.png");

images.getImage();

创建图片对象并获得图片

 

label1.setIcon(images);  添加到容器中

setBounds(); 位置,及创建的高度和宽度

 

JTextField() 文本输入

JPasswordField()密码输入框

JCheckBox()创建多选框

JComboBox()下拉框

 

监听器:ActionListener 动作监听器



 

 

package com.ui.com;

import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class qq  extends JFrame{

	public static void main(String[] args) {
    	qq q = new qq();
    	q.qqinit();

	}
	
	public void qqinit(){
		this.setTitle("qq界面");
		this.setSize(400, 300);
		this.setDefaultCloseOperation(3);
//		this.getContentPane().setBackground(Color.BLACK);
		//设置流式布局
		this.setLayout(new FlowLayout());
		//设置禁止改变窗体的大小
		this.setResizable(false);
		
		
		//添加组件
		//将图片添加到窗体上面
	  //创建容器,设置背景图片
		Container con =this.getContentPane();
		JLabel label1 = new JLabel();
//		Image image1 = new ImageIcon("qqbk.png").getImage();
//		label1.setIcon(new ImageIcon(image1));
		//设置界面的背景图片
		ImageIcon images = new ImageIcon("qqbk.png");
		images.getImage();
		label1.setIcon(images);
		label1.setBounds(0,0,400,300);
		//将背景图片添加到容器中
		con.add(label1);
		
		//qq图像的设定
		JLabel label2 = new JLabel();
		//将图添加到界面
//		Image image2 = new ImageIcon("qqtx.png").getImage();
//		label2.setIcon(new ImageIcon(image2));
		//将图片添加到界面
		ImageIcon icon = new ImageIcon("qqtx.png");
		icon.getImage();
		label2.setIcon(icon);
		//设置指定的位置
		label2.setBounds(40,95,79,79);
		label1.add(label2);
		
		
		//添加账号输入框,并添加在窗体上
		JTextField username = new JTextField();
		username.setBounds(140,95,160,25);
		JLabel label3 = new JLabel("注册账号:");
		label3.setBounds(300,95,70,25);
		label1.add(username);
		label1.add(label3);
		
		//添加密码输入框,添加在窗体上
		JPasswordField  pwd = new JPasswordField();
		pwd.setBounds(140,135,160,25);
		JLabel label4 = new JLabel("找回密码:");
		label4.setBounds(300,135,70,25);
		label1.add(pwd);
		label1.add(label4);
		
		//输入框下方的选择
		//使用JCheckBox创建多选框
		JCheckBox box1 = new JCheckBox("记住密码");
		box1.setBounds(140,170,80,15);
		JCheckBox box2 = new JCheckBox("自动登陆");
		box2.setBounds(230,170,80,15);
		label1.add(box1);
		label1.add(box2);
		
		//在线状态的选择
		JComboBox<String> com = new JComboBox<String>();
		//在下拉框中添加属性
		com.addItem("在线");
		com.addItem("离线");
		com.addItem("隐身");
		//设置该框在界面中的位置
		com.setBounds(40,175,79,20);
		//添加到容器中
		label1.add(com);
		
		//设置界面的按钮
		JButton  btn1 = new JButton("登陆");
		btn1.setBounds(250,220,80,30);
		label1.add(btn1);
		//设置取消按钮
		JButton  btn2 = new JButton("取消");
		btn2.setBounds(150,220,80,30);
		label1.add(btn2);
		
		this.setVisible(true);
		//添加监听器对象
     qqListener qlis = new qqListener(username,pwd,this);
     //将登陆,取消  加入监听器
     btn1.addActionListener(qlis);
     btn2.addActionListener(qlis);
	 }

}

 

package com.ui.com;

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

import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class qqListener implements ActionListener {
	private JTextField username;
	private JPasswordField pwd;
	private qq qq;
	
	public qqListener(JTextField username, JPasswordField pwd, qq qq) {
		this.username = username;
		this.pwd = pwd;
		this.qq = qq;
	}
//实现监听器的方法
	public void actionPerformed(ActionEvent e){
//		String str = (String) e.getSource();
//		System.out.println(str+">>>>>>>>>>>>>>>>>");
		//获得默认的动作按钮
		String command = e.getActionCommand();
		System.out.println("command "+command);
		//判断是否登陆
		if(command.equals("登陆")){
			//得到username框中的字符
			String name = username.getText();
			//得到密码框中的字符串
			String pwds = pwd.getText();
			//判断账号和密码是否都为1
			if(name.equals("1")&&pwds.equals("1")){
				//如果为1 ,就不显示现在的界面
				qq.setVisible(false);
				//要显示的界面
				mainUI mu = new mainUI();
				mu.maininit();
				}else{
					//如果密码和账号错误弹出提示框
					JOptionPane.showMessageDialog(null, "帐号错误");
				}
			//打印账号和密码
			System.out.println("账号" + username);
			System.out.println("密码" + pwd);
		}else  {
			qq.setVisible(false);
		}
	}
}
 

 

  • 大小: 172.7 KB
0
2
分享到:
评论
1 楼 chenzheng8975 2014-07-04  
java 的awt基本没公司用

相关推荐

Global site tag (gtag.js) - Google Analytics