一、
接触Java不算特别长的时间,但是Java的界面确实不好写,相对于VB、VS来说,没有可视化的界面可以操作(可能你会反驳说Eclipse的windowBuilder插件可以实现可视化操作,但我只想说windowbuilder的操作性一般般,优化做得不够好,拖动组件时卡得不要不要的,对于刚接触Java Frame的朋友,建议先手敲,只有手敲熟了,对于组件的属性以及事件接口就能熟记于心了)。
二、
这次运用了一些基本的语言,写了个计算器,实现了最基本的一些功能,像+、-、*、\、√等,但是还有很多功能还是没有加上去的,像菜单事件,快捷键事件,一些特别的运算功能等。只能说一步一步来了。
三、
从中发现,学习Java,逻辑思维跟算法真的很重要!!
以下是代码:
package example;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
@SuppressWarnings("serial")
public class Calculator extends JFrame {
// 设置窗口的默认宽度和长度
private static final int DEFAULT_WIDTH = 270;
private static final int DEFAULT_HEIGHT = 380;
// 分别设置文本框、按钮、菜单的字体
private Font fTest = new Font("Arial", Font.PLAIN, 30);
private Font fButton = new Font("Arial", Font.PLAIN, 16);
private Font fMenu = new Font("微软雅黑", Font.PLAIN, 15);
private JMenuBar menuBar; // 声明菜单栏对象
private JTextField text; // 声明文本框,用于显示计算结果
private JPanel panel; // 声明主面板,用于放置基本按钮
private JPanel textPanel;
private double result; // 用于存放结果的变量
private String lastCommand; //
private boolean start; //
/**
* 构造方法,用于初始化实例
*/
public Calculator() throws ClassNotFoundException, InstantiationException, IllegalAccessException,
UnsupportedLookAndFeelException {
// 设置为边框布局
setLayout(new BorderLayout());
result = 0;
lastCommand = "=";
start = true;
text = new JTextField("0"); // 创建文本框对象,并且默认显示值为0
text.setBackground(Color.WHITE); // 设置文本框背景颜色
text.setFont(fTest); // 设置文本框字体
text.setHorizontalAlignment(JTextField.RIGHT); // 将文本右对齐
text.setEditable(false); // 将文本框设置为不可编辑
ActionListener insert = new InsertAction();
ActionListener command = new CommandAction();
menuBar = new JMenuBar(); // 创建菜单栏对象
// 调用addMenu方法来创建菜单
addMenu("查看(L)");
addMenu("编辑(E)");
addMenu("帮助(H)");
textPanel = new JPanel(); // 创建文本框面板对象
textPanel.setLayout(new GridLayout(1, 1)); // 采用网格布局,设置为1行1列
// 在面板中添加一个(top:上边界,left:左边界,bottom:下边界,right:右边界)为(5,15,3,15)的空边框
textPanel.setBorder(BorderFactory.createEmptyBorder(5, 15, 3, 15));
panel = new JPanel(); // 创建按钮面板对象
panel.setLayout(new GridLayout(5, 4)); // 面板采用网格布局,设置为5行4列
panel.setBorder(BorderFactory.createEmptyBorder(3, 14, 10, 14)); // 同上
// 调用addButton方法来创建按钮对象
addButton("C", command);
addButton("%", command);
addButton("1/x", command);
addButton("√", command);
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);
textPanel.add(text);
add(textPanel, BorderLayout.NORTH);
add(panel, BorderLayout.CENTER); // 将按钮面板放在边框布局的中间
setJMenuBar(menuBar); // 将菜单栏添加到窗口容器中
}
/**
* 添加菜单的方法
*/
private void addMenu(String name) throws ClassNotFoundException, InstantiationException, IllegalAccessException,
UnsupportedLookAndFeelException {
// 改变菜单栏的观感,java默认为metal,个人觉得巨丑,下面代码将metal观感改为window界面观感
String laf = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
UIManager.setLookAndFeel(laf);
SwingUtilities.updateComponentTreeUI(this);
pack();
JMenu menu = new JMenu(name);
menu.setFont(fMenu);
menuBar.add(menu);
}
/**
* 添加按钮的方法
*/
private void addButton(String label, ActionListener listener) throws ClassNotFoundException, InstantiationException,
IllegalAccessException, UnsupportedLookAndFeelException {
// 改变按钮的观感
String laf = "javax.swing.plaf.nimbus.NimbusLookAndFeel";
UIManager.setLookAndFeel(laf);
SwingUtilities.updateComponentTreeUI(this);
pack();
JButton button = new JButton(label);
button.setFont(fButton);
// 为按钮添加事件
button.addActionListener(listener);
panel.add(button);
}
private class InsertAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String input = e.getActionCommand();
if (start) {
text.setText("");
start = false;
}
text.setText(text.getText() + input);
}
}
private class CommandAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals("C")) {
init();
}
if (start) {
if (command.equals("-")) {
text.setText(command);
start = false;
} else
lastCommand = command;
} else {
calculator_1(Double.parseDouble(text.getText()));
lastCommand = command;
start = true;
}
}
}
/**
* 运算符功能的实现
*/
public void calculator_1(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;
} else if (lastCommand.equals("√")) {
result = Double.parseDouble(text.getText());
result = Math.sqrt(result);
}
text.setText("" + result);
}
//初始化
public void init() {
text.setText("0");
result = 0;
lastCommand = "=";
start = true;
}
/**
* 主方法,程序的入口
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame frame;
try {
frame = new Calculator();
// 获取屏幕的宽度与长度
Toolkit kit = Toolkit.getDefaultToolkit();
int screenWidth = kit.getScreenSize().width;
int screenHeight = kit.getScreenSize().height;
// 将窗口设置为屏幕居中显示
frame.setBounds((screenWidth - DEFAULT_WIDTH) / 2, (screenHeight - DEFAULT_HEIGHT) / 2,
DEFAULT_WIDTH, DEFAULT_HEIGHT);
frame.setTitle("计算器");
frame.setResizable(false); // 设置窗口大小不可改变
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException
| UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
}
});
}
}