Java / SWT GUI Project on Calculator
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.layout.FillLayout;
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.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class Main {
public class calculator {
private Display display;
private Shell shell;
private Text txt1;
private Text txt2;
private Text txtOutput;
private Button btnClear;
private Button btnSubmit;
private Label lbl1;
private Label lbl2;
private Label lbl3;
private Group grpStart;
private ScrolledComposite scrolledComposite;
private Composite shellComposite;
public calculator() {
display = new Display();
shell = new Shell(display);
shell.setSize(700, 400);
shell.setText("Calculator");
shell.setLayout(new FillLayout());
scrolledComposite = new ScrolledComposite(shell, SWT.H_SCROLL | SWT.V_SCROLL);
scrolledComposite.setExpandVertical(true);
scrolledComposite.setExpandHorizontal(true);
shellComposite = new Composite(scrolledComposite, SWT.NONE);
shellComposite.setLayout(new GridLayout(2, false));
scrolledComposite.setContent(shellComposite);
scrolledComposite.addListener(SWT.Resize, new Listener() {
public void handleEvent(Event event) {
scrolledComposite.setMinSize(shellComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
}
});
grpStart = new Group(shellComposite, SWT.NONE);
grpStart.setText("Input Number in Text Box");
grpStart.setLayout(new GridLayout(2, false));
GridData gd_grpStart = new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1);
gd_grpStart.heightHint = 213;
gd_grpStart.widthHint = 374;
grpStart.setLayoutData(gd_grpStart);
lbl1 = new Label(grpStart, SWT.NONE);
lbl1.setText("Enter a : ");
txt1 = new Text(grpStart, SWT.BORDER);
txt1.setTextLimit(30);
lbl2 = new Label(grpStart, SWT.NONE);
lbl2.setText("Enter b : ");
txt2 = new Text(grpStart, SWT.BORDER);
txt2.setTextLimit(30);
lbl3 = new Label(grpStart, SWT.NONE);
lbl3.setText("Output : ");
txtOutput = new Text(grpStart, SWT.READ_ONLY | SWT.BORDER);
txtOutput.setTextLimit(30);
btnSubmit = new Button(grpStart, SWT.NONE);
btnSubmit.setToolTipText("Schedule");
btnSubmit.setText("Submit");// 229e");
btnClear = new Button(grpStart, SWT.NONE);
btnClear.setToolTipText("Schedule");
btnClear.setText("Clear");// 229e");
btnSubmit.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent e) {
System.out.println("button clicked");
System.out.println(txt1.getText());
System.out.println(txt2.getText());
int sum = Integer.parseInt(txt1.getText()) + Integer.parseInt(txt2.getText());
txtOutput.setText(Integer.toString(sum));
}
});
btnClear.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent e) {
txtOutput.setText("");
txt1.setText("");
txt2.setText("");
}
});
btnClear.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent e) {
System.out.println("button clicked");
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
public Main() {
new calculator();
}
public static void main(String[] args) {
new Main();
}
}