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

📄 mygui.java

📁 实验目的 1.培养学生综合掌握软件开发过程的能力。 2.培养学生综合运用面向对象设计方法的能力
💻 JAVA
字号:
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class myGui extends JFrame implements ActionListener
{
	File afile =new File("myGui.class");
	private Container c;
	private JMenu fileMenu1=new JMenu("文件");
	private JMenu fileMenu2=new JMenu("查看");
	private JMenu fileMenu3=new JMenu("帮助");
	private JMenuItem openFile1=new JMenuItem("读入字典文件");
	private JMenuItem openFile2=new JMenuItem("读检测文件");
	private JMenuItem check=new JMenuItem("拼写检查");
	private JMenuItem saveFile=new JMenuItem("保存结果文件");
	private JMenuItem exitFile=new JMenuItem("退出");
	private JMenuItem helpFile1=new JMenuItem("自动换行");
	private JMenuItem helpFile2=new JMenuItem("取消自动换行");
	private JMenuItem aboutFile=new JMenuItem("关于");
	private JMenuItem helpFile3=new JMenuItem("帮助");
	
	private JButton button1=new JButton("读入字典");
	private JButton button2=new JButton("读检测文件");
	private JButton button3=new JButton("拼写检查");
	private JButton button4=new JButton("保存检查结果");
	
	private JLabel label1=new JLabel("提示:首先请输入词典文件");
	private JLabel label2=new JLabel("待检查文件");
	private JLabel label3=new JLabel("请输入存放结果文件");
	private JLabel label4=new JLabel("检查结果");
	private JPanel panel1;
	private JPanel panel2;
	private JPanel panel3;
	private JPanel panel4;
	private JPanel panel5;
	private JScrollPane b1;
	private JScrollPane b2;
	
	private File f1=new File("Dictionary.txt");
	private File f2=new File("输入文件.txt");
	private File f3=new File("结果.txt");
	private JTextArea t1=new JTextArea("",24,40);
	private JTextArea t2=new JTextArea("",24,20);
	
	private String s=new String();
	private myDictionary adic =new myDictionary();
	private myText atext = new myText();
	private Checker acheck =new Checker();
	private output aout=new output();
	
	public  myGui()
	{
		super("小鸟的拼写检查器");
		
		ActionListener listener=new inclass();
		button1.addActionListener(listener);
		button2.addActionListener(listener);
		button3.addActionListener(listener);
		button4.addActionListener(listener);
		
		c=getContentPane();
		JMenuBar menuBar=new JMenuBar();
		setJMenuBar(menuBar);
		
		fileMenu1.add(openFile1);
		fileMenu1.addSeparator();
		fileMenu1.add(openFile2);
		fileMenu1.add(check);
		fileMenu1.add(saveFile);
		fileMenu1.addSeparator();
		fileMenu1.add(exitFile);
		fileMenu2.add(helpFile1);
		fileMenu2.add(helpFile2);
		
		fileMenu3.add(helpFile3);
		fileMenu3.addSeparator();
		fileMenu3.add(aboutFile);
				
		button2.setEnabled(false);
		button3.setEnabled(false);
		button4.setEnabled(false);
		openFile2.setEnabled(false);
		check.setEnabled(false);
		saveFile.setEnabled(false);
		helpFile1.setEnabled(false);
		menuBar.add(fileMenu1);
		menuBar.add(fileMenu2);
		menuBar.add(fileMenu3);
		openFile1.addActionListener(this);
		openFile2.addActionListener(this);
		saveFile.addActionListener(this);
		check.addActionListener(this);
		exitFile.addActionListener(this);
		helpFile1.addActionListener(this);
		helpFile2.addActionListener(this);
		aboutFile.addActionListener(this);
		helpFile3.addActionListener(this);

		
		t1.setLineWrap(true);
		t2.setLineWrap(true);
		b1=new JScrollPane(t1);
		b2=new JScrollPane(t2);
			
		label1.setFont(new Font("Serif",Font.BOLD,24));
		label2.setFont(new Font("Serif",Font.BOLD,24));
		label3.setFont(new Font("Serif",Font.BOLD,24));
		label4.setFont(new Font("Serif",Font.BOLD,24));
		
		
		panel1=new JPanel();
		panel2=new JPanel();
		panel3=new JPanel();
		panel4=new JPanel();
		panel5=new JPanel();
		
		c.add(panel1,BorderLayout.CENTER);
		c.add(label1,BorderLayout.SOUTH);
		panel1.setLayout(new BorderLayout());
		panel1.add(panel2,BorderLayout.CENTER);
		panel1.add(panel5,BorderLayout.SOUTH);
		
		panel2.setLayout(new BorderLayout());
		panel2.add(panel3,BorderLayout.CENTER);
		panel2.add(panel4,BorderLayout.EAST);
		
		panel3.setLayout(new BorderLayout());
		panel3.add(label2,BorderLayout.NORTH);
		panel3.add(b1,BorderLayout.CENTER);
		
		panel4.setLayout(new BorderLayout());
		panel4.add(label4,BorderLayout.NORTH);
		panel4.add(b2,BorderLayout.CENTER);
		
		panel5.setLayout(new GridLayout(1,4,10,10));
		panel5.add(button1);
		panel5.add(button2);
		panel5.add(button3);
		panel5.add(button4);
		
		setSize(800,600);
		setVisible(true);
	}
	
	private void getdic() throws IOException //打开字典
	{
		JFileChooser fc=new JFileChooser();
		fc.setCurrentDirectory(afile);
		int r1=fc.showOpenDialog(this);
		if(r1==JFileChooser.APPROVE_OPTION)
		{
			String s1;
			s1=fc.getSelectedFile().getAbsolutePath();
			f1=new File(s1);
			while(!f1.exists())
			{
				JOptionPane.showMessageDialog(this,"词典文件不存在","错误",1);
				r1=fc.showOpenDialog(this);
				if(r1==JFileChooser.CANCEL_OPTION) break;
				s1=fc.getSelectedFile().getAbsolutePath();
				f1=new File(s1);
			}
			label1.setText("词典文件:  "+f1.getName());
			adic.OpenDictionary(f1);
			openFile2.setEnabled(true);
			saveFile.setEnabled(true);
			button2.setEnabled(true);
			button3.setEnabled(true);
		}
		if(r1==JFileChooser.CANCEL_OPTION)
		JOptionPane.showMessageDialog(this,"没有选择词典文件","警告",1);
	}
	
	private void opentext() throws IOException //打开检查文件
	{
		JFileChooser fc=new JFileChooser();
		fc.setCurrentDirectory(afile);
		int r1=fc.showOpenDialog(this);
		if(r1==JFileChooser.APPROVE_OPTION)
		{
			String s1;
			s1=fc.getSelectedFile().getAbsolutePath();
			f2=new File(s1);
			while(!f1.exists())
			{
				JOptionPane.showMessageDialog(this,"需要查找文件不存在","错误",1);
				r1=fc.showOpenDialog(this);
				if(r1==JFileChooser.CANCEL_OPTION) break;
				s1=fc.getSelectedFile().getAbsolutePath();
				f2=new File(s1);
			}
			label1.setText("需要查找的文件:  "+f2.getName());
			s=atext.OpenText(f2);
			check.setEnabled(true);
			button4.setEnabled(true);
			t1.setText(s);
		}
			if(r1==JFileChooser.CANCEL_OPTION)
				JOptionPane.showMessageDialog(this,"没有选择需要查找的文件","警告",1);	
	}
	
	private void saveerr() throws IOException
	{
		JFileChooser fc=new JFileChooser();
		fc.setCurrentDirectory(afile);
		int r2=fc.showSaveDialog(this);
		if(r2==JFileChooser.APPROVE_OPTION)
		{
			String s2;
			s2=fc.getSelectedFile().getAbsolutePath();
			f3=new File(s2);
			label1.setText("存放查找结果的文件:  "+f3.getName());
		}
		aout.outprint(acheck.checkLine,f3);
		JOptionPane.showMessageDialog(this,label1.getText(),"成功",1);

	}
	
	
	public void actionPerformed(ActionEvent e)
	{

		JMenuItem menuItem=(JMenuItem) e.getSource();
		try
		{
			if(menuItem.getText()=="读入字典文件")  getdic();
			if(menuItem.getText()=="读检测文件") 	opentext();
		
			if(menuItem.getText()=="拼写检查")
			{	
				acheck.check(adic,s);
				t2.setText(acheck.checkLine);
				label1.setText("检查完成!!");
				JOptionPane.showMessageDialog(this,"检查完成!","成功",1);
			}
			
			if(menuItem.getText()=="保存结果文件") saveerr();
			if(menuItem.getText()=="退出")
			System.exit(1);
			if(menuItem.getText()=="取消自动换行")
			{
				t1.setLineWrap(false);
				t2.setLineWrap(false);
				helpFile1.setEnabled(true);
				helpFile2.setEnabled(false);
			}
			if(menuItem.getText()=="自动换行")
			{
				t1.setLineWrap(true);
				t2.setLineWrap(true);
				helpFile1.setEnabled(false);
				helpFile2.setEnabled(true);
			}
			if(menuItem.getText()=="关于")
				JOptionPane.showMessageDialog(this,"司徒志远:2001374126\n张微:      2001374130\n黄洁茜:   2001374110","制作组成员",1);
			if(menuItem.getText()=="帮助")
				JOptionPane.showMessageDialog(this,"首先载入字典文件\n然后载入要检查的纯英文的文件\n检查后可以将结果保存致文件","帮助文件",1);
		}
			
		catch(Exception ee)
		{
			JOptionPane.showMessageDialog(this,"操作错误!!!","错误",1);
			label1.setText("操作错误!!!");		
		}
	
	}
	
	class inclass implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			JButton button=(JButton) e.getSource();
		try
		{
			if(button.getText()=="读入字典")  getdic();
			if(button.getText()=="读检测文件") 	opentext();
		
			if(button.getText()=="拼写检查")
			{	
				acheck.check(adic,s);
				t2.setText(acheck.checkLine);
				label1.setText("检查完成!!");
				//JOptionPane.showMessageDialog(this,"检查完成!","成功",1);
			}
			if(button.getText()=="保存检查结果") saveerr();
		}
			
		catch(Exception ee)
		{
			//JOptionPane.showMessageDialog(this,"操作错误!!!","错误",1);
			label1.setText("操作错误!!!");		
		}
	
		}
	}
	
	public static void main(String args[])
	{
		myGui gui=new myGui();
		gui.addWindowListener(new WindowAdapter() {            
    		public void windowClosing(WindowEvent e) {                    
    			System.exit(0);            
    		}        
    	});
	}
}

⌨️ 快捷键说明

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