求源碼計算器
發布時間: 2023-07-04 21:05:37
㈠ 求一份java計算器源代碼,要有()+-*/和清除鍵及小數點的,不用科學計算器
importjava.awt.BorderLayout;
importjava.awt.Dimension;
importjava.awt.GridLayout;
importjava.awt.Toolkit;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JPanel;
importjavax.swing.SwingUtilities;
importjavax.swing.UIManager;
{
privateJPaneljp;//中間的面板
privatedoubleresult;//計算結果
privatebooleanstart;//用於判斷是否是首次輸入,true首次,false不是首次
privatebooleanflag;//用於判斷是否清空顯示區域的值,true需要,false不需要
privateStringlastCommand;//用於保存運算符
JButtondisplay;//顯示區域
publicCalculatorPanel(){
//初始化各項值
result=0;
start=true;
flag=false;
lastCommand="=";
//設置CalculatorPanel這個面板的布局為框架布局
setLayout(newBorderLayout());
//這個按鈕用來顯示值的區域
display=newJButton("0.0");
display.setEnabled(false);//按鈕樣式設置為禁用樣式
//用來初始化,清除用的
JButtonclear=newJButton("clear");
clear.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
//作初始化的操作
result=0;
start=true;
flag=false;
lastCommand="=";
display.setText("0.0");
}
});
jp=newJPanel();
//將此面板布局設置為網格布局,4行4列
jp.setLayout(newGridLayout(4,4));
//實例化監聽器對象
NumberActionna=newNumberAction();
CommandActionca=newCommandAction();
makeButton("7",na);
makeButton("8",na);
makeButton("9",na);
makeButton("/",ca);
makeButton("4",na);
makeButton("5",na);
makeButton("6",na);
makeButton("*",ca);
makeButton("1",na);
makeButton("2",na);
makeButton("3",na);
makeButton("-",ca);
makeButton("0",na);
makeButton(".",na);
makeButton("+",ca);
makeButton("=",ca);
//將面板,兩個按鍵加入到我的面板,實現計算器的界面
add(display,BorderLayout.NORTH);
add(jp,BorderLayout.CENTER);
add(clear,BorderLayout.SOUTH);
}
privatevoidmakeButton(StringbuttonName,ActionListeneral){
JButtonjb=newJButton(buttonName);
jp.add(jb);
jb.addActionListener(al);
}
//數字監聽器
{
@Override
publicvoidactionPerformed(ActionEvente){
JButtonjb=(JButton)e.getSource();//獲取事件源對象
Stringinput=jb.getText();//獲取事件源對象上的標簽
if(start){//首次輸入
//一上就".",就什麼也不做
if(input.equals(".")){
return;
}
//如果是"0.0"的話,就清空
if(display.getText().equals("0.0")){
display.setText("");
}
start=false;//改變是否首次輸入的標記值
}else{
//判斷顯示區域的值裡面是否已經有".",如果有,輸入的又是".",就什麼都不做
if(display.getText().indexOf(".")!=-1){
if(input.equals(".")){
return;
}
}
//判斷顯示區域的值裡面只有"-",輸入的又是".",就什麼都不做
if(display.getText().equals("-")){
if(input.equals(".")){
return;
}
}
//判斷顯示區域的值如果是"0",輸入的不是".",就什麼也不做
if(display.getText().equals("0")){
if(!input.equals(".")){
return;
}
}
}
//如果我點擊了運算符以後,再輸入數字的話,就要清空顯示區域的值
if(flag){
display.setText("");
flag=false;//還原初始值,不需要清空
}
display.setText(display.getText()+input);//設置顯示區域的值
}
}
//運算符監聽器
{
@Override
publicvoidactionPerformed(ActionEvente){
JButtonjb=(JButton)e.getSource();//獲取事件源對象
StringinputCommand=jb.getText();//獲取事件源對象上的標簽
if(start){//首次輸入
//一上就是"-"
if(inputCommand.equals("-")){
display.setText("-");//顯示區域的內容設置為"-"
start=false;//改變首次輸入的標記
}
}else{
if(!flag){//如果flag=false不需要清空顯示區的值,就調用方法計算
calculate(Double.parseDouble(display.getText()));//保存顯示區域的值,並計算
}
//保存你點擊的運算符
lastCommand=inputCommand;
flag=true;//因為我這里已經輸入過運算符,
}
}
}
//計算用的方法
privatevoidcalculate(doublex){
if(lastCommand.equals("+")){
result+=x;
}elseif(lastCommand.equals("-")){
result-=x;
}elseif(lastCommand.equals("*")){
result*=x;
}elseif(lastCommand.equals("/")){
result/=x;
}elseif(lastCommand.equals("=")){
result=x;
}
display.setText(""+result);
}
}
classMyFrameUitl{
publicstaticvoidinit(JFramejFrame,DimensionframeSize,Stringtitle,
StringiconFileName,booleanresizable){
Toolkittk=Toolkit.getDefaultToolkit();//系統工具包,可以讀取系統信息
DimensionscreenSize=tk.getScreenSize();//獲取屏幕大小
//獲取寬高
intscreenWidth=screenSize.width;
intscreenHeight=screenSize.height;
//找中心點
intcenterX=screenWidth/2;
intcenterY=screenHeight/2;
//解析窗體大小
intframeWidth=frameSize.width;
intframeHeight=frameSize.height;
//設置窗體位置
jFrame.setBounds(centerX-frameWidth/2,centerY-frameHeight/2,
frameWidth,frameHeight);
jFrame.setTitle(title);//設置標題
if(iconFileName!=null){
jFrame.setIconImage(tk.getImage(iconFileName));//設置圖標
}
//設置皮膚com.sun.java.swing.plaf.windows.WindowsLookAndFeel
/**/
try{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
SwingUtilities.updateComponentTreeUI(jFrame);
}catch(Exceptione1){
e1.printStackTrace();
}
jFrame.setResizable(resizable);//設置窗體是否可以改變大小
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設置程序關閉動作
jFrame.setVisible(true);//顯示窗體
}
}
{
publicCalculatorFrame(){
add(newCalculatorPanel());
MyFrameUitl.init(this,newDimension(400,300),"簡易計算器",null,
false);
this.pack();//讓組件按原始大小顯示,並且窗體的大小剛好能夠容納所有的組件
}
}
publicclassCalculatorTest{
publicstaticvoidmain(String[]args){
newCalculatorFrame();
}
}
運行結果:
㈡ 如何查看電腦上某程序的源代碼 如計算器
可以通過GitHub源代碼ping在計算機中檢查計算器的源代碼。具體操作方式如下:
1、進入GitHub的Microsoft個人問題主頁,如下圖所示。
(2)求源碼計算器擴展閱讀:
GitHub的Windows應用
GitHub 使用 git 分布式版本控制系統,而 git 最初是 LinusTorvalds 為幫助Linux開發而創造的,它針對的是 Linux 平台,因此 git 和 Windows 從來不是最好的朋友,因為它一點也不像Windows。
GitHub 發布了GitHub for Windows,為 Windows 平台開發者提供了一個易於使用的 Git 圖形客戶端。
GitHub forWindows是一個 Metro 風格應用程序,集成了自包含版本的 Git,bash 命令行 shell,PowerShell 的 posh-git 擴展。
GitHub 為 Windows 用戶提供了一個基本的圖形前端去處理大部分常用版本控制任務,可以創建版本庫,向本地版本庫遞交補丁,在本地和遠程版本庫之間同步。微軟也通過CodePlex向開發者提供 git 版本控制系統,而 GitHub 創造了一個更具有吸引力的 Windows 版本。
熱點內容