62,614
社区成员
发帖
与我相关
我的任务
分享
package com.core;
import javax.swing.*;
import java.awt.*;
import static com.js.util.SwingConsole.*;
public class MyCal extends JFrame
{
static TextPanel tp = new TextPanel(new BorderLayout());
private ButtonPanel bp = new ButtonPanel(new GridLayout(4, 4, 2, 2));
public MyCal()
{
add(tp, BorderLayout.NORTH);
add(bp, BorderLayout.CENTER);
}
public static void main(String[] args)
{
run(new MyCal(), 400, 350);
}
}
package com.core;
import java.awt.event.*;
import java.util.ArrayList;
import com.js.util.*;
import javax.swing.JButton;
import javax.swing.JOptionPane;
public class ManyListeners
{
private StringBuilder totalNum = new StringBuilder();
private Boolean flagNum = false;
private Boolean flagPoint = false;
private Stack<Character> stackErrCom = new Stack<Character>();
private ArrayList<Float> af = new ArrayList<Float>();
class NumberListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if (false == flagNum)
totalNum.delete(0, 20);
flagNum = true;
String name = ((JButton)e.getSource()).getText();
totalNum.append(name);
MyCal.tp.text.setText(totalNum.toString());
}
}
private void operatorLogic(Character c, String s)
{
try {
af.add(new Float(totalNum.toString()));
}
catch (NumberFormatException nfe) {
JOptionPane.showMessageDialog(
null, "先按数字键并且不要连续按", "吓死你",
JOptionPane.ERROR_MESSAGE
);
return;
}
stackErrCom.push(c);
MyCal.tp.text.setText(s);
totalNum.delete(0, 20);
flagPoint = false;
}
class PlusListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
operatorLogic('+', "+");
}
}
class DivideListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
operatorLogic('/', "/");
}
}
class MultiplyListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
operatorLogic('*', "*");
}
}
class MinusListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
operatorLogic('-', "-");
}
}
class EqualsListener implements ActionListener
{
private String filtrate(Float f1)
{
Integer n1 = f1.intValue();
Float fn1 = n1.floatValue();
if (f1 - fn1 == 0) {
return n1.toString();
}
else {
return f1.toString();
}
}
public void actionPerformed(ActionEvent e)
{
Character ADD = '+';
Character DIVIDE = '/';
Character MUL = '*';
Character MIN = '-';
Float totalF = 0.0f;
String result = null;
af.add(new Float(totalNum.toString()));
totalNum.delete(0, 20);
while (!stackErrCom.empty()) {
if ((stackErrCom.peek()).equals(ADD)) {
totalF = af.get(0) + af.get(1);
af.set(1, totalF);
af.remove(0);
}
if ((stackErrCom.peek()).equals(DIVIDE)) {
totalF = af.get(0) / af.get(1);
af.set(1, totalF);
af.remove(0);
}
if ((stackErrCom.peek()).equals(MUL)) {
totalF = af.get(0) * af.get(1);
af.set(1, totalF);
af.remove(0);
}
if ((stackErrCom.peek()).equals(MIN)) {
totalF = af.get(0) - af.get(1);
af.set(1, totalF);
af.remove(0);
}
stackErrCom.pop();
}
result = filtrate(totalF);
MyCal.tp.text.setText(result);
flagPoint = false;
af.clear();
totalNum.append(result);
flagNum = false;
}
}
class PointListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if (flagNum && !flagPoint) {
totalNum.append(".");
MyCal.tp.text.setText(totalNum.toString());
flagPoint = true;
}
else {
try {
throw new PointException("小数点的烦恼");
} catch (PointException e1) {
JOptionPane.showMessageDialog(
null, "先按数字键并不能按下多个小数点", "吓死你",
JOptionPane.ERROR_MESSAGE
);
}
}
}
}
private class PointException extends Exception
{
public PointException()
{
}
public PointException(String str)
{
}
}
}
package com.core;
import javax.swing.*;
import java.awt.*;
public class ButtonPanel extends JPanel
{
private JButton
b1 = new JButton("1"),
b2 = new JButton("2"),
b3 = new JButton("3"),
b4 = new JButton("4"),
b5 = new JButton("5"),
b6 = new JButton("6"),
b7 = new JButton("7"),
b8 = new JButton("8"),
b9 = new JButton("9"),
b0 = new JButton("0"),
bPlus = new JButton("+"),
bMinus = new JButton("-"),
bMultiply = new JButton("*"),
bDivide = new JButton("/"),
bEquals = new JButton("="),
bPoint = new JButton(".");
private Font font = new Font("Arial Bold", Font.BOLD, 16);
private Font font1 = new Font("Arial Bold", Font.BOLD, 22);
private ManyListeners ml = new ManyListeners();
private ManyListeners.NumberListener nl = ml.new NumberListener();
private ManyListeners.PlusListener pl = ml.new PlusListener();
private ManyListeners.EqualsListener el = ml.new EqualsListener();
private ManyListeners.PointListener pol = ml.new PointListener();
private ManyListeners.DivideListener dl = ml.new DivideListener();
private ManyListeners.MultiplyListener mul = ml.new MultiplyListener();
private ManyListeners.MinusListener mil = ml.new MinusListener();
public ButtonPanel(GridLayout gl)
{
super(gl);
add(b1);add(b2);add(b3);add(b4);add(b5);add(b6);
add(b7);add(b8);add(b9);add(b0);add(bPlus);
add(bMinus);add(bMultiply);add(bDivide);add(bEquals);
add(bPoint);
b1.setFont(font);
b2.setFont(font);
b3.setFont(font);
b4.setFont(font);
b5.setFont(font);
b6.setFont(font);
b7.setFont(font);
b8.setFont(font);
b9.setFont(font);
b0.setFont(font);
bPlus.setFont(font1);
bMinus.setFont(font1);
bDivide.setFont(font1);
bEquals.setFont(font1);
bPoint.setFont(font1);
bMultiply.setFont(font1);
b1.addActionListener(nl);
b2.addActionListener(nl);
b3.addActionListener(nl);
b4.addActionListener(nl);
b5.addActionListener(nl);
b6.addActionListener(nl);
b7.addActionListener(nl);
b8.addActionListener(nl);
b9.addActionListener(nl);
b0.addActionListener(nl);
bPlus.addActionListener(pl);
bEquals.addActionListener(el);
bPoint.addActionListener(pol);
bDivide.addActionListener(dl);
bMultiply.addActionListener(mul);
bMinus.addActionListener(mil);
}
}
package com.core;
import javax.swing.*;
import java.awt.*;
public class TextPanel extends JPanel
{
static JTextField text = new JTextField();
private Font font2 = new Font("Arial Bold", Font.BOLD, 22);
public TextPanel(BorderLayout bl)
{
super(bl);
text.setEditable(false);
text.setHorizontalAlignment(JTextField.RIGHT);
text.setFont(font2);
add(text);
}
}
package com.js.util;
import java.util.LinkedList;
public class Stack<T> {
private LinkedList<T> storage = new LinkedList<T>();
public void push(T v) { storage.addFirst(v); }
public T peek() { return storage.getLast(); }
public T pop() { return storage.removeLast(); }
public boolean empty() { return storage.isEmpty(); }
public String toString() { return storage.toString(); }
} ///:~
package com.js.util;
import javax.swing.*;
public class SwingConsole
{
public static void
run(final JFrame f, final int width, final int height)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run() {
// TODO Auto-generated method stub
f.setTitle(f.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(width, height);
f.setVisible(true);
f.setResizable(false);
}
}
);
}
}