程序圓今天給大家帶來的一款基於 awt 實現的圖形化界面的計算器。界面模擬 window 自帶的計算器(當然功能沒人家的強大哈):
這是 win 10 自帶的計算器:
這是我的計算器:
一 功能演示
下面給大家來演示一下功能:
計算器上面的功能都是可以使用的,我也是測試了很多次,大家可以放心使用。
視頻講解:https://b23.tv/ebsTrR
二 流程圖
三 實現
屬性
private JFrame frame;
private ImageIcon icon;
private JTextField textField;
private JButton[] button;
private JPanel panel;
private JLabel label;
/**
* data:當前輸入的數據
*/
private String data = "";
/**
* isLeftAvailable:判斷數據應該向哪一個操作數中存儲
*/
private boolean isLeftAvailable;
/**
* left, right:左右操作數
*/
private double left, right;
private String prevOperaotor = "";
方法
GUI 界面的實現
public void init()
{
setMyFrame();
setMyIcon();
setMyTextField();
setMyButton();
setMyLabel();
display();
}
/**
* setMyFrame:
* @description: 設置窗體
*/
private void setMyFrame()
{
frame = new JFrame();
// 設置 frame 的座標
frame.setLocation(700, 150);
// 設置 frame 的大小
frame.setSize(450, 540);
// 設置 frame 的標題
frame.setTitle("Shepard's Calculator");
// 禁用窗口大小調整
frame.setResizable(false);
//設置佈局,自定義
frame.setLayout(null);
// 關閉窗體
}
/**
* setMyIcon:
* @description: 設置圖標
*/
private void setMyIcon()
{
icon = new ImageIcon("D:\\Java\\JavaCode\\JavaSE\\Calculator\\img\\1.jpg");
// 添加圖片
frame.setIconImage(icon.getImage());
}
/**
* setMyTextField:
* @description: 設置文本域
*/
private void setMyTextField()
{
textField = new JTextField("0");
// 設置文本狂大小位置
textField.setBounds(20,15,400,60);
// 設置文本框字體
textField.setFont(new Font("黑體", Font.BOLD, 35));
// 設置背景顏色
textField.setBackground(new Color( 230, 230, 250));
frame.add(textField);
}
/**
* setMyButton:
* @description: 設置按鍵事件
*/
private void setMyButton()
{
// 按鈕文本
String[] arr =
{ "del","cls","%","/",
"7","8","9","*",
"4","5","6","+",
"1","2","3","-",
"+/-","0",".","=", };
// 按鈕
button = new JButton[arr.length];
// 創建面板
panel = new JPanel();
// 設置面板的佈局方式
panel.setBounds(20, 90, 400, 350);
// 網格佈局
panel.setLayout(new GridLayout(5, 4, 8, 8));
for(int i = 0; i < button.length; i++)
{
// 創建按鈕
button[i] = new JButton(arr[i]);
// 設置按鈕字體
button[i].setFont(new Font("黑體", Font.CENTER_BASELINE, 20));
// 設置按鈕背景顏色
button[i].setBackground(new Color(242, 240, 235));
// 設置 = 號的特殊顏色
if(button.length - 1 == i)
{
button[i].setBackground(new Color(211, 120, 129));
}
// 添加事件
int idx = i;
// 設置鼠標監聽
button[i].addMouseListener(new MouseAdapter() {
// 點擊事件
@Override
public void mouseClicked(MouseEvent e) {
// 獲取按鈕上的內容
click(button[idx].getText());
}
// 鼠標進入組件事件
@Override
public void mouseEntered(MouseEvent e) {
button[idx].setFont(new Font("黑體", Font.CENTER_BASELINE, 35));
button[idx].setBackground(new Color(240, 255, 255));
button[idx].setForeground(new Color(255, 99, 71));
}
// 鼠標離開組件事件
@Override
public void mouseExited(MouseEvent e) {
button[idx].setFont(new Font("黑體", Font.CENTER_BASELINE, 20));
button[idx].setBackground(new Color(242, 240, 235));
button[idx].setForeground(new Color(0, 0, 0));
}
});
// 按鈕添加到面板
panel.add(button[i]);
}
frame.add(panel);
}
/**
* setMyLabel:
* @description: 設置標籤
*/
private void setMyLabel()
{
// 標籤
label = new JLabel();
label.setText("<html><span style='font-size:14px;color:red;font-family:宋體'>版權所有:不會編程的程序圓</span></html>");
label.setBounds(40, 460, 300, 40);
frame.add(label);
}
因爲後面的代碼比較多,設置監聽 和 計算功能的實現 我就不放在文章中了,獲取源代碼,註釋和流程圖 的方式請 看原文(文末):
以上就是本次 Java 計算器的分享,如果你對我的代碼有什麼問題歡迎和我討論