import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
* 显示图形用户界面的主类。
* 功能:初始化好界面,设置好按钮的点击和键盘响应事件以取得输入。
* 获取输入的表达式后,将其传入Calc类中进行计算。
* 并通过Calc的getResult方法获取最终运算的结果。
* 再在结果文本框中显示出来。
*/
class GUI extends JFrame
{
//公共变量初始化
private JMenuBar menubar;//菜单栏
private JMenu menu_help;//帮助菜单项
private JMenuItem item_help, item_about;//菜单中的帮助文档项和关于计算器项
private JTextField express, result;//显示运算表达式和结果的文本框
private String [] buttonText = //各个按钮显示的内容
{ "0", ".", "=", "+", "C", "1", "2", "3", "-", "(",
"4", "5", "6", "*", ")", "7", "8", "9", "/", "退出" };
private JButton button[] = new JButton[20];
private boolean flag = false;//该变量用于指示何时进行文本框的清零操作
private Calc calc = new Calc();
//界面字体颜色的设置
Color green = new Color( 149, 216, 255 );
Font font = new Font( "宋体", Font.BOLD, 18 );
public GUI()
{
super( "科学计算器" );
//设置左上角显示的计算器图标
this.setIconImage( Toolkit.getDefaultToolkit().createImage( "1.jpg" ) );
this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
this.setResizable( false );//设置框体大小不可改变
this.setSize( 450, 370 );
//将窗口屏幕居中
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenHeight = screenSize.height;
int screenWidth = screenSize.width;
this.setLocation((screenWidth - 450) / 2, (screenHeight - 350) / 2);
MenuBarInit();
this.add( getNorthPanel(), "North" );//增加北部面板
this.add( getCenterPanel(), "Center" );//增加中部面板
this.add( getSouthPanel(), "South" );//增加南部面板
}
//菜单栏的初始化
private void MenuBarInit()
{
menubar = new JMenuBar();
menu_help = new JMenu( "帮助" );
item_help = new JMenuItem( "帮助文档" );
item_help.addActionListener( new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
new HelpDialog();
}
} );
item_about = new JMenuItem( "关于计算器" );
item_about.addActionListener( new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
new AboutDialog();
}
} );
menu_help.add( item_help );
menu_help.addSeparator();
menu_help.add( item_about );
menubar.add( menu_help );
this.setJMenuBar( menubar );
}
//北部面板的布局
private JPanel getNorthPanel()
{
express = new JTextField();
express.setFont( font );
express.setColumns( 34 );
//设置表达式文本框的键盘事件监听器
express.addKeyListener( new KeyListener()
{
public void keyPressed( KeyEvent e )
{
if( e.getKeyCode() == KeyEvent.VK_ENTER && flag )//按下=号
{
calc.run( express.getText() );
result.setText( calc.getResult() );
flag = false;//设置下一次按=或enter是无效的
}
else if( e.getKeyCode() == KeyEvent.VK_ESCAPE )//按下清零
{
express.setText( "" );
result.setText( "" );
express.setFocusable( true );
flag = false;
}
else
{
if( !flag )
{
express.setText( "" );
result.setText( "" );
flag = true;
}
}
}
public void keyReleased( KeyEvent e )
{
}
public void keyTyped( KeyEvent e )
{
}
} );
result = new JTextField();
result.setEditable( false );
result.setFont( font );
result.setColumns( 34 );
JPanel north = new JPanel( new BorderLayout() );
JPanel temp1 = new JPanel( new FlowLayout( FlowLayout.LEFT ) );
JPanel temp2 = new JPanel( new FlowLayout( FlowLayout.LEFT ) );
temp1.add( new JLabel( "运算表达式:" ) );
temp1.add( express );
temp2.add( new JLabel( "运算的结果:" ) );
temp2.add( result );
north.add( temp1, "North" );
north.add( temp2, "South" );
return north;
}
//中部面板的布局
private JPanel getCenterPanel()
{
JPanel center = new JPanel( new FlowLayout( FlowLayout.LEFT, 35, 10 ) );
for( int i = 0; i < 20; i++ )
{
button[i] = new JButton( buttonText[i] );
button[i].setForeground( Color.red );
button[i].setBackground( green );
button[i].setFont( new Font( "Courier New", Font.BOLD, 22 ) );
center.add( button[i] );
button[i].addActionListener( new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
String input = e.getActionCommand();
if( input.equals( "=" ) && flag )//按下=号则进行计算
{
calc.run( express.getText() );
result.setText( calc.getResult() );
flag = false;
}
else if( input.equals( "C" ) )//按下清零
{
express.setText( "" );
result.setText( "" );
express.setFocusable( true );
flag = false;
}
else if( input.equals( "退出" ) )
{
System.exit( 0 );
}
else//按下数字键或是操作符或是小数点则显示
{
if( !flag )
{
express.setText( "" );
result.setText( "" );
flag = true;
}
express.setText( express.getText() + input );
}
}
});
}//button定义结束
button[19].setFont( new Font( "新宋体", Font.CENTER_BASELINE, 16 ) );
center.setBorder( BorderFactory.createBevelBorder( 1 ) );
return center;
}
//南部面板的布局
private JPanel getSouthPanel()
{
JTextField text_time = new JTextField();
JLabel label = new JLabel( "当前时间:" );
label.setFont( new Font( "楷体", Font.LAYOUT_NO_START_CONTEXT, 18 ) );
JPanel south = new JPanel();
south.add( label );
south.add( text_time );
ShowTime show = new ShowTime( text_time );
show.start();
south.setBackground( green );
return south;
}
}
public class Calculator2
{
public static void main( String[] args )
{
GUI calc = new GUI();//建立计算器界面实例
calc.setVisible( true );
//调出帮助文档
new HelpDialog();
}
}
评论3
最新资源