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

📄 mainwindow.java

📁 基于算符优先关心的
💻 JAVA
字号:
/**
 * @Copyright(C) 2008 Software Engineering Laboratory (SELAB), Department of Computer 
 * Science, SUN YAT-SEN UNIVERSITY. All rights reserved.
**/

package gui;

import parser.Calculator;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;

/**
 * Display the main window of ExprEval. 
 *
 * @author Dr. Wen-jun LI
 * @author Da LUO
 * @version 1.00 (Last update: March 3, 2008)
**/
public class MainWindow extends JFrame
{
	/**
	 * A constant for the length of the input field.  
	**/
	private final int INPUT_FIELD_LENGTH = 30;

	/**
	 * Input field. 
	**/
	private JTextField inputField;

	/**
	 * Output Field. 
	**/
	private JTextField outputField;

	/**
	 * The "Calculate" Button. 
	**/
	private JButton calButton;

	/**
	 * Parent frame of the help dialog window. 
	**/
	private JFrame currentFrame;


	/**
	 * Constructor of class MainWindow. 
	**/
	public MainWindow() {
		// Set the properties of the main frame
		this.setTitle("ExprEval - An Expression Based Calculator");
		this.getContentPane().setLayout(new BorderLayout());
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// Layout the input and output panel of the calculator
		JPanel ioPanel = new JPanel();
		this.getContentPane().add(ioPanel);
		ioPanel.setLayout(new GridLayout(2, 1, 10, 10));
		ioPanel.setBorder(BorderFactory.createEmptyBorder(8, 10, 8, 10));
		// 1. Add an input text field
		inputField = new JTextField(INPUT_FIELD_LENGTH);
		ioPanel.add(inputField);
		inputField.setBorder(BorderFactory.createLoweredBevelBorder());
		inputField.setFont(new Font("Arial", Font.BOLD, 24));
		inputField.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent ev) {
				if (ev.getKeyChar() == KeyEvent.VK_ENTER)  calButton.doClick();
			}
		});
		// 2. Add an output text field
		outputField = new JTextField(INPUT_FIELD_LENGTH);
		ioPanel.add(outputField);
		outputField.setFont(new Font("Arial", Font.BOLD, 24));
		outputField.setEnabled(false);

		// Layout all buttons in a tool bar, totally 6 buttons
		JToolBar toolBar = new JToolBar();
		this.getContentPane().add(toolBar, BorderLayout.SOUTH);
		toolBar.setLayout(new GridLayout(1, 7, 8, 8));
		toolBar.setBorder(BorderFactory.createEmptyBorder(8, 10, 8, 10));
		// 1. Add the "Calculate" button
		calButton = new JButton("Calculate");
		toolBar.add(calButton);
		calButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ev) {
				try {
					Calculator calculator = new Calculator();
					double result = calculator.calculate(inputField.getText());
					String str = (new DecimalFormat("###,###,###,###.####")).format(result);
					outputField.setText(str);
				} catch (Exception ex) {
					outputField.setText(ex.getMessage());
				}
			}
		});
		// 2. Add the "Clear" button
		JButton clearButton = new JButton("Clear");
		toolBar.add(clearButton);
		clearButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ev) {
				inputField.setText("");
				outputField.setText("");
			}
		});
		// 3. Add the "Cut" button
		JButton cutButton = new JButton("Cut");
		toolBar.add(cutButton);
		cutButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ev) {
				inputField.selectAll();
				inputField.cut();
			}
		});
		// 4. Add the "Copy" button
		JButton copyButton = new JButton("Copy");
		toolBar.add(copyButton);
		copyButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ev) {
				outputField.setEnabled(true);
				outputField.selectAll();
				outputField.copy();
				outputField.setEnabled(false);
			}
		});
		// 5. Add the "Paste" button
		JButton pasteButton = new JButton("Paste");
		toolBar.add(pasteButton);
		pasteButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ev) {
				inputField.setText("");
				inputField.paste();
			}
		});
		// 6. Add the "Help" button
		JButton helpButton = new JButton("Help");
		toolBar.add(helpButton);
		currentFrame = this;
		helpButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ev) {
				JOptionPane.showMessageDialog(currentFrame, getHelpMessage());
			}
		});
		// 7. Add the "Exit" button
		JButton exitButton = new JButton("Exit");
		toolBar.add(exitButton);
		exitButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ev) {
				System.exit(0);
			}
		});

		// Display the main frame
		this.pack();
		this.setVisible(true);
	}


	/**
	 * Return the help message, including copyright and version information. 
	 * 
	 * @return  help message. 
	**/
	private String getHelpMessage() {
		String msg = "";
		msg += "ExprEval - An Expression Based Calculator, Version 1.0A\n";
		msg += "Copyright(C) 2008, Dr. Wen-jun LI (SELAB), Department of Computer Science, SUN YAT-SEN UNIVERSITY. \n";
		msg += "The experimental framework and reference solution are developed by Dr. Wen-jun LI and Da LUO. \n\n";
		msg += "The fundamental version of ExprEval covers the following features: \n";
		msg += "  1) Unsigned integral constants, for example \"058\" and \"123\". \n";
		msg += "  2) Unsigned real constants, including scientific notation: \"3.14\", \"5.1E-5\", and \"101.25E+3\". \n";
		msg += "  3) Binary arithmetic operations, for example \"1 + 2\", \"3 - 4\", \"5 * 6\", and \"7 / 8\". \n";
		msg += "  4) Only one unary arithmetic operation, for example \"-100\". \n\n";
		msg += "This version of ExprEval also features in: \n";
		msg += "  a) Exponential operation, for example \"5 ^ 3\". \n";
		msg += "  b) Boolean constants: \"true\" and \"false\". \n";
		msg += "  c) Relational operations: \"=\", \"<>\", \"<\", \"<=\", \">\", and \">=\". \n";
		msg += "  d) Logical operations: \"!\" (not), \"&\" (and), and \"|\" (or). \n";
		msg += "  e) The only trinary operation, for example \"(12 * 2 > 20) ? 25 : 33\". \n";
		msg += "  f) Predefined functions \"max(x1, x2, ...)\" and \"min(x1, x2, ...)\". \n";
		msg += "  g) Predefined functions \"sin(x)\" and \"cos(x)\". \n";
		return msg;
	}
}

⌨️ 快捷键说明

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