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

📄 calcwindow.java.bak

📁 本“计算器”可以完成任意的通常借助手持计算器来完成的标准运算。“计算器”可用于基本的算术运算
💻 BAK
字号:
///////    CalcWindow.java    ///////

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

//	子类CalcWindow(显示窗口)继承JPanel类
public class CalcWindow extends JPanel
{   public CalcWindow(int size, WinFace f)
    {   lcd = new Lcd(size);
        keypad = new Keypad(f);
			
      // vertical box
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); //布局
        add(Box.createRigidArea(new Dimension(0,40)));
        add(lcd);
        add(Box.createRigidArea(new Dimension(0,60)));
        add(Box.createVerticalGlue());
        add(keypad);
        add(Box.createRigidArea(new Dimension(0,20)));
    }

    protected CalcWindow() {}

    public void showStatus(String s)
    {   lcd.showStatus(s);   }

    public void showStr(String s)
    {   lcd.showStr(s);   }

    public void showOp(char op)
    {   lcd.showOp(op);   }

    public void go()
    {   JFrame win = new JFrame("计算器");		//设置win窗口名称
        win.addWindowListener(new WindowHandler(win));	//添加事件监听
        win.setContentPane(this); 					
		win.pack();								//调整win大小
		createMenu(win);
		win.setVisible(true);					//设置win为可见
    }

	public void createMenu(JFrame frame) {
        JMenuBar bar = new JMenuBar();		//创建菜单栏

		/*//创建[编辑]菜单
		JMenu editMenu = new JMenu("编辑(E)");
		editMenu.setMnemonic('E');
        
		JMenuItem copyItem=new JMenuItem("复制"),pasteItem = new JMenuItem("粘贴");       
		copyItem.setMnemonic('C');
		pasteItem.setMnemonic('P');

		//加载菜单项
		editMenu.add(copyItem);
		editMenu.add(pasteItem);

        bar.add(editMenu);		//将[编辑]菜单加入菜单条中

		*/
		JMenu funcMenu = new JMenu("功能(F)");
		funcMenu.setMnemonic('F');

		JRadioButtonMenuItem stadItem=new JRadioButtonMenuItem("标准型(T)");
		JRadioButtonMenuItem sciItem = new JRadioButtonMenuItem("科学型(S)");
		stadItem.setMnemonic('T');
		sciItem.setMnemonic('S');

		ButtonGroup	funcGroup = new ButtonGroup();

		funcGroup.add(stadItem);		
		funcGroup.add(sciItem);
		stadItem.setSelected(true);

		sciItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JTextArea textArea  = new JTextArea();
                JOptionPane.showMessageDialog(null,"对不起!\n科学计算器功能还有待开发。"

,"说明",1);
                }
        });


		//加载菜单项
		funcMenu.add(stadItem);
		funcMenu.add(sciItem);

        bar.add(funcMenu);		//将[功能]菜单加入菜单条中

        JMenu helpMenu = new JMenu("帮助(H)");
		helpMenu.setMnemonic('H');

		JMenuItem expItem=new JMenuItem("说明"),aboutItem = new JMenuItem("关于计算器");  
		
		helpMenu.add(expItem);
		helpMenu.add(aboutItem);

		expItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JTextArea textArea  = new JTextArea();
                JOptionPane.showMessageDialog(null,"使用“计算器”可以完成任意的通\n常借助手"
				+"持计算器来完成的标准运\n算。“计算器”可用于基本的算术\n运算,比如加减运算等。科学计算器\n功能还有待开发。"

,"说明",1);
                }
        });

        aboutItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null,"小型仿真计算器\n版本:1.0\n版权所有:PaulS.Wang!\n修改:占琪","关于 计算器",
				JOptionPane.ERROR_MESSAGE);
                }
        });
        bar.add(helpMenu);
        frame.setJMenuBar(bar);
    }

	protected Lcd lcd;
    protected Keypad keypad;
}

class Keypad extends JPanel implements ActionListener
{  Keypad(WinFace f) 
   {   face = f;
       setBorder(BorderFactory.createEmptyBorder(5,10,5,10));
	 // create buttons
       Font g = new Font("Courier", Font.BOLD, 22);
       for (int i=0; i < name.length; i++)
       { b[i] = new JButton(name[i]);                    //创建按钮组
         b[i].setVerticalTextPosition(JButton.CENTER);
         b[i].setHorizontalTextPosition(JButton.CENTER);
         b[i].setActionCommand(cmd[i]);                  //为按钮组创建操作命令
         b[i].addActionListener(this);                   //为按钮组添加事件监听
         b[i].setFont(g);								//为按钮组设置字体
       }
	   //设置背景色
       b[1].setBackground(Color.cyan);
       b[2].setBackground(Color.cyan);
       b[3].setBackground(Color.cyan);
       b[15].setBackground(Color.red);
       b[20].setBackground(Color.red);
       layout_buttons();
   }

   public void actionPerformed(ActionEvent e)            //ActionListener
   {   String cmd = e.getActionCommand();
       face.enter(cmd.charAt(0));
   }

   void layout_buttons()                       // 按钮布局
   {   setLayout(gridbag);
       c.fill = GridBagConstraints.BOTH;
       c.weightx = 1.0;   // full size cell
       e.fill = GridBagConstraints.BOTH;
       e.weightx = 1.0;   // full size cell
       e.gridwidth = GridBagConstraints.REMAINDER; //end row
       add_buttons(0,4);
       add_buttons(5,9);
       add_buttons(10,14);
       add_buttons(15,19);
       add_buttons(20,23);
   }

   void add_buttons(int s, int f)              // (9)
   {   for (int i=s; i < f; i++ )
       {   gridbag.setConstraints(b[i], c);
           add(b[i]);
       }
       gridbag.setConstraints(b[f], e);
       add(b[f]);
   }

   WinFace face;
   JButton[] b = new JButton[name.length];
   static String[] name 
       = {"+-", "MR", "M-", "M+", "/", 
          "%",  "7",  "8",  "9",  "X",  
          "sqrt", "4",  "5",  "6",  "-",  
          "C",  "1",  "2",  "3",  "=",  
          "AC", "0",  ".",  "+" }; 
   static String[] cmd 
       = {"N",  "R",  "m",  "M", "/", 
          "%",  "7",  "8",  "9",  "*",  
          "s",  "4",  "5",  "6",  "-",  
          "C",  "1",  "2",  "3",  "=",  
          "A",  "0",  ".",  "+" }; 
   private GridBagLayout gridbag = new GridBagLayout();
   private GridBagConstraints c = new GridBagConstraints();
   private GridBagConstraints e = new GridBagConstraints();
}

class Lcd extends JPanel
{  Lcd(int len)
   {  size =len;
      status = new JLabel(" ", JLabel.LEFT);		//用于显示计算器状态
      num_op = new JLabel(str, JLabel.RIGHT);		//用于显示输入与结果
      setBorder(BorderFactory.createCompoundBorder	//复合边框
      ( BorderFactory.createLoweredBevelBorder(),
        BorderFactory.createEmptyBorder(5,10,5,10)));	//

      setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
      add(status);
      add(Box.createHorizontalGlue());
      display();
      add(num_op);
   }

   void showStatus(String s)
   {   status.setText(s);  }

   void showStr(String s)
   {   str = s;   display();  }

   void showOp(char c)
   {   op = c; display();  }

   void display()
   {   num_op.setText(h1+str+h2+op+h3); }                  // (iii)

   int size;
   String str = "0.";
   char op = ' ';
   JLabel status;      // status
   JLabel num_op;      // number-opcode

   //HTML coded strings
   String h1="<html><p align=right>"+
                 "<font color=blue size=+3><tt><b>";
   String h2="</b></tt></font>" +
               "<font color=red size=+0><tt>&nbsp;";
   String h3="</tt></font></p></html>";
}

⌨️ 快捷键说明

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