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

📄 bcreator.java

📁 java compiler project to implement LR parsing
💻 JAVA
字号:
/*******************************************************************************
 * BCreator. 
 * New language which is called BASCAL.
 * Date : 26/10/2006,Time : 2:41.
 ******************************************************************************/
import mk.ceit.Compiler;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.lang.*;
import java.io.*;


    /**************************************************************************/
public class BCreator extends JFrame implements ActionListener,ItemListener
{
    JTextArea code;
    JScrollPane sp_code;
    JTextArea error;
    JScrollPane sp_error;
    JButton NEW;
    JButton open;
    JButton save;
    JButton clear;
    JButton bcolor;
    JButton compile;
    JButton execute;
    JButton exit;
    JButton fcolor;
    JComboBox size;
    JComboBox type;
    int fontSize;
    String fontType;
    
    /**************************************************************************/
    public BCreator() 
    {
        BCreatorLayout customLayout = new BCreatorLayout();
        
        fontSize=18;
        fontType="Helvetica";
        
        getContentPane().setFont(new Font(fontType, Font.PLAIN, fontSize));
        
        
        getContentPane().setLayout(customLayout);
        getContentPane().setBackground(Color.black);

        code = new JTextArea("");
        sp_code = new JScrollPane(code);
        getContentPane().add(sp_code);

        error = new JTextArea("");
        sp_error = new JScrollPane(error);
        getContentPane().add(sp_error);
        
        
        NEW = new JButton("new");
        getContentPane().add(NEW);
        
        
        open =
        new JButton("open");
        getContentPane().add(open);
        
        
        save = new JButton("save");
        getContentPane().add(save);
        
        
        clear = new JButton("clear");
        getContentPane().add(clear);
        
        
        bcolor = new JButton("BC");
        getContentPane().add(bcolor);
        
        
        compile = new JButton("C>>");
        getContentPane().add(compile);
        
        
        execute = new JButton("E>>");
        getContentPane().add(execute);
        

        exit = new JButton("ESC");
        getContentPane().add(exit);

        fcolor = new JButton("FC");
        getContentPane().add(fcolor);

        size = new JComboBox();
        size.addItem("11");
        size.addItem("12");
        size.addItem("13");
        size.addItem("14");
        size.addItem("15");
        size.addItem("16");
        size.addItem("17");
        size.addItem("18");
        size.addItem("19");
        size.addItem("20");
        size.addItem("21");
        size.addItem("22");
        size.addItem("23");
        size.addItem("24");
        size.addItem("25");
        
        getContentPane().add(size);

        type = new JComboBox();
        type.addItem("Arial Black");
        type.addItem("Arial");
        type.addItem("Comic Sans MS");
        type.addItem("Franklin Gothic Medium");
        type.addItem("Georgia");
        type.addItem("Impact");
        type.addItem("Lucida Console");
        type.addItem("Cordia New");
        getContentPane().add(type);

        setSize(getPreferredSize());
        
        open.addActionListener(this);
    	save.addActionListener(this);
    	clear.addActionListener(this);
    	compile.addActionListener(this);
    	execute.addActionListener(this);
    	exit.addActionListener(this);
    	bcolor.addActionListener(this);
    	fcolor.addActionListener(this);
    	NEW.addActionListener(this);
    	size.addItemListener(this);
    	type.addItemListener(this);

        
        setColor(Color.yellow,Color.black,new Font("Arial",Font.BOLD,11));
        code.setFont(new Font("Arial", Font.BOLD, 18));
        error.setFont(new Font("Arial", Font.BOLD, 16));
        error.setEditable(false);
        
        /******************************************/

        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
    /**************************************************************************/
    public static void main(String args[]) {
        BCreator window = new BCreator();

        window.setTitle("BCreator");
        window.pack();
        window.show();
    }
    /**************************************************************************/
    public void actionPerformed(ActionEvent e)
    {
    	if(e.getSource()==compile)
    	{
    		error.setText("");
    		Compiler c=new Compiler(code.getText());
    		c.compile();
        	error.setText(c.getFinalMessage());
    	}
    	/******************************************/
    	if(e.getSource()==clear)
    	{
    		Clear();
    	}
    	/******************************************/
    	if(e.getSource()==open)
    	{
    		OpenFile();
    	}
    	/******************************************/
    	if(e.getSource()==save)
    	{
    		SaveFile();
    	}
    	/******************************************/
    	if(e.getSource()==exit)
    	{
    		System.exit(0);
    	}
    	/******************************************/
    	if(e.getSource()==NEW)
    	{
    		if(code.getText().length()!=0)
    		{
	    		if(JOptionPane.showConfirmDialog(null,"Would you want to save code")
	    		==JOptionPane.OK_OPTION)
	    		{
	    			SaveFile();
	    			Clear();
	    		}
	    		else
	    		Clear();
    		}
    		else
    		Clear();
    	}
    	/******************************************/
    	if(e.getSource()==bcolor)
    	{
    		Color color=Color.WHITE;
    		color=JColorChooser.showDialog(                    
                   BCreator.this, "Choose a color", color );
            
            if ( color == null )
            color = Color.WHITE;
            code.setBackground( color );
            error.setBackground( color );
    	}
    	/******************************************/
    	if(e.getSource()==fcolor)
    	{
    		Color color= Color.BLACK;
    		color=JColorChooser.showDialog(                    
                   BCreator.this, "Choose a color", color );
            
            if ( color == null )
            color = Color.BLACK;
            code.setForeground( color );
            error.setForeground( color );
    	}
    }
    /**************************************************************************/
    public void itemStateChanged(ItemEvent event )
    {
    	if(event.getSource()==size)
    	{
    		fontSize=Integer.parseInt((String)size.getSelectedItem());
    		fontProperties();
    	}
    	/******************************************/
    	if(event.getSource()==type)
    	{
    		fontType=(String)type.getSelectedItem();
    		fontProperties();
    	}
    	
    }
    /**************************************************************************/
    public void Clear()
    {
    	code.setText("");
    	error.setText("");	
    }
    /**************************************************************************/
    public void OpenFile()
    {
    	ObjectInputStream input;
    	JFileChooser fileChooser = new JFileChooser();
    	fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
    	int result = fileChooser.showOpenDialog( this );
    	if ( result != JFileChooser.CANCEL_OPTION )
    	{
    		File fileName = fileChooser.getSelectedFile();
	    	try 
	        {
	           input = new ObjectInputStream(new FileInputStream( fileName ) );
	           code.setText((String)input.readObject());
	           input.close();
	        } 
	        catch ( EOFException endOfFileException )
	        {
	           return; 
	        } 
	        catch ( ClassNotFoundException classNotFoundException )
	        {
	           JOptionPane.showMessageDialog(null, "Unable to create object.",
	            "Error Message", JOptionPane.ERROR_MESSAGE);
	        } 

	        catch ( IOException ioException )
	        {
	        	JOptionPane.showMessageDialog(null, "Error opening file.",
	            "Error Message", JOptionPane.ERROR_MESSAGE);
	        } 
    	}
    }
    /**************************************************************************/
    public void SaveFile()
    {
    	ObjectOutputStream output;
    	JFileChooser fileChooser = new JFileChooser();
    	fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
    	int result = fileChooser.showSaveDialog( this );
    	if ( result != JFileChooser.CANCEL_OPTION )
    	{
    		File fileName = fileChooser.getSelectedFile();
	    	try 
	        {
	           output = new ObjectOutputStream(new FileOutputStream( fileName ) );
	           output.writeObject( code.getText() ); 
	           output.close();
	        } 
	        catch ( IOException ioException )
	        {
	        	JOptionPane.showMessageDialog(null, "Error opening file.",
	            "Error Message", JOptionPane.ERROR_MESSAGE);
	        } 
    	}
    }
    /**************************************************************************/
    public void fontProperties()
    {
    	code.setFont(new Font(fontType, Font.PLAIN, fontSize));
    }
    /**************************************************************************/
    public void setColor(Color color1,Color color2,Font font)
    {
    	NEW.setForeground(color1);
    	open.setForeground(color1);
    	save.setForeground(color1);
    	clear.setForeground(color1);
    	compile.setForeground(color1);
    	execute.setForeground(color1);
    	exit.setForeground(color1);
    	bcolor.setForeground(color1);
    	fcolor.setForeground(color1);
    	size.setForeground(color1);
    	type.setForeground(color1);
    	
    	NEW.setBackground(color2);
    	open.setBackground(color2);
    	save.setBackground(color2);
    	clear.setBackground(color2);
    	compile.setBackground(color2);
    	execute.setBackground(color2);
    	exit.setBackground(color2);
    	bcolor.setBackground(color2);
    	fcolor.setBackground(color2);
    	size.setBackground(color2);
    	type.setBackground(color2);
    	
    	NEW.setFont(font);
    	open.setFont(font);
    	save.setFont(font);
    	clear.setFont(font);
    	compile.setFont(font);
    	execute.setFont(font);
    	exit.setFont(font);
    	bcolor.setFont(font);
    	fcolor.setFont(font);
    	size.setFont(font);
    	type.setFont(font);
    	
    }
    /**************************************************************************/
}

class BCreatorLayout implements LayoutManager {

    public BCreatorLayout() {
    }

    public void addLayoutComponent(String name, Component comp) {
    }

    public void removeLayoutComponent(Component comp) {
    }

    public Dimension preferredLayoutSize(Container parent) {
        Dimension dim = new Dimension(0, 0);

        Insets insets = parent.getInsets();
        dim.width = 903 + insets.left + insets.right;
        dim.height = 629 + insets.top + insets.bottom;

        return dim;
    }

    public Dimension minimumLayoutSize(Container parent) {
        Dimension dim = new Dimension(0, 0);
        return dim;
    }

    public void layoutContainer(Container parent) {
        Insets insets = parent.getInsets();

        Component c;
        c = parent.getComponent(0);
        if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+64,888,456);}
        c = parent.getComponent(1);
        if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+520,888,104);}
        c = parent.getComponent(2);
        if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+40,72,24);}
        c = parent.getComponent(3);
        if (c.isVisible()) {c.setBounds(insets.left+80,insets.top+40,72,24);}
        c = parent.getComponent(4);
        if (c.isVisible()) {c.setBounds(insets.left+152,insets.top+40,72,24);}
        c = parent.getComponent(5);
        if (c.isVisible()) {c.setBounds(insets.left+224,insets.top+40,72,24);}
        c = parent.getComponent(6);
        if (c.isVisible()) {c.setBounds(insets.left+296,insets.top+40,72,24);}
        c = parent.getComponent(7);
        if (c.isVisible()) {c.setBounds(insets.left+512,insets.top+40,72,24);}
        c = parent.getComponent(8);
        if (c.isVisible()) {c.setBounds(insets.left+584,insets.top+40,72,24);}
        c = parent.getComponent(9);
        if (c.isVisible()) {c.setBounds(insets.left+440,insets.top+40,72,24);}
        c = parent.getComponent(10);
        if (c.isVisible()) {c.setBounds(insets.left+368,insets.top+40,72,24);}
        c = parent.getComponent(11);
        if (c.isVisible()) {c.setBounds(insets.left+656,insets.top+40,72,24);}
        c = parent.getComponent(12);
        if (c.isVisible()) {c.setBounds(insets.left+728,insets.top+40,168,24);}
    }
}

⌨️ 快捷键说明

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