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

📄 form.java

📁 一个用Java Swing写的简单单词拼写检查序
💻 JAVA
字号:
import java.io.*;
import java.awt.*;
import javax.swing.JOptionPane;
import java.awt.event.*;

public class Form implements ActionListener
{
	String filename;
	
	Frame f;
	Label label1;
	Label label2;
	Label label3;
	TextField file;
	TextArea ta;
	Button b1,b2,b3;
	FileDialog fd;
	
	MenuBar mb;
	Menu mf,mh;
	
	Panel panel1=new Panel(new FlowLayout(FlowLayout.CENTER));
	Panel panel2=new Panel(new FlowLayout(FlowLayout.LEFT));
	Panel panel3=new Panel(new FlowLayout(FlowLayout.CENTER));
	Panel panel4=new Panel(new BorderLayout());
	Panel panel5=new Panel(new FlowLayout(FlowLayout.CENTER));
	
	Checker acheck =new Checker();
	
	public void display()
	{
		f=new Frame("单词检查程序");
		f.setBackground(Color.LIGHT_GRAY);
		f.setLocation(400,200);
		addMenu();
		label1=new Label("快速拼写检查程序");
		label1.setSize(50,50);
		panel1.add(label1);
	
		file=new TextField(30);
		b1=new Button("打开文件");
		b1.addActionListener(this);
		panel2.add(file);
		panel2.add(b1);
		
		ta=new TextArea("",15,40);
		panel3.add(ta);	
		
		panel4.add(panel2,BorderLayout.NORTH);
		panel4.add(panel3,BorderLayout.CENTER);
		
		b2=new Button("检测");
		b2.addActionListener(this);
		b3=new Button("退出");
		b3.addActionListener(this);
		panel5.add(b2);
		panel5.add(b3);
	
		f.add(panel1,BorderLayout.NORTH);
		f.add(panel4,BorderLayout.CENTER);
		f.add(panel5,BorderLayout.SOUTH);
		f.addWindowListener(new WinClose());
		f.pack();
		f.setVisible(true);
}
	public void addMenu()
	{
		mb=new MenuBar();
		f.setMenuBar(mb);
		mf=new Menu("文件");
		mh=new Menu("帮助");
		mb.add(mf);
		mb.add(mh);
		mf.add(new MenuItem("打开"));
		mf.add(new MenuItem("检测"));
		mf.add(new MenuItem("退出"));
		mf.addActionListener(this);
		mh.add(new MenuItem("帮助"));
		mh.add(new MenuItem("关于"));
		mh.addActionListener(this);
	}
	public void openFile()
	{
		fd=new FileDialog(f,"打开");
		fd.setVisible(true);
		filename=fd.getDirectory()+fd.getFile();
		file.setText(filename);
	}

	public void outputText() throws IOException
	{
		
		File result=new File("result.txt");
		FileReader fr=new FileReader(result);
		BufferedReader br=new BufferedReader(fr);
		String aline;
		while((aline=br.readLine())!=null)
			ta.append(aline+"\n");
		fr.close();
		br.close();
	}
	public void actionPerformed(ActionEvent e)
	{
		try{
		if(e.getActionCommand()=="打开")
			openFile();
		else if(e.getActionCommand()=="检测")
		{
				acheck.check(filename);
				outputText();
		}
		else if(e.getActionCommand()=="退出")
			System.exit(0);
		else if(e.getActionCommand()=="帮助")
			JOptionPane.showMessageDialog(f,"打开待检测单词文件\n按检测按钮开始检查","帮助文件",JOptionPane.INFORMATION_MESSAGE); 
		else if(e.getActionCommand()=="关于")
			JOptionPane.showMessageDialog(f,"组员:\n官立才:200530690507(程序编写)\n蔡跃生:200530690502","关于",JOptionPane.INFORMATION_MESSAGE);
		else if(e.getSource()==b1)
		{
				openFile();
		}
		else if(e.getSource()==b2)
		{
				acheck.check(filename);
				outputText();
		}
		else
			System.exit(0);
		}
		catch(Exception e1)
		{	
			JOptionPane.showMessageDialog(f,"没有选择文件!","错误",JOptionPane.ERROR_MESSAGE); 
		}
	}
	class WinClose extends WindowAdapter
	{
		public void windowClosing(WindowEvent e)
		{
			System.exit(0);
		}
	}
	public static void main(String[] args)
	{
		(new Form()).display();
	}
}

⌨️ 快捷键说明

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