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的创建语句。
}
//创建面板中的组件
//向面板中添加,组件
}
*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的创建语句。
}
//创建面板中的组件
//向面板中添加,组件
}
相关文章推荐
- javac一次性编译多个包下的.java文件
- java面向对象--07
- 跟据经纬度实现附近搜索Java实现
- Spring 学习笔记-- Spring ContextLoaderListener 解析
- java 重写与重载笔记
- Java学习之路0720(六)(类与对象、封装、继承)
- 基于JavaMail API收发邮件的方法
- struts 属性驱动
- 【Java】 Base64原理及简单应用
- java:图形化界面awt基本实例
- java Integer与int的装箱(缓存)与拆箱
- 重学java23种设计模式(3)建造者模式
- JSON解析、JAVA常用的工具类、JAVA集合框架、JAVA泛型、枚举
- Struts2中关于namespace的学习心得
- 非阻塞的Remote Debug(二)
- 深入理解Java对象序列化
- Myeclipse2014 激活 (包括方法和工具)
- struts ognl表达式$%#三个符号使用
- JAVA-开发环境搭建
- 详解Java虚拟机类加载