步骤 1 : GUIUtil 步骤 2 : checkEmpty 步骤 3 : checkNumber 步骤 4 : checkZero 步骤 5 : setColor 步骤 6 : setImageIcon 步骤 7 : useLNF 步骤 8 : showPanel 
					工具类的名字,往往都会以Util结尾,Util是Utility的缩写,Utility是工具的意思。 
					
				GUIUtil就表示专门用在图形界面上的工具类 下面是整个类的代码,后续会把每个方法拿出来讲解 package util;
import java.awt.Color;
import java.awt.Dimension;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class GUIUtil {
	private static String imageFolder = "e:/project/hutubill/img";
	public static void setImageIcon(JButton b, String fileName, String tip) {
		ImageIcon i = new ImageIcon(new File(imageFolder, fileName).getAbsolutePath());
		b.setIcon(i);
		b.setPreferredSize(new Dimension(61, 81));
		b.setToolTipText(tip);
		b.setVerticalTextPosition(JButton.BOTTOM);
		b.setHorizontalTextPosition(JButton.CENTER);
		b.setText(tip);
	}
	public static void setColor(Color color, JComponent... cs) {
		for (JComponent c : cs) {
			c.setForeground(color);
		}
	}
	/**
	 * 
	 * @param p
	 * @param strechRate 拉伸比例1表示满屏幕
	 */
	public static void showPanel(JPanel p,double strechRate) {
		GUIUtil.useLNF();
		JFrame f = new JFrame();
		f.setSize(500, 500);
		f.setLocationRelativeTo(null);
		CenterPanel cp = new CenterPanel(strechRate);
		f.setContentPane(cp);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setVisible(true);
		cp.show(p);
	}
	
	public static void showPanel(JPanel p) {
		showPanel(p,0.85);
	}	
	public static boolean checkNumber(JTextField tf, String input) {
		if (!checkEmpty(tf, input))
			return false;
		String text = tf.getText().trim();
		try {
			Integer.parseInt(text);
			return true;
		} catch (NumberFormatException e1) {
			JOptionPane.showMessageDialog(null, input + " 需要是整数");
			tf.grabFocus();
			return false;
		}
	}
	public static boolean checkZero(JTextField tf, String input) {
		if (!checkNumber(tf, input))
			return false;
		String text = tf.getText().trim();
		if (0 == Integer.parseInt(text)) {
			JOptionPane.showMessageDialog(null, input + " 不能为零");
			tf.grabFocus();
			return false;
		}
		return true;
	}
	public static boolean checkEmpty(JTextField tf, String input) {
		String text = tf.getText().trim();
		if (0 == text.length()) {
			JOptionPane.showMessageDialog(null, input + " 不能为空");
			tf.grabFocus();
			return false;
		}
		return true;
	}
	public static int getInt(JTextField tf) {
		return Integer.parseInt(tf.getText());
	}
	public static void useLNF() {
		try {
			javax.swing.UIManager.setLookAndFeel("com.birosoft.liquid.LiquidLookAndFeel");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
 
								
										
									
								
							
				
					
						增值内容,请先登录
					
				
				 
		
					
		
		
					
					完整的J2SE桌面项目,从无到有完整的开发流程,涵盖全部52个知识点,154个开发步骤, 一共36个讲解视频,累计时长3小时10分59秒,大小1.94G,充实J2SE项目经验,为简历加上一个有吸引力的砝码
					 
			增值内容,点击购买 
					使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
					 
					
					public static boolean checkEmpty(JTextField tf, String input) {
		String text = tf.getText().trim();
		if (0 == text.length()) {
			JOptionPane.showMessageDialog(null, input + " 不能为空");
			tf.grabFocus();
			return false;
		}
		return true;
	}
 
									
									public static boolean checkEmpty(JTextField tf, String input) {
		String text = tf.getText().trim();
		if (0 == text.length()) {
			JOptionPane.showMessageDialog(null, input + " 不能为空");
			tf.grabFocus();
			return false;
		}
		return true;
	}
								
								
				
					
						增值内容,请先登录
					
				
				 
		
					
		
		
					
					完整的J2SE桌面项目,从无到有完整的开发流程,涵盖全部52个知识点,154个开发步骤, 一共36个讲解视频,累计时长3小时10分59秒,大小1.94G,充实J2SE项目经验,为简历加上一个有吸引力的砝码
					 
			增值内容,点击购买 
					使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
					 
					
					public static boolean checkNumber(JTextField tf, String input) {
		if (!checkEmpty(tf, input))
			return false;
		String text = tf.getText().trim();
		try {
			Integer.parseInt(text);
			return true;
		} catch (NumberFormatException e1) {
			JOptionPane.showMessageDialog(null, input + " 需要是整数");
			tf.grabFocus();
			return false;
		}
	}
 
									
									public static boolean checkNumber(JTextField tf, String input) {
		if (!checkEmpty(tf, input))
			return false;
		String text = tf.getText().trim();
		try {
			Integer.parseInt(text);
			return true;
		} catch (NumberFormatException e1) {
			JOptionPane.showMessageDialog(null, input + " 需要是整数");
			tf.grabFocus();
			return false;
		}
	}
								
								
				
					
						增值内容,请先登录
					
				
				 
		
					
		
		
					
					完整的J2SE桌面项目,从无到有完整的开发流程,涵盖全部52个知识点,154个开发步骤, 一共36个讲解视频,累计时长3小时10分59秒,大小1.94G,充实J2SE项目经验,为简历加上一个有吸引力的砝码
					 
			增值内容,点击购买 
					使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
					 
					
					public static boolean checkZero(JTextField tf, String input) {
		if (!checkNumber(tf, input))
			return false;
		String text = tf.getText().trim();
		if(0==Integer.parseInt(text)){
			JOptionPane.showMessageDialog(null, input + " 不能为零");
			tf.grabFocus();
			return false;
		}
		return true;
	}
 
									
									public static boolean checkZero(JTextField tf, String input) {
		if (!checkNumber(tf, input))
			return false;
		String text = tf.getText().trim();
		if(0==Integer.parseInt(text)){
			JOptionPane.showMessageDialog(null, input + " 不能为零");
			tf.grabFocus();
			return false;
		}
		return true;
	}
								
								
				
					
						增值内容,请先登录
					
				
				 
		
					
		
		
					
					完整的J2SE桌面项目,从无到有完整的开发流程,涵盖全部52个知识点,154个开发步骤, 一共36个讲解视频,累计时长3小时10分59秒,大小1.94G,充实J2SE项目经验,为简历加上一个有吸引力的砝码
					 
			增值内容,点击购买 
					使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
					 
					
					public static void setColor(Color color, JComponent... cs) {
		for (JComponent c : cs) {
			c.setForeground(color);
		}
	}
 
									
									public static void setColor(Color color, JComponent... cs) {
		for (JComponent c : cs) {
			c.setForeground(color);
		}
	}
								
								
				
					
						增值内容,请先登录
					
				
				 
		
					
		
		
					
					完整的J2SE桌面项目,从无到有完整的开发流程,涵盖全部52个知识点,154个开发步骤, 一共36个讲解视频,累计时长3小时10分59秒,大小1.94G,充实J2SE项目经验,为简历加上一个有吸引力的砝码
					 
			增值内容,点击购买 
					使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
					 
					
					public static void setImageIcon(JButton b, String fileName, String tip) {
		ImageIcon i = new ImageIcon(new File(imageFolder, fileName).getAbsolutePath());
		b.setIcon(i);
		b.setPreferredSize(new Dimension(61, 81));
		b.setToolTipText(tip);
		b.setVerticalTextPosition(JButton.BOTTOM);
		b.setHorizontalTextPosition(JButton.CENTER);
		b.setText(tip);
	}
 
									
									public static void setImageIcon(JButton b, String fileName, String tip) {
		ImageIcon i = new ImageIcon(new File(imageFolder, fileName).getAbsolutePath());
		b.setIcon(i);
		b.setPreferredSize(new Dimension(61, 81));
		b.setToolTipText(tip);
		b.setVerticalTextPosition(JButton.BOTTOM);
		b.setHorizontalTextPosition(JButton.CENTER);
		b.setText(tip);
	}
								
								
				
					
						增值内容,请先登录
					
				
				 
		
					
		
		
					
					完整的J2SE桌面项目,从无到有完整的开发流程,涵盖全部52个知识点,154个开发步骤, 一共36个讲解视频,累计时长3小时10分59秒,大小1.94G,充实J2SE项目经验,为简历加上一个有吸引力的砝码
					 
			增值内容,点击购买 
					使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
					 
					
					public static void useLNF() {
		try {
			javax.swing.UIManager.setLookAndFeel("com.birosoft.liquid.LiquidLookAndFeel");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
 
									
									public static void useLNF() {
		try {
			javax.swing.UIManager.setLookAndFeel("com.birosoft.liquid.LiquidLookAndFeel");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
								
								
				
					
						增值内容,请先登录
					
				
				 
		
					
		
		
					
					完整的J2SE桌面项目,从无到有完整的开发流程,涵盖全部52个知识点,154个开发步骤, 一共36个讲解视频,累计时长3小时10分59秒,大小1.94G,充实J2SE项目经验,为简历加上一个有吸引力的砝码
					 
			增值内容,点击购买 
					使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
					 
					
				
	/**
	 * 
	 * @param p
	 * @param strech 拉伸比例1表示满屏幕
	 */
	public static void showPanel(JPanel p,double strech) {
		GUIUtil.useLNF();
		JFrame f = new JFrame();
		f.setSize(500, 500);
		f.setLocationRelativeTo(null);
		CenterPanel cp = new CenterPanel(strech);
		f.setContentPane(cp);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setVisible(true);
		cp.show(p);
	}
	public static void showPanel(JPanel p) {
		showPanel(p,0.85);
	}	
 
								
										
									
								
							
package test;
 
import javax.swing.JButton;
import javax.swing.JPanel;
 
import util.GUIUtil;
 
public class Test {
    public static void main(String[] args) {
    	GUIUtil.useLNF();
        JPanel p = new JPanel();
        p.add(new JButton("按钮1"));
        p.add(new JButton("按钮2"));
        GUIUtil.showPanel(p);
    }
}
 
								
										
									
								
							
				HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。
			 
			 
			
			
			
			
			
		
		
		
		 	问答区域     
		 	
				
		  
	 
	  		
	  
	  	2022-04-07
	  		
	  				
	  					 
	  
					
						运行报错,但可以出来图像 
					
					
						
							
						
											
							
					
					
					
	   
	  
	  
	  
 
		回答已经提交成功,正在审核。 请于 我的回答 处查看回答记录,谢谢	
	 
	  		
	  
	  	2021-08-02
	  		
	  				
	  					 
	  
					
						站长我想问下这个加了!之后不就反了嘛?不太理解这儿的意思 
					
					
						
							
						
											
							
					
					
					
	   
	  
	  
	  
 
		回答已经提交成功,正在审核。 请于 我的回答 处查看回答记录,谢谢	
	 
	  		
	  
	  	2021-07-06
	  		
	  				
	  					 
	  
					
						乱码了要怎么设置 
					
					
						
							
						
											
							
					
					
					
	   
	  
	  
	  	    
	    
	  
	  		
	  
	  	2021-05-20
	  		
	  				
	  					 
	  
					
						 这个new File(imageFolder, fileName),imageFolder什么意思 
					
					
						
							
						
											
							
					
					
					
	   
	  
	  
	  	    
	    
	  
	  		
	  
	  	2021-03-31
	  		
	  				
	  					 
	  
					
						public static void setColor(Color color, JComponent... cs) 中...怎么用呢 
					
					
						
							
						
											
							
					
					
					
	   
	  
	  
	  	    
	    
	  提问太多,页面渲染太慢,为了加快渲染速度,本页最多只显示几条提问。还有 28 条以前的提问,请 点击查看 
			
			提问之前请登陆
			
		 
		提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢	
	 
 | 
	|||||||||||||||||||||||