Object Oriented C++

JOptionPane.showMessageDialog(null,”hello world….  “); 

HELLO WORLD

———————————

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;
import java.awt.Color;

public class AddTwoNumbers {

private JFrame frame;
private JTextField textField1;
private JTextField textField2;
private JTextField textFieldAns;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
AddTwoNumbers window = new AddTwoNumbers();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the application.
*/
public AddTwoNumbers() {
initialize();
}

/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.getContentPane().setBackground(new Color(143, 188, 143));
frame.getContentPane().setForeground(new Color(153, 0, 0));
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);

textField1 = new JTextField();
textField1.setBackground(new Color(220, 220, 220));
textField1.setFont(new Font(“Tahoma”, Font.BOLD, 25));
textField1.setBounds(10, 11, 170, 55);
frame.getContentPane().add(textField1);
textField1.setColumns(10);

textField2 = new JTextField();
textField2.setBackground(new Color(220, 220, 220));
textField2.setFont(new Font(“Tahoma”, Font.BOLD, 25));
textField2.setColumns(10);
textField2.setBounds(202, 11, 170, 55);
frame.getContentPane().add(textField2);

textFieldAns = new JTextField();
textFieldAns.setBackground(new Color(220, 220, 220));
textFieldAns.setFont(new Font(“Tahoma”, Font.BOLD, 25));
textFieldAns.setBounds(202, 151, 170, 55);
frame.getContentPane().add(textFieldAns);
textFieldAns.setColumns(10);

JLabel lblNewLabel = new JLabel(“Αποτέλεσμα”);
lblNewLabel.setFont(new Font(“Tahoma”, Font.BOLD, 22));
lblNewLabel.setBounds(37, 167, 143, 23);
frame.getContentPane().add(lblNewLabel);

JButton btnAdd = new JButton(“+”);
btnAdd.setFont(new Font(“Tahoma”, Font.BOLD, 13));
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {

int num1,num2,ans;
num1=Integer.parseInt(textField1.getText());
num2=Integer.parseInt(textField2.getText());

ans=num1+num2;
textFieldAns.setText(Integer.toString(ans));
}
catch(Exception e1) {
JOptionPane.showMessageDialog(null,”Enter Valid Number”);
}
}
});
btnAdd.setBounds(10, 98, 89, 23);
frame.getContentPane().add(btnAdd);

JButton btnAdd_1 = new JButton(“-“);
btnAdd_1.setFont(new Font(“Tahoma”, Font.BOLD, 13));
btnAdd_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {

int num1,num2,ans;
num1=Integer.parseInt(textField1.getText());
num2=Integer.parseInt(textField2.getText());

ans=num1-num2;
textFieldAns.setText(Integer.toString(ans));
}
catch(Exception e1) {
JOptionPane.showMessageDialog(null,”Enter Valid Number”);
}

}
});
btnAdd_1.setBounds(109, 98, 89, 23);
frame.getContentPane().add(btnAdd_1);

JButton btnAdd_2 = new JButton(“*”);
btnAdd_2.setFont(new Font(“Tahoma”, Font.BOLD, 13));
btnAdd_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {

int num1,num2,ans;
num1=Integer.parseInt(textField1.getText());
num2=Integer.parseInt(textField2.getText());

ans=num1*num2;
textFieldAns.setText(Integer.toString(ans));
}
catch(Exception e1) {
JOptionPane.showMessageDialog(null,”Enter Valid Number”);
}
}
});
btnAdd_2.setBounds(208, 98, 89, 23);
frame.getContentPane().add(btnAdd_2);

JButton btnAdd_3 = new JButton(“/”);
btnAdd_3.setFont(new Font(“Tahoma”, Font.BOLD, 13));
btnAdd_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {

int num1,num2,ans;
num1=Integer.parseInt(textField1.getText());
num2=Integer.parseInt(textField2.getText());

ans=num1/num2;
textFieldAns.setText(Integer.toString(ans));
}
catch(Exception e1) {
JOptionPane.showMessageDialog(null,”Enter Valid Number”);
}
}
});
btnAdd_3.setBounds(301, 98, 89, 23);
frame.getContentPane().add(btnAdd_3);
}
}

ΠΡΑΞΕΙΣ ΜΕ ΑΡΙΘΜΟΥΣ

———————————

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.Font;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JEditorPane;
import javax.swing.border.LineBorder;
import java.awt.Color;
import javax.swing.SwingConstants;
import javax.swing.JLabel;

public class Example2 {

private JFrame frame;
private JTextField num2;
private JTextField num1;
private JEditorPane editorPane;
private JButton btnNewButton;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Example2 window = new Example2();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the application.
*/
public Example2() {
initialize();
}

/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 367, 560);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);

btnNewButton = new JButton(“C”);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
num2.setText(null);
num1.setText(null);
actionRecived.setText(null);
}
});
btnNewButton.setFont(new Font(“Tahoma”, Font.BOLD, 18));
btnNewButton.setBounds(10, 122, 70, 55);
frame.getContentPane().add(btnNewButton);

JButton btnNewButton_1 = new JButton(“7”);
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
num1.setText(num1.getText()+7);
}
});
btnNewButton_1.setFont(new Font(“Tahoma”, Font.BOLD, 18));
btnNewButton_1.setBounds(10, 177, 70, 55);
frame.getContentPane().add(btnNewButton_1);

JButton btnNewButton_2 = new JButton(“4”);
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
num1.setText(num1.getText()+4);
}
});
btnNewButton_2.setFont(new Font(“Tahoma”, Font.BOLD, 18));
btnNewButton_2.setBounds(10, 234, 70, 55);
frame.getContentPane().add(btnNewButton_2);

JButton btnNewButton_3 = new JButton(“1”);
btnNewButton_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
num1.setText(num1.getText()+1);
}
});
btnNewButton_3.setFont(new Font(“Tahoma”, Font.BOLD, 18));
btnNewButton_3.setBounds(10, 292, 70, 55);
frame.getContentPane().add(btnNewButton_3);

JButton btnC = new JButton(“0”);
btnC.setFont(new Font(“Tahoma”, Font.BOLD, 18));
btnC.setBounds(83, 122, 70, 55);
frame.getContentPane().add(btnC);

JButton btnNewButton_5 = new JButton(“8”);
btnNewButton_5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
num1.setText(num1.getText()+8);
}
});
btnNewButton_5.setFont(new Font(“Tahoma”, Font.BOLD, 18));
btnNewButton_5.setBounds(83, 177, 70, 55);
frame.getContentPane().add(btnNewButton_5);

JButton btnNewButton_6 = new JButton(“5”);
btnNewButton_6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
num1.setText(num1.getText()+5);
}
});
btnNewButton_6.setFont(new Font(“Tahoma”, Font.BOLD, 18));
btnNewButton_6.setBounds(83, 234, 70, 55);
frame.getContentPane().add(btnNewButton_6);

JButton btnNewButton_7 = new JButton(“2”);
btnNewButton_7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
num1.setText(num1.getText()+2);
}
});
btnNewButton_7.setFont(new Font(“Tahoma”, Font.BOLD, 18));
btnNewButton_7.setBounds(83, 292, 70, 55);
frame.getContentPane().add(btnNewButton_7);

JButton btnNewButton_4 = new JButton(“=”);
btnNewButton_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
double one=Double.parseDouble(num1.getText());
double two=Double.parseDouble(num2.getText());
if (actionRecived.getText().equals(“add”))
{
num2.setText(String.valueOf(one+two));
}
if (actionRecived.getText().equals(“sub”))
{

num2.setText(String.valueOf(one-two));
}
if (actionRecived.getText().equals(“multi”))
{

num2.setText(String.valueOf(one*two));
}

if (actionRecived.getText().equals(“div”))
{

num2.setText(String.valueOf(one/two));
}

}
});
btnNewButton_4.setFont(new Font(“Tahoma”, Font.BOLD, 18));
btnNewButton_4.setBounds(156, 122, 70, 55);
frame.getContentPane().add(btnNewButton_4);

JButton btnNewButton_9 = new JButton(“9”);
btnNewButton_9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
num1.setText(num1.getText()+9);

}
});
btnNewButton_9.setFont(new Font(“Tahoma”, Font.BOLD, 18));
btnNewButton_9.setBounds(156, 177, 70, 55);
frame.getContentPane().add(btnNewButton_9);

JButton btnNewButton_10 = new JButton(“6”);
btnNewButton_10.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
num1.setText(num1.getText()+6);
}
});
btnNewButton_10.setFont(new Font(“Tahoma”, Font.BOLD, 18));
btnNewButton_10.setBounds(156, 234, 70, 55);
frame.getContentPane().add(btnNewButton_10);

JButton btnNewButton_11 = new JButton(“3”);
btnNewButton_11.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
num1.setText(num1.getText()+3);
}
});
btnNewButton_11.setFont(new Font(“Tahoma”, Font.BOLD, 18));
btnNewButton_11.setBounds(156, 292, 70, 55);
frame.getContentPane().add(btnNewButton_11);

JButton btnNewButton_12 = new JButton(“+”);
btnNewButton_12.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
num2.setText(num1.getText());
num1.setText(null);
actionRecived.setText(“add”);
}
});
btnNewButton_12.setFont(new Font(“Tahoma”, Font.BOLD, 18));
btnNewButton_12.setBounds(229, 122, 70, 55);
frame.getContentPane().add(btnNewButton_12);

JButton btnNewButton_13 = new JButton(“-“);
btnNewButton_13.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
num2.setText(num1.getText());
num1.setText(null);
actionRecived.setText(“sub”);

}
});
btnNewButton_13.setFont(new Font(“Tahoma”, Font.BOLD, 18));
btnNewButton_13.setBounds(229, 177, 70, 55);
frame.getContentPane().add(btnNewButton_13);

JButton btnNewButton_14 = new JButton(“*”);
btnNewButton_14.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
num2.setText(num1.getText());
num1.setText(null);
actionRecived.setText(“multi”);
}
});
btnNewButton_14.setFont(new Font(“Tahoma”, Font.BOLD, 18));
btnNewButton_14.setBounds(229, 234, 70, 55);
frame.getContentPane().add(btnNewButton_14);

JButton btnNewButton_15 = new JButton(“/”);
btnNewButton_15.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
num2.setText(num1.getText());
num1.setText(null);
actionRecived.setText(“div”);
}
});
btnNewButton_15.setFont(new Font(“Tahoma”, Font.BOLD, 18));
btnNewButton_15.setBounds(229, 292, 70, 55);
frame.getContentPane().add(btnNewButton_15);

editorPane = new JEditorPane();
editorPane.setFont(new Font(“Tahoma”, Font.BOLD, 18));
editorPane.setBorder(new LineBorder(new Color(0, 0, 0)));
editorPane.setEditable(false);
editorPane.setBounds(10, 11, 289, 100);
frame.getContentPane().add(editorPane);

num2 = new JTextField();
num2.setHorizontalAlignment(SwingConstants.RIGHT);
num2.setFont(new Font(“Tahoma”, Font.BOLD, 18));
num2.setBorder(null);
num2.setBounds(23, 66, 264, 36);
frame.getContentPane().add(num2);
num2.setColumns(10);

num1 = new JTextField();
num1.setHorizontalAlignment(SwingConstants.RIGHT);
num1.setFont(new Font(“Tahoma”, Font.BOLD, 15));
num1.setBorder(null);
num1.setBounds(47, 23, 240, 36);
frame.getContentPane().add(num1);
num1.setColumns(10);

actionRecived = new JLabel(“”);
actionRecived.setFont(new Font(“Tahoma”, Font.BOLD, 18));
actionRecived.setBounds(10, 467, 158, 43);
frame.getContentPane().add(actionRecived);
}
private JLabel actionRecived;
}

Calculator

cal fotou1