Java Swing可视化界面开发

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_35956194/article/details/85035516

开发文档地址

踩过的坑:

JFrame设置背景色

          jf.setBackground(Color.blue);
          jf.getContentPane().setBackground(Color.red);//获取JFrame内容的根组件
          jf.getContentPane().setVisible(true);//如果改为true那么就变成了红色。
          jf.setVisible(true);
  • 设置JTextField的大小
          JTextField= new JTextField();
	      yTextField.setPreferredSize(new Dimension(50, 10));
  • 组件设置透明色
panel.setBackground(null);// 设置背景透明 
  • BoxLayout布局管理器的案例参考demo

       https://blog.csdn.net/sjf0115/article/details/7064909 

/**  
 * All rights Reserved, Designed By NARI
 * @Title:  Main.java   
 * @Package qrcodesoft   
 * @Description: TODO(用一句话描述该文件做什么)   
 * @author wuyongcheg     
 * @date   2018年12月12日 下午9:20:35   
 * @version V1.0 
 * @Copyright: 2018 www.naritech.cn All rights reserved. 
 */
package qrcodesoft;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.security.PrivateKey;
import java.security.PublicKey;

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;
import javax.swing.WindowConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.text.Utilities;

import qrcodesoft.utils.Base64Utils;
import qrcodesoft.utils.FileUtils;
import qrcodesoft.utils.JTextFieldHintListener;
import qrcodesoft.utils.QRCodeUtil;
import qrcodesoft.utils.RSAUtils;
import qrcodesoft.utils.StringUtils;

/**
 * @ClassName: Main
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author wuyongcheg
 * @date 2018年12月12日
 *
 */
public class Main {
	private static final String PLEASE_INPUT="请输入....";
	private static final String PATH_INPUT="例如:D:/文件夹名称";
	public static void main(String[] args) {
		
		// 1. 创建一个顶层容器(窗口)
		JFrame jf = new JFrame("二维码生成器"); // 创建窗口
		jf.setSize(700, 600); // 设置窗口大小
		jf.setLocationRelativeTo(null); // 把窗口位置设置到屏幕中心
		jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); // 当点击窗口的关闭按钮时退出程序(没有这一句,程序不会退出)
		GridBagLayout gridBag = new GridBagLayout(); // 布局管理器
		GridBagConstraints c = null; // 约束

		// 2. 创建中间容器(面板容器)
		JPanel panel = new JPanel(gridBag);

		// 创建文本标签
		JLabel label01 = new JLabel();
		label01.setText("请输入编码");
		label01.setFont(new Font("微软雅黑", Font.PLAIN, 18)); // 设置字体,null 表示使用默认字体
		label01.setForeground(Color.black);
		// c = new GridBagConstraints();
		Insets insets0 = new Insets(0, 0, 5, 0);
		c = new GridBagConstraints(-1, -1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, insets0,
				5, 5);
		gridBag.addLayoutComponent(label01, c);

		// 创建文本框,指定可见列数为8列,输入框的长度
		final JTextField textField01 = new JTextField(19);
		textField01.setFont(new Font(null, Font.PLAIN, 18));
		textField01.addFocusListener(new JTextFieldHintListener(PLEASE_INPUT, textField01));
		/*
		 * c = new GridBagConstraints(); c.gridwidth = GridBagConstraints.REMAINDER;
		 * c.fill = GridBagConstraints.BOTH; GridBagConstraints 中的5,5 代表输入框panding 值
		 */
		Insets insets1 = new Insets(0, 0, 10, 0);
		c = new GridBagConstraints(-1, -1, 0, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, insets1,
				5, 5);
		gridBag.addLayoutComponent(textField01, c);
		
		JButton jdelBtn=new JButton("清 除");
		jdelBtn.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		jdelBtn.setForeground(Color.blue);
		Insets insets4 = new Insets(0, 0, 10, 0);
		c = new GridBagConstraints(-1, -1, 0, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, insets4,
				0, 0);
		gridBag.addLayoutComponent(jdelBtn, c);
		
		
		jdelBtn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				textField01.setText("");
				
			}
		});
		
		
		// 创建文本标签
		JLabel label02 = new JLabel();
		label02.setText("请输入二维码存储路径");
		label02.setFont(new Font("微软雅黑", Font.PLAIN, 18)); // 设置字体,null 表示使用默认字体
		label02.setForeground(Color.black);
		// 组件彼此间的间距 Insets 上,左,下,右
		Insets insets = new Insets(0, 0, 5, 0);
		c = new GridBagConstraints(-1, -1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, insets,
				5, 5);

		gridBag.addLayoutComponent(label02, c);

		// 创建文本框,指定可见列数为8列
		final JTextField textField02 = new JTextField(15);
		textField02.addFocusListener(new JTextFieldHintListener(PATH_INPUT, textField02));
		textField02.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		c = new GridBagConstraints();
		c.gridwidth = GridBagConstraints.REMAINDER;
		c.fill = GridBagConstraints.BOTH;
		gridBag.addLayoutComponent(textField02, c);
		
		
		
		
		jdelBtn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				textField01.setText("");
				
			}
		});
		
		

		// 创建一个按钮,点击后获取文本框中的文本
		JButton btn = new JButton("生成二维码");
		btn.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		btn.setForeground(Color.blue);
		
		try {
			btn.setIcon(new ImageIcon(ImageIO.read(Main.class.getClassLoader().getResource("resources/images/inspection_icon.png"))));
		} catch (IOException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		
		// btn.setFont(new Font(null, Font.PLAIN, 20));
		btn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String text1 = textField01.getText().trim();
				String text2 = textField02.getText().trim(); 
				String newText2="";
				if (text2.contains("/")) {
					 newText2 = text2.replace("/","\\");
					 System.out.println("--------------"+newText2);
				}else {
					newText2=text2;
				}
				try {

					if (StringUtils.isBlank2(text1)||text1.equals(PLEASE_INPUT)) {
						JOptionPane.showMessageDialog(null, "编 码 不 能  为 空 !", "提示", JOptionPane.WARNING_MESSAGE);

						/*
						 * String priviteKey="rsakey/"+RSAUtils.PRIVITE_KEY; String path =
						 * Main.class.getClassLoader().getResource(priviteKey).getPath(); String string
						 * = FileUtils.getString(path);
						 */

					} else if (StringUtils.isBlank2(newText2)||newText2.equals(PATH_INPUT)) {

						/*String publicKey = "rsakey/" + RSAUtils.PUBLIC_KEY;
						String priviteKey = "rsakey/" + RSAUtils.PRIVITE_KEY;
						// String path = Main.class.getClassLoader().getResource(publicKey).getPath();
						String path1 = Main.class.getClassLoader().getResource(priviteKey).getPath();

						// encryption_Privite(FileUtils.getInputStream(path), text1);
						decode_public(FileUtils.getInputStream(path1),
								"SV0CKPf1hTjmnCTVslbEOMiFdJRUTsgICpjISBsIKiHF2nLKLCtgCLPG6kSpeYUEiVch2hp224ybG/tiBVGsWjVtBq/HGENrzNh+Jhwom62u5MvlOudTgE4zeqzD0H6sL5UaB2ZvQa3ARNJh43HkkEzhbepv3qTwyDhQzC7gS6s=");
*/
						JOptionPane.showMessageDialog(null, "存 储 路 径 不 能  为 空 !", "提示", JOptionPane.WARNING_MESSAGE);
					} else if (!StringUtils.isLegalPath(newText2)) {
						JOptionPane.showMessageDialog(null, "存 储 路 径  不 合 法 !", "提示", JOptionPane.WARNING_MESSAGE);
					} else if (!StringUtils.isBlank2(newText2) && !StringUtils.isDiskExist(newText2)) {

						JOptionPane.showMessageDialog(null, "存 储 路 径  不 存 在 !", "提示", JOptionPane.WARNING_MESSAGE);
					} else {
						QRCodeUtil.encode(text1, "", newText2, true);

					}

				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		/*
		 * c = new GridBagConstraints(); c.gridwidth = GridBagConstraints.REMAINDER;
		 * c.fill = GridBagConstraints.BOTH;
		 */
		// Insets 上,左,下,右
		Insets insets3 = new Insets(10, 0, 0, 0);
		c = new GridBagConstraints(-1, -1, 0, 0, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, insets3,
				5, 5);
		gridBag.addLayoutComponent(btn, c);
		panel.add(label01);
		panel.add(textField01);
		panel.add(label02);
		panel.add(textField02);
		panel.add(btn);
		panel.add(jdelBtn);

		// 4. 把 面板容器 作为窗口的内容面板 设置到 窗口
		jf.setContentPane(panel);

		// 5. 显示窗口,前面创建的信息都在内存中,通过 jf.setVisible(true) 把内存中的窗口显示在屏幕上。
		jf.setVisible(true);
	}

	/**
	 * 公钥加密
	 * 
	 * @param inPrivate
	 * @param plaintext
	 */
	public static void encryption_Privite(InputStream inPublic, String plaintext) {
		PrivateKey privateKey;
		try {
			PublicKey publicKey = RSAUtils.loadPublicKey(inPublic);
			// 加密
			byte[] encryptByte = RSAUtils.encryptData(plaintext.getBytes(), publicKey);
			// 为了方便观察吧加密后的数据用base64加密转一下,要不然看起来是乱码,所以解密是也是要用Base64先转换
			String afterencrypt = Base64Utils.encode(encryptByte);
			System.out.println("--------加密后的字符串-------" + afterencrypt);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * 私钥解密
	 * 
	 * @param inPrivate
	 * @param ciphertext
	 */
	public static void decode_public(InputStream inPrivate, String ciphertext) {
		PrivateKey privateKey;

		try {
			privateKey = RSAUtils.loadPrivateKey(inPrivate);
			// 因为RSA加密后的内容经Base64再加密转换了一下,所以先Base64解密回来再给RSA解密
			byte[] decryptByte = RSAUtils.decryptData(Base64Utils.decode(ciphertext), privateKey);
			String decryptStr = new String(decryptByte);
			System.out.println("--------解密后的字符串-------" + decryptStr);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}

运行效果:

  • 给JButton设置图片的方法 


jBtn.setIcon(
new ImageIcon(ImageIO.read(Main.class.getClassLoader().
getResource("resources/images/qr_code.png"))));
  •  如何给java工程设置图片

图片一定放在,工程的src目录下,不能放在工程的根目录下;

usage:获取工程资源文件下的图片:

ImageIO.read(Main.class.getClassLoader().getResource("resources/images/qr_code.png"))

   

  • 读取java工程下的文件 

      文件一定放在,工程的src目录下,不能放在工程的根目录下;

usage:       

 String publicKey = "rsakey/" + RSAUtils.PUBLIC_KEY;
        String path = Main.class.getClassLoader().getResource(publicKey).getPath();

如何使用这种方式:

String publicKey = "rsakey"+File.separator + RSAUtils.PUBLIC_KEY;

 String path = Main.class.getClassLoader().getResource(publicKey).getPath();

会导致路径乱码出现%号等字符串,遇见这种情况可以这样解决:

URLDecoder.decode(path, "UTF-8");//获取路径加入File.separator 乱码,进行解码就ok;

    

猜你喜欢

转载自blog.csdn.net/qq_35956194/article/details/85035516