用java写一个SWT应用程序窗口的计算器代码
时间: 2023-11-15 16:04:08 浏览: 110
小型计算器
以下是一个简单的SWT应用程序窗口计算器的Java代码:
```java
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class Calculator {
private Shell shell;
private Text displayText;
public Calculator(Display display) {
shell = new Shell(display);
shell.setText("Calculator");
shell.setLayout(new GridLayout(4, true));
// 创建显示文本区域
displayText = new Text(shell, SWT.RIGHT);
GridData gridData = new GridData(SWT.FILL, SWT.CENTER, true, false, 4, 1);
displayText.setLayoutData(gridData);
// 创建数字和运算符按钮
Button button0 = new Button(shell, SWT.PUSH);
button0.setText("0");
gridData = new GridData(SWT.FILL, SWT.CENTER, true, false);
button0.setLayoutData(gridData);
Button button1 = new Button(shell, SWT.PUSH);
button1.setText("1");
gridData = new GridData(SWT.FILL, SWT.CENTER, true, false);
button1.setLayoutData(gridData);
Button button2 = new Button(shell, SWT.PUSH);
button2.setText("2");
gridData = new GridData(SWT.FILL, SWT.CENTER, true, false);
button2.setLayoutData(gridData);
Button button3 = new Button(shell, SWT.PUSH);
button3.setText("3");
gridData = new GridData(SWT.FILL, SWT.CENTER, true, false);
button3.setLayoutData(gridData);
Button button4 = new Button(shell, SWT.PUSH);
button4.setText("4");
gridData = new GridData(SWT.FILL, SWT.CENTER, true, false);
button4.setLayoutData(gridData);
Button button5 = new Button(shell, SWT.PUSH);
button5.setText("5");
gridData = new GridData(SWT.FILL, SWT.CENTER, true, false);
button5.setLayoutData(gridData);
Button button6 = new Button(shell, SWT.PUSH);
button6.setText("6");
gridData = new GridData(SWT.FILL, SWT.CENTER, true, false);
button6.setLayoutData(gridData);
Button button7 = new Button(shell, SWT.PUSH);
button7.setText("7");
gridData = new GridData(SWT.FILL, SWT.CENTER, true, false);
button7.setLayoutData(gridData);
Button button8 = new Button(shell, SWT.PUSH);
button8.setText("8");
gridData = new GridData(SWT.FILL, SWT.CENTER, true, false);
button8.setLayoutData(gridData);
Button button9 = new Button(shell, SWT.PUSH);
button9.setText("9");
gridData = new GridData(SWT.FILL, SWT.CENTER, true, false);
button9.setLayoutData(gridData);
Button buttonPlus = new Button(shell, SWT.PUSH);
buttonPlus.setText("+");
gridData = new GridData(SWT.FILL, SWT.CENTER, true, false);
buttonPlus.setLayoutData(gridData);
Button buttonMinus = new Button(shell, SWT.PUSH);
buttonMinus.setText("-");
gridData = new GridData(SWT.FILL, SWT.CENTER, true, false);
buttonMinus.setLayoutData(gridData);
Button buttonMultiply = new Button(shell, SWT.PUSH);
buttonMultiply.setText("*");
gridData = new GridData(SWT.FILL, SWT.CENTER, true, false);
buttonMultiply.setLayoutData(gridData);
Button buttonDivide = new Button(shell, SWT.PUSH);
buttonDivide.setText("/");
gridData = new GridData(SWT.FILL, SWT.CENTER, true, false);
buttonDivide.setLayoutData(gridData);
Button buttonEquals = new Button(shell, SWT.PUSH);
buttonEquals.setText("=");
gridData = new GridData(SWT.FILL, SWT.CENTER, true, false);
buttonEquals.setLayoutData(gridData);
// 添加按钮事件监听器
SelectionAdapter buttonListener = new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
Button button = (Button) event.widget;
String buttonText = button.getText();
String displayString = displayText.getText();
displayText.setText(displayString + buttonText);
}
};
button0.addSelectionListener(buttonListener);
button1.addSelectionListener(buttonListener);
button2.addSelectionListener(buttonListener);
button3.addSelectionListener(buttonListener);
button4.addSelectionListener(buttonListener);
button5.addSelectionListener(buttonListener);
button6.addSelectionListener(buttonListener);
button7.addSelectionListener(buttonListener);
button8.addSelectionListener(buttonListener);
button9.addSelectionListener(buttonListener);
// 添加运算符按钮事件监听器
SelectionAdapter operatorListener = new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
Button button = (Button) event.widget;
String buttonText = button.getText();
String displayString = displayText.getText();
displayText.setText(displayString + " " + buttonText + " ");
}
};
buttonPlus.addSelectionListener(operatorListener);
buttonMinus.addSelectionListener(operatorListener);
buttonMultiply.addSelectionListener(operatorListener);
buttonDivide.addSelectionListener(operatorListener);
// 添加等于号按钮事件监听器
buttonEquals.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
String displayString = displayText.getText();
String[] tokens = displayString.split(" ");
int result = Integer.parseInt(tokens[0]);
for (int i = 1; i < tokens.length; i += 2) {
String operator = tokens[i];
int operand = Integer.parseInt(tokens[i + 1]);
if (operator.equals("+")) {
result += operand;
} else if (operator.equals("-")) {
result -= operand;
} else if (operator.equals("*")) {
result *= operand;
} else if (operator.equals("/")) {
result /= operand;
}
}
displayText.setText(Integer.toString(result));
}
});
// 设置窗口大小和打开窗口
shell.setSize(200, 200);
shell.open();
}
public void run() {
Display display = Display.getDefault();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
public static void main(String[] args) {
Display display = new Display();
Calculator calculator = new Calculator(display);
calculator.run();
}
}
```
这个窗口计算器包含数字、运算符、等于号按钮和一个文本区域来显示计算结果。每当用户点击一个数字或运算符按钮时,它们都将显示在文本区域中。当用户点击等于号按钮时,程序将从文本区域中提取出数字和运算符,计算结果并将其显示在文本区域中。
阅读全文