環(huán)境搭建安裝java、eclipse、以及對(duì)應(yīng)的swt插件。
開(kāi)始工程
建立工程:在java下建立一個(gè)在其他 —- WindowsBuilder —- SWTdesigner —- SWT/JFrame Java PRoject。建立好以后運(yùn)行將得到一個(gè)swt窗口。
設(shè)計(jì)界面:點(diǎn)擊代碼下的Design選項(xiàng)卡 —- 拖動(dòng)一個(gè)布局到窗口里 —- 然后自定義界面如果沒(méi)有發(fā)現(xiàn)Design選項(xiàng)卡的話在代碼編輯窗口內(nèi)右擊 選擇打開(kāi)方式為: Windows Builder edit
碼代碼:獲取框里的文本:text.getText();設(shè)置文本:text.setText();很簡(jiǎn)單的函數(shù)。
Question & BUG TODO 處理按下 1 后顯示 01 的問(wèn)題 FIXME 按下后變?yōu)?.1 FIXME 直接按下 . 的話前面沒(méi)有變?yōu)?0 . TODO 字符串 轉(zhuǎn) double TODO 處理浮點(diǎn)數(shù)運(yùn)算后結(jié)果多余顯示的“.0”
直接復(fù)制代碼或把代碼放到不同的計(jì)算器中運(yùn)行的話可能hi出現(xiàn)一大片紅色的錯(cuò)誤,是因?yàn)樵谛碌沫h(huán)境中沒(méi)有把需要的包給導(dǎo)入。
需要在項(xiàng)目上右擊 —- 屬性 —- 然后一步步把需要的包導(dǎo)入,很多簡(jiǎn)單,不懂就多試試。
在java中運(yùn)算浮點(diǎn)數(shù)的話會(huì)得不到準(zhǔn)確的值是因?yàn)橛?jì)算器中是用二進(jìn)制來(lái)存數(shù)據(jù)的,所以一個(gè)小數(shù)在計(jì)算機(jī)中會(huì)是無(wú)限循環(huán)的。
要處理這個(gè)BUG的話就需要使用java的BigDecimal類。
怎么把左上角的圖標(biāo)自定義?——–在Design里點(diǎn)擊窗口、在左邊的屬性框里找到Image。
怎么設(shè)置文本框里的文本出現(xiàn)在后邊?——也是在屬性框里找到Style 找到 align,設(shè)置。
怎么把工作空間導(dǎo)入eclipse?——–在工作空間那里(如果沒(méi)有這個(gè)窗口的話,在窗口那里找到它,把它顯示出來(lái))右擊導(dǎo)入 —- General —- 現(xiàn)有項(xiàng)目到工作空間 —- 選擇根目錄 —-選項(xiàng)目文件夾的上層文件夾—- Finish
package com.java; * 計(jì)算器import org.eclipse.swt.widgets.Display;import org.eclipse.swt.widgets.Shell;import org.eclipse.swt.layout.GridLayout;import org.eclipse.swt.widgets.Button;import org.eclipse.swt.SWT;import org.eclipse.swt.layout.GridData;import org.eclipse.swt.widgets.Text;import org.eclipse.swt.events.SelectionAdapter;import org.eclipse.swt.events.SelectionEvent;import org.eclipse.wb.swt.SWTResourceManager;public class Test { protected Shell shell; private Text text; private int op = 0; private double num1; private double num2; private static final int op_ADD =1; private static final int op_SUB =2; private static final int op_MUL =3; private static final int op_DIV =4; /** * Launch the application. * @param args */ public static void main(String[] args) { try { Test window = new Test(); window.open(); } catch (Exception e) { e.printStackTrace(); } } //TODO 把文本設(shè)置在文本框的右邊:在 text 屬性的 style 的 align 里設(shè)置。 /** * Open the window. */ public void open() { Display display = Display.getDefault(); createContents(); shell.open(); shell.layout(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } } /** * Create contents of the window. */ protected void createContents() { shell = new Shell(); shell.setImage(SWTResourceManager.getImage("E://Design//nexus4full(96 96)//circle.png")); shell.setSize(240, 297); shell.setText("SWT Application"); shell.setLayout(new GridLayout(4, false)); text = new Text(shell, SWT.BORDER | SWT.RIGHT); GridData gd_text = new GridData(SWT.FILL, SWT.FILL, true, false, 4, 1); gd_text.heightHint = 36; gd_text.widthHint = 424; text.setLayoutData(gd_text); Button button = new Button(shell, SWT.NONE); button.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { text.setText(text.getText() + "7"); if (text.getText().startsWith("0")){ text.setText ( text.getText().substring ( 1, text.getText().length( ) ) ) ; } } }); GridData gd_button = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1); gd_button.heightHint = 37; gd_button.widthHint = 51; button.setLayoutData(gd_button); button.setText("7"); Button button_1 = new Button(shell, SWT.NONE); button_1.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { text.setText(text.getText() + "8"); if (text.getText().startsWith("0")){ text.setText ( text.getText().substring ( 1, text.getText().length( ) ) ) ; } } }); GridData gd_button_1 = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1); gd_button_1.heightHint = 37; gd_button_1.widthHint = 51; button_1.setLayoutData(gd_button_1); button_1.setText("8"); Button button_2 = new Button(shell, SWT.NONE); GridData gd_button_2 = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1); gd_button_2.heightHint = 37; gd_button_2.widthHint = 51; button_2.setLayoutData(gd_button_2); button_2.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { text.setText(text.getText() + "9"); if (text.getText().startsWith("0")){ text.setText ( text.getText().substring ( 1, text.getText().length( ) ) ) ; } } }); button_2.setText("9"); Button button_10 = new Button(shell, SWT.NONE); GridData gd_button_10 = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1); gd_button_10.widthHint = 46; gd_button_10.heightHint = 40; button_10.setLayoutData(gd_button_10); button_10.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { op = op_ADD; num1 =Double.parseDouble (text.getText() ); text.setText("0"); } }); button_10.setText("+"); Button button_3 = new Button(shell, SWT.NONE); button_3.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { text.setText(text.getText() + "4"); if (text.getText().startsWith("0")){ text.setText ( text.getText().substring ( 1, text.getText().length( ) ) ) ; } } }); GridData gd_button_3 = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1); gd_button_3.heightHint = 37; gd_button_3.widthHint = 51; button_3.setLayoutData(gd_button_3); button_3.setText("4"); Button button_4 = new Button(shell, SWT.NONE); button_4.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { text.setText(text.getText() + "5"); if (text.getText().startsWith("0")){ text.setText ( text.getText().substring ( 1, text.getText().length( ) ) ) ; } } }); GridData gd_button_4 = new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1); gd_button_4.heightHint = 33; button_4.setLayoutData(gd_button_4); button_4.setText("5"); Button button_5 = new Button(shell, SWT.NONE); button_5.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { text.setText(text.getText() + "6"); if (text.getText().startsWith("0")){ text.setText ( text.getText().substring ( 1, text.getText().length( ) ) ) ; } } }); GridData gd_button_5 = new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1); gd_button_5.heightHint = 35; button_5.setLayoutData(gd_button_5); button_5.setText("6"); Button button_11 = new Button(shell, SWT.NONE); button_11.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { op = op_SUB ; num1 =Double.parseDouble (text.getText() ); //num1 = Integer.parseInt ( text.getText() ); text.setText("0"); } }); button_11.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1)); button_11.setText("-"); Button button_6 = new Button(shell, SWT.NONE); button_6.setFont(SWTResourceManager.getFont("微軟雅黑", 11, SWT.NORMAL)); button_6.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { //TODO 處理按下 1 后顯示 01 的問(wèn)題 text.setText (text.getText() + "1"); if (text.getText().startsWith("0")){ text.setText ( text.getText().substring (1, text.getText().length( ) ) ) ; } //FIXME 按下后變?yōu)?.1// else if(text.getText().startsWith("0.")){// text.setText ( text.getText() +"1") ;// } } }); GridData gd_button_6 = new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1); gd_button_6.heightHint = 37; gd_button_6.widthHint = 51; button_6.setLayoutData(gd_button_6); button_6.setText("1"); Button button_7 = new Button(shell, SWT.NONE); button_7.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { text.setText(text.getText() + "2"); if (text.getText().startsWith("0")){ text.setText ( text.getText().substring ( 1, text.getText().length( ) ) ) ; } } }); button_7.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1)); button_7.setText("2"); Button button_8 = new Button(shell, SWT.NONE); button_8.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { text.setText(text.getText() + "3"); if (text.getText().startsWith("0")){ text.setText ( text.getText().substring ( 1, text.getText().length( ) ) ) ; } } }); button_8.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1)); button_8.setText("3"); Button button_12 = new Button(shell, SWT.NONE); button_12.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { op = op_MUL; num1 =Double.parseDouble (text.getText() ); //num1 = Integer.parseInt ( text.getText() ); text.setText("0"); } }); button_12.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1)); button_12.setText("/u00D7"); Button btnC = new Button(shell, SWT.NONE); btnC.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { text.setText("0"); } }); btnC.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1)); btnC.setText("C"); Button button_9 = new Button(shell, SWT.NONE); button_9.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { text.setText(text.getText() + "0");// if (text.getText().startsWith("0")){// text.setText ( text.getText().substring ( 1, text.getText().length( ) ) ) ;// } } }); GridData gd_button_9 = new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1); gd_button_9.heightHint = 37; gd_button_9.widthHint = 51; button_9.setLayoutData(gd_button_9); button_9.setText("0"); Button button_14 = new Button(shell, SWT.NONE); button_14.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { text.setText (text.getText() + "."); //FIXME 直接按下 . 的話前面沒(méi)有變?yōu)?0 . } }); button_14.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1)); button_14.setText("."); Button button_13 = new Button(shell, SWT.NONE); button_13.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { op = op_DIV; num1 =Double.parseDouble (text.getText() ); //num1 = Integer.parseInt( text.getText() ); text.setText("0"); } }); button_13.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1)); button_13.setText("/u00F7"); Button button_15 = new Button(shell, SWT.NONE); button_15.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { double result ; //TODO 字符串 轉(zhuǎn) double num2 =Double.parseDouble ( text.getText() ); switch ( op) { case op_ADD: result = num1+num2; text.setText( result +""); //FIXME java中計(jì)算浮點(diǎn)數(shù)的時(shí)候不精確,如:0.3 * 3 //需要使用 java 的大數(shù)類 BigDecimal。 //TODO 處理浮點(diǎn)數(shù)運(yùn)算后結(jié)果多余顯示的“.0” if (text.getText().endsWith("0")) text.setText(text.getText().substring(0,text.getText().length()-2)); break; case op_SUB: result = num1 - num2; text.setText( result +""); if (text.getText().endsWith("0")) text.setText(text.getText().substring(0,text.getText().length()-2)); break; case op_MUL: result = num1 * num2; text.setText( result +""); if (text.getText().endsWith("0")) text.setText(text.getText().substring(0,text.getText().length()-2)); break; case op_DIV: result = num1 / num2; text.setText( result +""); if (text.getText().endsWith("0")) text.setText(text.getText().substring(0,text.getText().length()-2)); break; default: break; } } }); GridData gd_button_15 = new GridData(SWT.FILL, SWT.FILL, false, false, 4, 1); gd_button_15.heightHint = 34; button_15.setLayoutData(gd_button_15); button_15.setText("="); }}View Code
接下來(lái)要做的就是把它給打包,讓他脫離eclipse運(yùn)行了,現(xiàn)在還什么都不懂,加油!!
eclipse --> extport --> runable jar --> ....--> finish.新建一個(gè)bat文件,編輯--> java -jar Calcultor.jar.用bat的方式來(lái)運(yùn)行。
參考:
http://scorpiomiracle.VEvb.com/blog/630358
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注