Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // school work
- import java.awt.*; //using awt's layouts
- import java.awt.event.*; //using awt's event classes and listener interfaces
- import javax.swing.*; //using swing's components and container
- //a swing application extends from javax.swing.JFrame
- public class SwingArithmetics extends JFrame {
- private JTextField tfNumber1, tfNumber2, tfResult;
- private JButton btnAdd, btnSub, btnMulti, btnDiv, btnRemain, btnClear;
- private double number1, number2, result;
- //constructor to set up ui components and event handlers
- public SwingArithmetics() {
- //swing components should be added to the content-pane of the JFrame
- Container cp = getContentPane();
- //set this container to grid layout of 6 rows and 2 columns
- cp.setLayout(new GridLayout(6, 2, 10, 3));
- //components are added left-to-right, top-to-bottom
- cp.add(new JLabel("First Number "));
- tfNumber1 = new JTextField(10);
- tfNumber1.setHorizontalAlignment(JTextField.RIGHT);
- cp.add(tfNumber1);
- cp.add(new JLabel("Second Number "));
- tfNumber2 = new JTextField(10);
- tfNumber2.setHorizontalAlignment(JTextField.RIGHT);
- cp.add(tfNumber2);
- cp.add(new JLabel("Result "));
- tfResult = new JTextField(10);
- tfResult.setHorizontalAlignment(JTextField.RIGHT);
- cp.add(tfResult);
- btnAdd = new JButton("+");
- cp.add(btnAdd);
- btnAdd.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent evt) {
- number1 = Integer.parseInt(tfNumber1.getText());
- number2 = Integer.parseInt(tfNumber2.getText());
- result = number1 + number2; //to calculate the sum
- tfResult.setText(result + "");
- }
- });
- btnSub = new JButton("-");
- cp.add(btnSub);
- btnSub.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent evt) {
- number1 = Integer.parseInt(tfNumber1.getText());
- number2 = Integer.parseInt(tfNumber2.getText());
- result = number1 - number2;
- tfResult.setText(result + "");
- }
- });
- btnMulti = new JButton("*");
- cp.add(btnMulti);
- btnMulti.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent evt) {
- number1 = Integer.parseInt(tfNumber1.getText());
- number2 = Integer.parseInt(tfNumber2.getText());
- result = number1 * number2;
- tfResult.setText(result + "");
- }
- });
- btnDiv = new JButton("/");
- cp.add(btnDiv);
- btnDiv.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent evt) {
- number1 = Integer.parseInt(tfNumber1.getText());
- number2 = Integer.parseInt(tfNumber2.getText());
- result = number1 / number2;
- tfResult.setText(result + "");
- }
- });
- btnRemain = new JButton("%");
- cp.add(btnRemain);
- btnRemain.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent evt) {
- number1 = Integer.parseInt(tfNumber1.getText());
- number2 = Integer.parseInt(tfNumber2.getText());
- result = number1 % number2;
- tfResult.setText(result + "");
- }
- });
- btnClear = new JButton("CLEAR");
- cp.add(btnClear);
- btnClear.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent evt) {
- tfNumber1.setText(""); //to clear num 1 field
- tfNumber2.setText(""); //to clear num 2 field
- tfResult.setText(""); //to clear result field
- }
- });
- setDefaultCloseOperation(EXIT_ON_CLOSE); // for the "window-close" button
- setTitle("Swing Arithmetics");
- setSize(300, 170);
- setVisible(true);
- }
- // The entry main() method
- public static void main(String[] args) {
- // For thread safety, use the event-dispatching thread to construct UI
- javax.swing.SwingUtilities.invokeLater(new Runnable() {
- @Override
- public void run() {
- new SwingArithmetics(); // Let the constructor does the job
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement