用到的工具:java语言,IDEA软件
简单界面的设计
总体思路:先定义一个UI类,在类中定义一个initUI的方法,在此方法中定义并添加窗体或者组件,再运用main函数执行。
需要注意的知识点:1、JFrame jf = new JFrame();用来创建窗体
2、jf.settittle("登陆界面")等方法定义窗体标题、大小等属性
3、定义组件(可以定义尺寸)
4、把组件添加到窗体上jf.add()
5、使界面可视化jf.setVisible (true);
6、定义监听器
源代码如下:
package test1;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class UI{
public void initUI(){
JFrame jf = new JFrame ();
jf.setTitle ("登录界面");//窗体标题
jf.setSize (500, 700);//窗体大小
// 设置关闭时 直接退出进程
jf.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
// 布局管理 流式布局
FlowLayout fl = new FlowLayout ();
jf.setLayout (fl);
// 创建组件对象 按钮 输入框 标签
JButton btn = new JButton ("登录");
JLabel namejla = new JLabel ("账号:");
JLabel pwdjla = new JLabel ("密码:");
JTextField nameField = new JTextField();
JPasswordField pwdField = new JPasswordField();
// 图片
ImageIcon img = new ImageIcon("C:\\\\Users\\\\LENOVO\\\\Desktop\\\\蓝杰\\\\图片30.JPG");
JLabel imgjla = new JLabel();
imgjla.setIcon(img);
// 窗体设置图标
jf.setIconImage (img.getImage ());
// 设置尺寸
Dimension dim = new Dimension ();
dim.setSize (410,50);
nameField.setPreferredSize (dim);
pwdField.setPreferredSize (dim);
// 把组件加载到窗体上
jf.add (imgjla);// 显示图片标签
jf.add (namejla);
jf.add (nameField);
jf.add (pwdjla);
jf.add (pwdField);
jf.add (btn);
// 可视化 显示到屏幕上 等窗体加载完组件之后再执行
jf.setVisible (true);
// 事件监听器:
btn.addActionListener (new ActionListener (){// 匿名内部类
int count=0;
// 点击按钮才会调用
@Override
public void actionPerformed(ActionEvent e){
System.out.println ("点击了按钮"+(count++));
System.out.println ("账号:"+nameField.getText ());
System.out.println ("密码:"+pwdField.getText ());
}
});
}
public static void main(String[] args){
UI ui = new UI ();
ui.initUI ();
}
}
界面设计的进一步设计
我们将监听器类单独写出来
总体思路:两个类--监听器类和UI类
需要注意的知识点:1、监听器类中的JTextField nameField; JPasswordField pwdField;可以在UI类中调用。
监听器类
UI类
2、public class ButtonAction implements ActionListener, MouseListener{}
注意implements 是实现多个接口, 接口的方法一般为空的, 必须重写才能使用
3、JOptionPane.showMessageDialog (null,"您已成功登录!");是弹出窗体的方法
运行界面
右键弹出的窗体界面
点击B,C,D按钮的界面
输入账号密码可以检验正确性
显示按压位置并输出
源代码:
ButtonAction类
package test02;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
/**
* 创建一个类实现监听器接口
*/
public class ButtonAction implements ActionListener, MouseListener{
/**
* 重写接口中所有的方法 加上大括号
* 抽象-实现
*/
JTextField nameField;
JPasswordField pwdField;
public void setNameField(JTextField nameField){
this.nameField = nameField;
}
public void setPwdField(JPasswordField pwdField){
this.pwdField = pwdField;
}
public void actionPerformed(ActionEvent e){
/**
* 写点击按钮之后要执行的代码
*/
System.out.println ("按钮被点击了");
/**
* 获取当前被点击的按钮的数据
*/
String btnstr = e.getActionCommand ();
System.out.println ("点击了"+btnstr);
if(btnstr.equals ("登录")){
// 弹窗
JOptionPane.showMessageDialog (null,"您已成功登录!");
System.out.println ("账号:"+nameField.getText ());
System.out.println ("密码:"+pwdField.getText ());
String nametext = nameField.getText();
String pwdtext = pwdField.getText();
if(nametext.equals("123")){
System.out.println("账号正确");
if(pwdtext.equals("234")){
System.out.println("密码正确");
}else{
System.out.println("密码错误");
}
}else{
System.out.println("账号错误");
}
}
else{
JOptionPane.showMessageDialog (null,"点击此按钮不能登陆!");
}
}
public void mouseClicked(MouseEvent e){
System.out.println ("鼠标点击了窗体");
}
/**
* Invoked when a mouse button has been pressed on a component.
* @param e the event to be processed
*/
JFrame jf = new JFrame ();
public void mousePressed(MouseEvent e){
int x = e.getX ();
int y = e.getY ();
int buttonid = e.getButton ();
System.out.println ("鼠标按键:"+buttonid);
if(buttonid==MouseEvent.BUTTON3){
System.out.println ("右键");
JLabel jla = new JLabel("你右键干嘛?");
jf.setTitle("点击右键出现的窗体");
jf.add(jla);
jf.setSize (600,400);
jf.setVisible (true);
}else {
jf.setVisible (false);
}
System.out.println ("鼠标在窗体上"+"x:"+x+" y: "+y+"按下了");
}
/**
* Invoked when a mouse button has been released on a component.
* @param e the event to be processed
*/
public void mouseReleased(MouseEvent e){
System.out.println ("鼠标在窗体上松开了");
}
/**
* Invoked when the mouse enters a component.
* @param e the event to be processed
*/
public void mouseEntered(MouseEvent e){
System.out.println ("鼠标进入窗体");
}
/**
* Invoked when the mouse exits a component.
* @param e the event to be processed
*/
public void mouseExited(MouseEvent e){
System.out.println ("鼠标离开窗体");
}
}
UI类
package test02;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class UI {
String[] btnstrs = {"登录","B","C","D"};
public void initUI(){
JFrame jf = new JFrame();
jf.setTitle("登录界面");
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jf.setSize(500,800);
FlowLayout fl = new FlowLayout();
jf.setLayout(fl);
JLabel namejla = new JLabel ("账号:");
JLabel pwdjla = new JLabel ("密码:");
JTextField nameField = new JTextField();
JPasswordField pwdField = new JPasswordField();
// 设置尺寸
Dimension dim = new Dimension ();
dim.setSize (410,50);
nameField.setPreferredSize (dim);
pwdField.setPreferredSize (dim);
// 把组件加载到窗体上
jf.add (namejla);
jf.add (nameField);
jf.add (pwdjla);
jf.add (pwdField);
// 监听器(动作 鼠标)
ButtonAction btnaction = new ButtonAction();
for(int i = 0; i < btnstrs.length; i++){
JButton btn = new JButton (btnstrs[i]);
btn.addActionListener (btnaction);// 按钮绑定实现的监听器类对象
jf.add (btn);
}
//btnaction.nameField = nameField;
//btnaction.pwdField = pwdField;
btnaction.setNameField(nameField);
btnaction.setPwdField(pwdField);
// 窗体添加鼠标监听器
jf.addMouseListener (btnaction);
// 可视化 显示到屏幕上 等窗体加载完组件之后再执行
jf.setVisible (true);
}
public static void main(String[] args){
UI ui = new UI();
ui.initUI ();
}
}
初学者,谢谢大家!