JAVA_XXBJ(14)面板添加文本框和按钮

导入图片

虽然Java里面有画笔,但我们不可能说用画笔去绘画一个背景之类的,很不现实,所以我们可以加入图片,使用外界的图片。
第一步:放入图片
我们从网上找一堆图片,将图片放在同一个文件夹里面,将文件夹复制到项目下面去,如图:
在这里插入图片描述

第二步:导入图片
创建一个图片变量(全局变量),把图片赋值给图片变量,这一步是在面板的构造方法里面,然后在paint函数里面绘制背景图片
第三步:处理异常
异常表示有风险,有两种解决方式,一种是逐级上报,另一种则是制作日志,这里选择第二种。
把鼠标放在ImageIO,选择try/catch
具体代码

class BOKPanel extends JPanel{
		//背景图片变量
		Image background;//定义图片变量 background
		public BOKPanel() {
			// 读取图片文件赋值给background变量
			try {//尝试
				background = ImageIO.read(new File("Image/login.jpg"));
				//文件在Image文件夹
			} catch (IOException e) {//捕获,万一出错,
				// TODO Auto-generated catch block
				e.printStackTrace();//把异常日志信息打印出来
			}
		}
		//绘制方法
		@Override
		public void paint(Graphics g) {
			super.paint(g);
			//绘制背景图片
			g.drawImage(background, 0, 0,1199,660,null);
		}
	}	
	

//记得导包

JLabel

JLabel的作用就是创建文本,图片,图文…使用JLabel可以在背景图片上面增加文字或图片。
先声明一个对象全局变量,然后调用JLabel构造方法,设定绝对布局
代码如下:

JLabel user;
public bok_1() {
	user = new JLabel("使用者");
	user.setBounds(600, 300, 40, 30);
	this.add(user);
	
	BOKPanel panel = new BOKPanel();
	this.add(panel);
	
	this.setSize(1198, 600);
	this.setLocationRelativeTo(null);
	this.setUndecorated(true);
	this.setDefaultCloseOperation(3);//这里3就是JFrame.EXIT_ON_CLOSE
	//logo
	this.setIconImage(new ImageIcon("image/115.png").getImage());
	this.setVisible(true);	
}

//记得导包

JTextField和JPasswordField

JTextField是文本输入框,JPasswordField是密码输入框(输入进去的是看不见的),这两个的具体操作步骤 如JLabel;
代码如下:

	JLabel user;
	JTextField text;
	JPasswordField password;
public bok_1() {
	user = new JLabel("使用者");
	user.setBounds(100, 300, 40, 30);
	this.add(user);
	
	text = new JTextField();
	text.setBounds(670, 300, 100, 30);
	this.add(text);
	
	password = new JPasswordField();
	password.setBounds(670, 350, 100, 30);
	this.add(password);
		
	
	BOKPanel panel = new BOKPanel();
	this.add(panel);
	
	this.setSize(1198, 600);
	this.setLocationRelativeTo(null);
	this.setUndecorated(true);
	this.setDefaultCloseOperation(3);//这里3就是JFrame.EXIT_ON_CLOSE
	//logo
	this.setIconImage(new ImageIcon("image/115.png").getImage());
	this.setVisible(true);
}

//记得导包

JButton 按钮

实际操作和上面JLabel,JtextField,JPasswordText一样,但按钮毕竟是按钮,按钮可以增加一些功能,添加判断语句之类的,很好用。

下面是具体代码:

	JLabel user;
	JTextField text;
	JPasswordField password;
	JButton button;
public bok_1() {
	user = new JLabel("使用者");
	user.setBounds(100, 300, 40, 30);
	this.add(user);
	
	text = new JTextField();
	text.setBounds(670, 300, 100, 30);
	this.add(text);
	
	password = new JPasswordField();
	password.setBounds(670, 350, 100, 30);
	this.add(password);
	
	button = new JButton("按钮");
	button.setBounds(100, 400, 100, 100);
	button.addActionListener(new ActionListener() {
		//按钮监听
		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			String userName = text.getText();
			if("L777".equals(userName)){
				System.out.println("nice");
				JOptionPane.showMessageDialog(null,"nice");
			}
		}
	});
	
	this.add(button);
	
	
	BOKPanel panel = new BOKPanel();
	this.add(panel);
	
	this.setSize(1198, 600);
	this.setLocationRelativeTo(null);
	this.setUndecorated(true);
	this.setDefaultCloseOperation(3);//这里3就是JFrame.EXIT_ON_CLOSE
	//logo
	this.setIconImage(new ImageIcon("image/115.png").getImage());
	this.setVisible(true);
}

全部代码

package bok;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class bok_1 extends JFrame {
	JLabel user;
	JTextField text;
	JPasswordField password;
	JButton button;
public bok_1() {
	user = new JLabel("使用者");
	user.setBounds(100, 300, 40, 30);
	this.add(user);
	
	text = new JTextField();
	text.setBounds(670, 300, 100, 30);
	this.add(text);
	
	password = new JPasswordField();
	password.setBounds(670, 350, 100, 30);
	this.add(password);
	
	button = new JButton("按钮");
	button.setBounds(100, 400, 100, 100);
	button.addActionListener(new ActionListener() {
		//按钮监听
		@Override
		public void actionPerformed(ActionEvent e) {
			// 获取文本内容
			String userName = text.getText();
			if("L777".equals(userName)){
				System.out.println("nice");
				JOptionPane.showMessageDialog(null,"nice");
			}
		}
	});
	
	this.add(button);
	BOKPanel panel = new BOKPanel();
	this.add(panel);
	
	this.setSize(1198, 600);
	this.setLocationRelativeTo(null);
	this.setUndecorated(true);
	this.setDefaultCloseOperation(3);//这里3就是JFrame.EXIT_ON_CLOSE
	//logo
	this.setIconImage(new ImageIcon("image/115.png").getImage());
	this.setVisible(true);	
}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new bok_1();
	}

	class BOKPanel extends JPanel{
		//背景图片变量
		Image background;//定义图片变量 background
		public BOKPanel() {
			// 读取图片文件赋值给background变量
			try {//尝试
				background = ImageIO.read(new File("Image/login.jpg"));
				//文件在Image文件夹
			} catch (IOException e) {//捕获,万一出错,
				// TODO Auto-generated catch block
				e.printStackTrace();//把异常日志信息打印出来
			}
		}
		//绘制方法
		@Override
		public void paint(Graphics g) {
			super.paint(g);
			//绘制背景图片
			g.drawImage(background, 0, 0,1199,660,null);
		}
	}		
}

(个人学习总结,如有错误,敬请斧正,学习时间,20,11)

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值