我想用Eclipse做一个简单的计算器

softbank1006 2008-07-28 06:26:20
package sample.swt;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class CalendarMain {

/**
* @param args
*/
private Shell shell;
private Text displayText;


//>>> CalendarTable  ……(A)

private Shell getCalendarShell(Display display){

shell = new Shell(display);

shell.setText("簡易计算器");
shell.setSize(350,250);

shell.setLayout(new GridLayout(1, true)); // ……(1)

// Composite
Composite ctlComp = new Composite(shell, SWT.NO_FOCUS); // ……(2)
ctlComp.setLayout(new GridLayout(2, true)); // ……(3)
GridData compData = new GridData();
compData.horizontalAlignment = GridData.FILL;
compData.grabExcessHorizontalSpace = true; // ……(4)
ctlComp.setLayoutData(compData);

// text
displayText = new Text(ctlComp, SWT.SINGLE | SWT.BORDER); // ……(5)
GridData textData = new GridData(GridData.FILL_HORIZONTAL); // ……(6)
displayText.setLayoutData(textData);



// button Composite
Composite buttonComp = new Composite(ctlComp, SWT.NO_FOCUS );
buttonComp.setLayout(new GridLayout(3, true));
GridData buttonData = new GridData(GridData.FILL); // ……(7)
buttonData.horizontalAlignment = GridData.END; // ……(8)
buttonComp.setLayoutData(buttonData);

// button
Button button_seven = new Button(buttonComp, SWT.PUSH); // ……(9)
button_seven.setText("7");

Button button_eight = new Button(buttonComp, SWT.PUSH);
button_eight.setText("8");

Button button_nine = new Button(buttonComp, SWT.PUSH);
button_nine.setText("9");


Button button_four = new Button(buttonComp, SWT.PUSH);
button_four.setText("4");

Button button_five = new Button(buttonComp, SWT.PUSH);
button_five.setText("5");


Button button_six = new Button(buttonComp, SWT.PUSH);
button_six.setText("6");


Button button_one= new Button(buttonComp, SWT.PUSH);
button_one.setText("1");

Button button_two = new Button(buttonComp, SWT.PUSH);
button_two.setText("2");

Button button_three = new Button(buttonComp, SWT.PUSH);
button_three.setText("3");


Button button_zero = new Button(buttonComp, SWT.PUSH);
button_zero.setText("0");

Button button_plus = new Button(buttonComp, SWT.PUSH);
button_plus.setText("+");

Button button_minus = new Button(buttonComp, SWT.PUSH);
button_minus.setText("-");

Button button_multiply = new Button(buttonComp, SWT.PUSH);
button_multiply.setText("×");

Button button_divide = new Button(buttonComp, SWT.PUSH);
button_divide.setText("÷");

Button button_equals = new Button(buttonComp, SWT.PUSH);
button_equals.setText("=");

Button button_cancel = new Button(buttonComp, SWT.PUSH);
button_cancel.setText("C");







return shell;

}

public static void main(String[] args) {
// TODO
Display display = new Display();
Shell shell = new CalendarMain().getCalendarShell(display);

//打开shell
shell.open();

while (!shell.isDisposed()){
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();

}

现在只有一个框,还显示按键。
怎样才能完成一个计算器呢?



...全文
627 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复

58,454

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧