您的位置:首页 > 编程语言 > Java开发

Java登录界面的开发

2015-07-25 17:42 531 查看
/*1.先定义面板

*2.在面板中定义组件

*3.把组件添加到面板中

*3.把面板添加到框架中

*

* 4.d定义一个监听器类:

* 5.实例化一个对象'

*记住,这里的监听器类和监听器对象以及组件的添加方法都是一一对应的,

*

*(). implements XXXListener

*().实现接口内的所有方法。

*().addXXXListener(XXXEvent e);

*

*

*有哪些监听器接口,可以用来实现呢??

*:KeyListener 接口, ActionListener接口、

* 6.注册给“确认按钮”

*

* 实现一个接口,就要实现该接口内的的所有方法。

*

*

* */

package com.log;

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

public class Login extends JFrame

{

class ActL implements MouseListener

{

public void mouseClicked(MouseEvent e)
// 报错:Multiple markers at this line

// - implements java.awt.event.MouseListener.mouseEntered

// - Cannot reduce the visibility of the inherited method from

// MouseListener

{

String str=tf1.getText();

System.out.println(str);

}

public void mouseEntered(MouseEvent e)

{

}

public void mouseExited(MouseEvent e)

{

}

public void mousePressed(MouseEvent e)

{

}

public void mouseReleased(MouseEvent e)

{

}

ActL()

{

}

}

// 意思是: JPanel=new JPanel(); 错误:Syntax error on token "JPanel", VariableDeclaratorId expected after this token ,在JPanel处,应该有个变量声明

TextField tf1=new TextField("chihaojie");

TextField tf2=new TextField(10);

JButton jb1=new JButton("确认");

JButton jb2=new JButton("取消");

//定义标签

JLabel jL1=new JLabel("用户名");

JLabel jL2=new JLabel("密码");

JPanel jp1=new JPanel();

JPanel jp2=new JPanel();

JPanel jp3=new JPanel();

ActL at=new ActL();

Login()

{

jb1.addMouseListener(at); // 我知道了,是不是addMouseListener(at)是函数调用,而由类定义可知,

// 变量声明都是放在构造函数外,函数调用都放在构造函数内。

// 修改好了,关键是,如何对这些代码进行布局呢???

// 为什么把它放在构造方法内,不报错???

jp1.add(jL1);

jp1.add(tf1);

jp2.add(jL2);

jp2.add(tf2);

jp3.add(jb1);

jp3.add(jb2);

// 在框架的构造方法中,设置布局管理器

this.setLayout(new GridLayout(3,1)); // 学到了 使用 new GridLayout();作为参数 3行一列,在添加组件时,会不会发生覆盖???

this.add(jp1);

this.add(jp2);

this.add(jp3);

//设置框架属性

this.setSize(300,300);//设置框架大小

this.setBackground(Color.blue); //设置框架背景

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setVisible(true);

}

// Multiple markers at this line

// - Cannot reduce the visibility of the inherited method from

// ActionListener

// - implements java.awt.event.ActionListener.actionPerformed

public static void main(String[] args)

{

new Login();

//String str=tf1.getText();提示出错:Cannot make a static reference to the non-static field tf1 这里之所以编译器把tf1当成类,是因为,它没看到,tf1的创建语句。

}

//创建面板中的组件

//向面板中添加,组件

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: