事源:
近期学了一点GUI编程,便折腾了一下,还是有点有趣的,在eclipse上装了WindowBuilder插件,
怎么说,感觉不是很好用(小白刚接触,在面板里找不到想要的功能,可能眼瞎)。
开怼:
总体来说,了解了事件监听机制后,就开始搞事情,听各种dalao蒟蒻说计算器很好做,也是初学者必经之路,
所以就开始攻略一下简单计算器(做了后一点都不简单啊,水平不够的关系吧= =)。
揪心过程:
然后折腾面板的时候一点不满意就删了重新来过(= =嘛,不满意很揪心),折腾了半天,怼着苹果手机上的竖排计算器,
面板总算是做得差不多了,然后就开始怼功能,来来回回摸着手机,饭都不用吃修仙盯手机..
总结就是:
一天半的产品..还有点bug...最痛苦的是..我忘记写注释了!!!!!!(各位写代码一定要记住写注释啊!!!)
源码:
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Stack;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
public class CalcTest extends JFrame {
private static final long serialVersionUID = 45681563L;
private JPanel contentPane;
private JTextField textField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CalcTest frame = new CalcTest();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
private static double Equals(String operator,double a,double b)
{
switch(operator)
{
case "+":return a+b;
case "-":return a-b;
case "*":return a*b;
case "/":return a/b;
}
return 0;
}
/**
* Create the frame.
*/
public CalcTest() {
setTitle("Calc");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 380, 579);
contentPane = new JPanel();
contentPane.setBackground(Color.DARK_GRAY);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(0, 138, 374, 324);
contentPane.add(panel);
panel.setLayout(null);
JButton btnNewJButton = new JButton("AC");
btnNewJButton.setBackground(SystemColor.inactiveCaptionBorder);
btnNewJButton.setFont(new Font("宋体", Font.BOLD, 20));
btnNewJButton.setBounds(1, 0, 93, 81);
panel.add(btnNewJButton);
JButton btnNewJButton_2 = new JButton("%");
btnNewJButton_2.setBackground(SystemColor.inactiveCaptionBorder);
btnNewJButton_2.setFont(new Font("宋体", Font.BOLD, 20));
btnNewJButton_2.setBounds(187, 0, 93, 81);
panel.add(btnNewJButton_2);
JButton btnNewJButton_3 = new JButton("/");
btnNewJButton_3.setForeground(Color.WHITE);
btnNewJButton_3.setBackground(Color.ORANGE);
btnNewJButton_3.setFont(new Font("宋体", Font.BOLD, 20));
btnNewJButton_3.setBounds(280, 0, 93, 81);
panel.add(btnNewJButton_3);
JButton btnNewJButton_4 = new JButton("7");
btnNewJButton_4.setBackground(SystemColor.inactiveCaptionBorder);
btnNewJButton_4.setFont(new Font("宋体", Font.BOLD, 20));
btnNewJButton_4.setBounds(1, 81, 93, 81);
panel.add(btnNewJButton_4);
JButton btnNewJButton_5 = new JButton("8");
btnNewJButton_5.setBackground(SystemColor.inactiveCaptionBorder);
btnNewJButton_5.setFont(new Font("宋体", Font.BOLD, 20));
btnNewJButton_5.setBounds(94, 81, 93, 81);
panel.add(btnNewJButton_5);
JButton btnNewJButton_6 = new JButton("9");
btnNewJButton_6.setBackground(SystemColor.inactiveCaptionBorder);
btnNewJButton_6.setFont(new Font("宋体", Font.BOLD, 20));
btnNewJButton_6.setBounds(187, 81, 93, 81);
panel.add(btnNewJButton_6);
JButton btnNewJButton_7 = new JButton("*");
btnNewJButton_7.setForeground(Color.WHITE);
btnNewJButton_7.setBackground(Color.ORANGE);
btnNewJButton_7.setFont(new Font("宋体", Font.BOLD, 20));
btnNewJButton_7.setBounds(280, 81, 93, 81);
panel.add(btnNewJButton_7);
JButton btnNewJButton_8 = new JButton("4");
btnNewJButton_8.setBackground(SystemColor.inactiveCaptionBorder);
btnNewJButton_8.setFont(new Font("宋体", Font.BOLD, 20));
btnNewJButton_8.setBounds(1, 162, 93, 81);
panel.add(btnNewJButton_8);
JButton btnNewJButton_9 = new JButton("5");
btnNewJButton_9.setBackground(SystemColor.inactiveCaptionBorder);
btnNewJButton_9.setFont(new Font("宋体", Font.BOLD, 20));
btnNewJButton_9.setBounds(94, 162, 93, 81);
panel.add(btnNewJButton_9);
JButton btnNewJButton_10 = new JButton("6");
btnNewJButton_10.setBackground(SystemColor.inactiveCaptionBorder);
btnNewJButton_10.setFont(new Font("宋体", Font.BOLD, 20));
btnNewJButton_10.setBounds(187, 162, 93, 81);
panel.add(btnNewJButton_10);
JButton btnNewJButton_11 = new JButton("-");
btnNewJButton_11.setForeground(Color.WHITE);
btnNewJButton_11.setBackground(Color.ORANGE);
btnNewJButton_11.setFont(new Font("宋体", Font.BOLD, 20));
btnNewJButton_11.setBounds(280, 162, 93, 81);
panel.add(btnNewJButton_11);
JButton btnNewJButton_12 = new JButton("1");
btnNewJButton_12.setBackground(SystemColor.inactiveCaptionBorder);
btnNewJButton_12.setFont(new Font("宋体", Font.BOLD, 20));
btnNewJButton_12.setBounds(1, 243, 93, 81);
panel.add(btnNewJButton_12);
JButton btnNewJButton_13 = new JButton("2");
btnNewJButton_13.setBackground(SystemColor.inactiveCaptionBorder);
btnNewJButton_13.setFont(new Font("宋体", Font.BOLD, 20));
btnNewJButton_13.setSize(93, 81);
btnNewJButton_13.setLocation(94, 243);
panel.add(btnNewJButton_13);
JButton btnNewJButton_14 = new JButton("3");
btnNewJButton_14.setBackground(SystemColor.inactiveCaptionBorder);
btnNewJButton_14.setFont(new Font("宋体", Font.BOLD, 20));
btnNewJButton_14.setSize(93, 81);
btnNewJButton_14.setLocation(187, 243);
panel.add(btnNewJButton_14);
JButton btnNewJButton_15 = new JButton("+");
btnNewJButton_15.setForeground(Color.WHITE);
btnNewJButton_15.setBackground(Color.ORANGE);
btnNewJButton_15.setFont(new Font("宋体", Font.BOLD, 20));
btnNewJButton_15.setBounds(280, 243, 93, 81);
panel.add(btnNewJButton_15);
JButton btnNewJButton_1 = new JButton("+/-");
btnNewJButton_1.setFont(new Font("宋体", Font.BOLD, 20));
btnNewJButton_1.setBackground(SystemColor.inactiveCaptionBorder);
btnNewJButton_1.setBounds(94, 0, 93, 81);
panel.add(btnNewJButton_1);
JPanel panel_1 = new JPanel();
panel_1.setBounds(0, 462, 374, 82);
contentPane.add(panel_1);
panel_1.setLayout(null);
JButton btnNewJButton_16 = new JButton("0");
btnNewJButton_16.setBackground(SystemColor.inactiveCaptionBorder);
btnNewJButton_16.setFont(new Font("宋体", Font.BOLD, 20));
btnNewJButton_16.setBounds(1, 0, 186, 82);
panel_1.add(btnNewJButton_16);
JButton btnNewJButton_18 = new JButton(".");
btnNewJButton_18.setBackground(SystemColor.inactiveCaptionBorder);
btnNewJButton_18.setFont(new Font("宋体", Font.BOLD, 20));
btnNewJButton_18.setBounds(187, 0, 93, 82);
panel_1.add(btnNewJButton_18);
JButton btnNewJButton_19 = new JButton("=");
btnNewJButton_19.setForeground(Color.WHITE);
btnNewJButton_19.setBackground(Color.ORANGE);
btnNewJButton_19.setFont(new Font("宋体", Font.BOLD, 20));
btnNewJButton_19.setBounds(280, 0, 93, 82);
panel_1.add(btnNewJButton_19);
textField = new JTextField();
textField.setHorizontalAlignment(SwingConstants.RIGHT);
textField.setFont(new Font("宋体", Font.PLAIN, 70));
textField.setForeground(Color.WHITE);
textField.setText("0");
textField.setEditable(false);
textField.setBorder(null);
textField.setBackground(Color.DARK_GRAY);
textField.setBounds(14, 56, 346, 82);
contentPane.add(textField);
textField.setColumns(10);
Stack<Double> Number = new Stack<>();
Stack<String> Operator = new Stack<>();
StringBuffer sb = new StringBuffer();
{
btnNewJButton_12.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int flag = 0;
if(sb.indexOf("-")!=-1)flag=1;
if(sb.length()-flag>=9)return;
btnNewJButton.setText("C");
sb.append("1");
textField.setText(sb.toString());
}
});
}
{
btnNewJButton_13.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int flag = 0;
if(sb.indexOf("-")!=-1)flag=1;
if(sb.length()-flag>=9)return;
btnNewJButton.setText("C");
sb.append("2");
textField.setText(sb.toString());
}
});
}
{
btnNewJButton_14.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int flag = 0;
if(sb.indexOf("-")!=-1)flag=1;
if(sb.length()-flag>=9)return;
btnNewJButton.setText("C");
sb.append("3");
textField.setText(sb.toString());
}
});
}
{
btnNewJButton_8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int flag = 0;
if(sb.indexOf("-")!=-1)flag=1;
if(sb.length()-flag>=9)return;
btnNewJButton.setText("C");
sb.append("4");
textField.setText(sb.toString());
}
});
}
{
btnNewJButton_9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int flag = 0;
if(sb.indexOf("-")!=-1)flag=1;
if(sb.length()-flag>=9)return;
btnNewJButton.setText("C");
sb.append("5");
textField.setText(sb.toString());
}
});
}
{
btnNewJButton_10.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int flag = 0;
if(sb.indexOf("-")!=-1)flag=1;
if(sb.length()-flag>=9)return;
btnNewJButton.setText("C");
sb.append("6");
textField.setText(sb.toString());
}
});
}
{
btnNewJButton_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int flag = 0;
if(sb.indexOf("-")!=-1)flag=1;
if(sb.length()-flag>=9)return;
btnNewJButton.setText("C");
sb.append("7");
textField.setText(sb.toString());
}
});
}
{
btnNewJButton_5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int flag = 0;
if(sb.indexOf("-")!=-1)flag=1;
if(sb.length()-flag>=9)return;
btnNewJButton.setText("C");
sb.append("8");
textField.setText(sb.toString());
}
});
}
{
btnNewJButton_6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int flag = 0;
if(sb.indexOf("-")!=-1)flag=1;
if(sb.length()-flag>=9)return;
btnNewJButton.setText("C");
sb.append("9");
textField.setText(sb.toString());
}
});
}
{
btnNewJButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
btnNewJButton.setText("AC");
sb.delete(0,sb.length());
textField.setText("0");
while(!Operator.isEmpty())Operator.pop();
while(!Number.isEmpty())Number.pop();
}
});
}
{
btnNewJButton_16.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(sb.length()==0 || (sb.length()==1 && sb.indexOf("-")!=-1))return;
int flag = 0;
if(sb.indexOf("-")!=-1)flag=1;
if(sb.length()-flag>=9)return;
btnNewJButton.setText("C");
sb.append("0");
textField.setText(sb.toString());
}
});
}
{
btnNewJButton_18.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(sb.length()>=9)return;
if(sb.indexOf(".")!=-1)return;
btnNewJButton.setText("C");
if(sb.length()==0 || (sb.length()==1 && sb.indexOf("-")!=-1))
sb.append("0.");
else
sb.append(".");
textField.setText(sb.toString());
}
});
}
{
btnNewJButton_1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(sb.indexOf("-")==-1)
{
sb.insert(0,'-');
if(sb.length()==1)
{
textField.setText("-0");
return;
}
}
else
{
sb.delete(0,1);
if(sb.length()==0)
{
textField.setText("0");
return;
}
}
textField.setText(sb.toString());
}
});
}
{
btnNewJButton_15.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
double num = Double.parseDouble(sb.toString());
while(Operator.size()>=1)
num = Equals(Operator.pop(),Number.pop(),num);
String str = num+"";
if(str.length()>9)
str = str.substring(0,9);
if(str.endsWith(".0"))
str = str.substring(0,str.length()-2);
textField.setText(str);
Number.push(num);
Operator.push("+");
sb.delete(0,sb.length());
}catch(NumberFormatException e1){}
}
});
}
{
btnNewJButton_11.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
double num = Double.parseDouble(sb.toString());
while(Operator.size()>=1)
num = Equals(Operator.pop(),Number.pop(),num);
String str = num+"";
if(str.length()>9)
str = str.substring(0,9);
if(str.endsWith(".0"))
str = str.substring(0,str.length()-2);
textField.setText(str);
Number.push(num);
Operator.push("-");
sb.delete(0,sb.length());
}catch(NumberFormatException e1){}
}
});
}
{
btnNewJButton_7.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
double num = Double.parseDouble(sb.toString());
if(Operator.size()>=1 && (Operator.peek().equals("*") || Operator.peek().equals("/")))
num = Equals(Operator.pop(),Number.pop(),num);
String str = num+"";
if(str.length()>9)
str = str.substring(0,9);
if(str.endsWith(".0"))
str = str.substring(0,str.length()-2);
textField.setText(str);
Number.push(num);
Operator.push("*");
sb.delete(0,sb.length());
}catch(NumberFormatException e1){}
}
});
}
{
btnNewJButton_3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
double num = Double.parseDouble(sb.toString());
if(Operator.size()>=1 && (Operator.peek().equals("*") || Operator.peek().equals("/")))
num = Equals(Operator.pop(),Number.pop(),num);
String str = num+"";
if(str.length()>9)
str = str.substring(0,9);
if(str.endsWith(".0"))
str = str.substring(0,str.length()-2);
textField.setText(str);
Number.push(num);
Operator.push("/");
sb.delete(0,sb.length());
}catch(NumberFormatException e1){}
}
});
}
{
btnNewJButton_19.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
double num = Double.parseDouble(sb.toString());
while(!Operator.isEmpty())
num = Equals(Operator.pop(),Number.pop(),num);
String str = num+"";
if(str.length()>9)
str = str.substring(0,9);
if(str.endsWith(".0"))
str = str.substring(0,str.length()-2);
textField.setText(str);
sb.delete(0,sb.length());
if(num!=0)
sb.append(str);
}
});
}
{
btnNewJButton_2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
double num = Double.parseDouble(sb.toString());
if(num==0)
{
sb.delete(0,sb.length());
textField.setText("0");
return;
}
num /= 100;
sb.delete(0,sb.length());
sb.append(num);
textField.setText(sb.toString());
}catch(NumberFormatException e1){}
}
});
}
}
}