计算器应用程序
来源:互联网 发布:python 2.7不支持中文 编辑:程序博客网 时间:2024/06/05 10:04
Calculator.java
package webstart;import java.awt.*;import javax.swing.*;public class Calculator{ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { CalculatorFrame frame = new CalculatorFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }); }}
CalculatorFrame.java
package webstart;import java.awt.event.*;import java.beans.*;import java.io.*;import java.net.*;import javax.jnlp.*;import javax.swing.*;/** * A frame with a calculator panel and a menu to load and save the calculator history. */public class CalculatorFrame extends JFrame{ private CalculatorPanel panel; public CalculatorFrame() { setTitle(); panel = new CalculatorPanel(); add(panel); JMenu fileMenu = new JMenu("File"); JMenuBar menuBar = new JMenuBar(); menuBar.add(fileMenu); setJMenuBar(menuBar); JMenuItem openItem = fileMenu.add("Open"); openItem.addActionListener(EventHandler.create(ActionListener.class, this, "open")); JMenuItem saveItem = fileMenu.add("Save"); saveItem.addActionListener(EventHandler.create(ActionListener.class, this, "save")); pack(); } /** * Gets the title from the persistent store or asks the user for the title if there is no prior * entry. */ public void setTitle() { try { String title = null; BasicService basic = (BasicService) ServiceManager.lookup("javax.jnlp.BasicService"); URL codeBase = basic.getCodeBase(); PersistenceService service = (PersistenceService) ServiceManager .lookup("javax.jnlp.PersistenceService"); URL key = new URL(codeBase, "title"); try { FileContents contents = service.get(key); InputStream in = contents.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); title = reader.readLine(); } catch (FileNotFoundException e) { title = JOptionPane.showInputDialog("Please supply a frame title:"); if (title == null) return; service.create(key, 100); FileContents contents = service.get(key); OutputStream out = contents.getOutputStream(true); PrintStream printOut = new PrintStream(out); printOut.print(title); } setTitle(title); } catch (UnavailableServiceException e) { JOptionPane.showMessageDialog(this, e); } catch (MalformedURLException e) { JOptionPane.showMessageDialog(this, e); } catch (IOException e) { JOptionPane.showMessageDialog(this, e); } } /** * Opens a history file and updates the display. */ public void open() { try { FileOpenService service = (FileOpenService) ServiceManager .lookup("javax.jnlp.FileOpenService"); FileContents contents = service.openFileDialog(".", new String[] { "txt" }); JOptionPane.showMessageDialog(this, contents.getName()); if (contents != null) { InputStream in = contents.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); String line; while ((line = reader.readLine()) != null) { panel.append(line); panel.append("\n"); } } } catch (UnavailableServiceException e) { JOptionPane.showMessageDialog(this, e); } catch (IOException e) { JOptionPane.showMessageDialog(this, e); } } /** * Saves the calculator history to a file. */ public void save() { try { ByteArrayOutputStream out = new ByteArrayOutputStream(); PrintStream printOut = new PrintStream(out); printOut.print(panel.getText()); InputStream data = new ByteArrayInputStream(out.toByteArray()); FileSaveService service = (FileSaveService) ServiceManager .lookup("javax.jnlp.FileSaveService"); service.saveFileDialog(".", new String[] { "txt" }, data, "calc.txt"); } catch (UnavailableServiceException e) { JOptionPane.showMessageDialog(this, e); } catch (IOException e) { JOptionPane.showMessageDialog(this, e); } }}
CalculatorPanel.java
package webstart;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.text.*;/** A panel with calculator buttons and a result display.*/public class CalculatorPanel extends JPanel{ private JTextArea display; private JPanel panel; private double result; private String lastCommand; private boolean start; /** Lays out the panel. */ public CalculatorPanel() { setLayout(new BorderLayout()); result = 0; lastCommand = "="; start = true; // add the display display = new JTextArea(10, 20); add(new JScrollPane(display), BorderLayout.NORTH); ActionListener insert = new InsertAction(); ActionListener command = new CommandAction(); // add the buttons in a 4 x 4 grid panel = new JPanel(); panel.setLayout(new GridLayout(4, 4)); addButton("7", insert); addButton("8", insert); addButton("9", insert); addButton("/", command); addButton("4", insert); addButton("5", insert); addButton("6", insert); addButton("*", command); addButton("1", insert); addButton("2", insert); addButton("3", insert); addButton("-", command); addButton("0", insert); addButton(".", insert); addButton("=", command); addButton("+", command); add(panel, BorderLayout.CENTER); } /** Gets the history text. @return the calculator history */ public String getText() { return display.getText(); } /** Appends a string to the history text. @param s the string to append */ public void append(String s) { display.append(s); } /** Adds a button to the center panel. @param label the button label @param listener the button listener */ private void addButton(String label, ActionListener listener) { JButton button = new JButton(label); button.addActionListener(listener); panel.add(button); } /** This action inserts the button action string to the end of the display text. */ private class InsertAction implements ActionListener { public void actionPerformed(ActionEvent event) { String input = event.getActionCommand(); start = false; display.append(input); } } /** This action executes the command that the button action string denotes. */ private class CommandAction implements ActionListener { public void actionPerformed(ActionEvent event) { String command = event.getActionCommand(); if (start) { if (command.equals("-")) { display.append(command); start = false; } else lastCommand = command; } else { try { int lines = display.getLineCount(); int lineStart = display.getLineStartOffset(lines - 1); int lineEnd = display.getLineEndOffset(lines - 1); String value = display.getText(lineStart, lineEnd - lineStart); display.append(" "); display.append(command); calculate(Double.parseDouble(value)); if (command.equals("=")) display.append("\n" + result); lastCommand = command; display.append("\n"); start = true; } catch (BadLocationException e) { e.printStackTrace(); } } } } /** Carries out the pending calculation. @param x the value to be accumulated with the prior result. */ public void calculate(double x) { if (lastCommand.equals("+")) result += x; else if (lastCommand.equals("-")) result -= x; else if (lastCommand.equals("*")) result *= x; else if (lastCommand.equals("/")) result /= x; else if (lastCommand.equals("=")) result = x; } }
0 0
- 计算器应用程序
- android应用程序--计算器
- C# 控制台应用程序 计算器
- QT 应用程序之计算器
- MFC编写简易计算器应用程序
- 嵌入式GUI-应用程序-截图-记事本-计算器 等
- 实现一个Java GUI计算器应用程序界面
- C#学习之七---计算器应用程序
- 控制台应用程序下的计算器(调用子类)
- Java Web Start 发布计算器应用程序
- C#实现桌面应用程序简单的计算器
- smart210上Qt应用程序开发实例(加法计算器)
- 什么是MFC,用MFC建立一个简单的计算器应用程序
- 计算器
- 计算器
- /* 计算器 */
- 计算器
- 计算器
- rtl8192 调试
- Java Web Start 发布计算器应用程序
- Android微信分享
- ListView的下拉刷新、滑动删除和上拉加载更多
- 132. Palindrome Partitioning II
- 计算器应用程序
- 第十二周oj项目D汉罗塔
- Objective-C编程 - 关于Block的要点
- Linux 内核资源限制 -- 栈限制
- 基于opencv的简单视频处理类示例
- adb
- openv直方图均衡化综合示例
- 有限元网格开源软件GMSH基础
- 第12周OJ实践 进制转换(十进制转二进制)