需要安装好的插件
在设置代码保存时候自动实现格式化功能前需要安装好两个插件,分别是:
- google-java-format
- Save Action
具体的安装过程,我在上一篇文章介绍过了。
google-java-format设置
google-java-format设置前,需要打开下面途中的其他设置,双击 google-java-format插件,在右侧选项中勾选Enable google-java-format选项。
Save Action设置
Save Action设置需要勾选相关的选项,实现保存时候自动格式化代码。
效果
当所有配置好了之后,按住Ctrl+S则可以在保存的同时格式化代码格式。
以下为格式化后的一段计算器代码,非常快捷高效。
package Calculator;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Stack;
import javax.swing.*;
class MyException extends Exception {
public MyException() {
super();
}
public MyException(String message) {
super(message);
}
}
class SwingConsole {
public static void run(final JFrame f, final int width, final int height) {//酱紫,里面可以用非main方法
SwingUtilities.invokeLater(new Runnable() {
public void run() {
f.setTitle(f.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(width, height);
f.setVisible(true);
}
});
}
}
public class Calculator extends JFrame {//这是我的public类,ONLY ONE
private JTextField textField; //输入文本框
private String input; //结果
private String operator; //操作符
public Calculator() { //函数
input = "";
operator = "";
Container container = this.getContentPane();
JPanel panel = new JPanel();
textField = new JTextField(30);
textField.setEditable(false); //文本框禁止编辑
textField.setHorizontalAlignment(JTextField.LEFT);
//textField.setBounds(100, 100, 20, 20); //在容器布局为空情况下生效
textField.setPreferredSize(new Dimension(200, 30));
container.add(textField, BorderLayout.NORTH);
String[] name = {"7", "8", "9", "+", "4", "5", "6", "-", "1", "2", "3", "*", "0", "C", "=", "/"};
panel.setLayout(new GridLayout(4, 4, 1, 1));
for (int i = 0; i < name.length; i++) {
JButton button = new JButton(name[i]);
button.addActionListener(new MyActionListener());
panel.add(button);
}
container.add(panel, BorderLayout.CENTER);
}
class MyActionListener implements ActionListener { //内部类实现按钮响应
@Override
public void actionPerformed(ActionEvent e) {
int cnt = 0;
String actionCommand = e.getActionCommand(); //获取按钮上的字符串
if (actionCommand.equals("+") || actionCommand.equals("-") || actionCommand.equals("*")
|| actionCommand.equals("/")) {
input += " " + actionCommand + " ";
} else if (actionCommand.equals("C")) { //清除输入
input = "";
} else if (actionCommand.equals("=")) { //按下等号
try {
input += "=" + calculate(input);
} catch (MyException e1) {
if (e1.getMessage().equals("Infinity"))
input += "=" + e1.getMessage();
else
input = e1.getMessage();
}
textField.setText(input);
input = "";
cnt = 1;
} else
input += actionCommand; //按下数字
if (cnt == 0)
textField.setText(input);
}
}
private String calculate(String input) throws MyException { //计算函数
String[] comput = input.split(" ");
Stack<Double> stack = new Stack<>();
Double m = Double.parseDouble(comput[0]);
stack.push(m); //第一个操作数入栈
for (int i = 1; i < comput.length; i++) {
if (i % 2 == 1) {
if (comput[i].equals("+"))
stack.push(Double.parseDouble(comput[i + 1]));
if (comput[i].equals("-"))
stack.push(-Double.parseDouble(comput[i + 1]));
if (comput[i].equals("*")) { //将前一个数出栈做乘法再入栈
Double d = stack.peek(); //取栈顶元素
stack.pop();
stack.push(d * Double.parseDouble(comput[i + 1]));
}
if (comput[i].equals("/")) { //将前一个数出栈做乘法再入栈
double help = Double.parseDouble(comput[i + 1]);
if (help == 0)
throw new MyException("Infinity"); //不会继续执行该函数
double d = stack.peek();
stack.pop();
stack.push(d / help);
}
}
}
double d = 0d;
while (!stack.isEmpty()) { //求和
d += stack.peek();
stack.pop();
}
String result = String.valueOf(d);
return result;
}
public static void main(String[] args) {
SwingConsole.run(new Calculator(), 250, 300);
}
}