⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frmmain.java

📁 该压缩文件中共包含16个非常实用的java学习实例
💻 JAVA
字号:
import java.applet.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.io.*;public class FrmMain extends Frame implements  ActionListener{	private Canvas c1;	private Canvas c2;		private JButton b1;	private JButton b2;	private JButton b3;		private JPanel p;	private JPanel p1;		private Graphics g1;	private Graphics g2;		private Image m1;	private Image m2;		private FileInputStream f1=null;	private FileInputStream f2=null;		private String filepath1=null;	private String filepath2=null;		private byte bit1[]=new byte[1];	private byte bit2[]=new byte[1];				public  FrmMain()	{		c1=new Canvas();		c2=new Canvas();		c1.setSize(180,220);		c2.setSize(180,220);		FlowLayout fl=new FlowLayout(1,10,10);		p1=new JPanel(fl);				p1.add(c1);		p1.add(c2);				b1=new JButton("选择第一幅图片");		b2=new JButton("选择第二幅图片");		b3=new JButton("开始比较");		b1.setFont(new Font("DialogInput", 0, 12));		b2.setFont(new Font("DialogInput", 0, 12));		b3.setFont(new Font("DialogInput", 0, 12));		p=new JPanel();		this.setLayout(new BorderLayout());		this.add("Center",p1);		this.add("South",p);				p.add(b1);		p.add(b2);		p.add(b3);		b1.addActionListener(this);		b2.addActionListener(this);		b3.addActionListener(this);				//m1=Toolkit.getDefaultToolkit().getImage("样品.jpg");				this.setTitle("图片比较");		this.setSize(400,300);		this.setResizable(false);		Dimension d=Toolkit.getDefaultToolkit().getScreenSize();		Dimension f=this.getSize();		this.setLocation(d.width/2-f.width/2,d.height/2-f.height/2);		this.show();		this.addWindowListener(new WindowAdapter()		{			public void windowClosing(WindowEvent evt)			{				System.exit(0);			}		});				this.init();	}	public void init()	{		g1=c1.getGraphics();		g2=c2.getGraphics();				g1.draw3DRect(0,0,c1.getWidth()-1,c1.getHeight()-1,true);		g2.draw3DRect(0,0,c2.getWidth()-1,c2.getHeight()-1,true);	}	public static void main(String[] args)	{		FrmMain app=new FrmMain();	}	public void actionPerformed(ActionEvent e) 	{		if(e.getActionCommand().equals("选择第一幅图片"))		{			FileDialog fdlg=new FileDialog(new Frame(),"选择第一幅图片",FileDialog.LOAD);			fdlg.setVisible(true);			filepath1=fdlg.getDirectory()+fdlg.getFile();			m1=Toolkit.getDefaultToolkit().getImage(filepath1);			g1.drawImage(m1,0,0,c1.getWidth()-1,c1.getHeight()-1,Color.BLUE,this);		}		else if(e.getActionCommand().equals("选择第二幅图片"))		{			FileDialog fdlg=new FileDialog(new Frame(),"选择第一幅图片",FileDialog.LOAD);			fdlg.setVisible(true);			filepath2=fdlg.getDirectory()+fdlg.getFile();			m2=Toolkit.getDefaultToolkit().getImage(filepath2);			g2.drawImage(m2,0,0,c2.getWidth(),c2.getHeight(),Color.BLUE,this);					}		else		{			//System.out.println(filepath1);			if(filepath1==null||filepath2==null)				{					JOptionPane.showMessageDialog(null,"请选择2张要比较的图片","错误",JOptionPane.ERROR_MESSAGE);					return;				}			if(filepath1.equals("")||filepath2.equals(""))				{					JOptionPane.showMessageDialog(null,"请选择2张要比较的图片","错误",JOptionPane.ERROR_MESSAGE);					return;				}			try			{				f1=new FileInputStream(filepath1);				f2=new FileInputStream(filepath2);				int i1,i2;				String str="";				while(true)				{					i1=f1.read(bit1);					i2=f2.read(bit2);					if(i1!=i2)					{						str="图片大小不相等!!!";						break;					}					if(bit1[0]!=bit2[0])					{						str="图片内容不相等!!!";						break;					}										if(i1==-1||i2==-1)					{						str="此2张图片完全相等!!!";						break;					}				}				JOptionPane.showMessageDialog(null,str,"结果显示",JOptionPane.INFORMATION_MESSAGE);				return;			}			catch(Exception e1)			{				System.out.println(e1.getMessage());			}					}	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -