Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- This class is the stand-alone class .it does all the work
- N. Gattuso -- 01/13/2014
- ****************************************************************** */
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class MyWindowFrame extends JFrame {
- private Container pane;
- private JTextField textField;
- //The constructor
- public MyWindowFrame() {
- //create a pane to work in..
- pane=getContentPane();
- //set the layout of the pane to null
- pane.setLayout(null);
- JMenuBar menuBar = new JMenuBar();
- JMenu menuFile = new JMenu();
- JMenuItem menuFileExit = new JMenuItem();
- menuFile.setText("File");
- menuFileExit.setText("Exit");
- // Add action listener.for the menu button
- menuFileExit.addActionListener
- (
- new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- MyWindowFrame.this.windowClosed();
- }
- }
- );
- menuFile.add(menuFileExit);
- menuBar.add(menuFile);
- setTitle("Calculator");
- setJMenuBar(menuBar);
- setSize(new Dimension(600, 1000));
- JButton zero = new JButton("0");
- pane.add(zero);
- zero.setLocation(50, 850);
- zero.setSize(300, 100);
- zero.addActionListener
- (
- new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String text = textField.getText();
- zero.setText(text + "0");
- }
- }
- );
- JButton one= new JButton("1");
- pane.add(one);
- one.setLocation(50, 750);
- one.setSize(100, 100);
- one.addActionListener
- (
- new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String text = textField.getText();
- one.setText(text + "1");
- }
- }
- );
- JButton two= new JButton("2");
- pane.add(two);
- two.setLocation(150, 750);
- two.setSize(100, 100);
- two.addActionListener
- (
- new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String text = textField.getText();
- two.setText(text + "1");
- }
- }
- );
- JButton four = new JButton("4");
- pane.add(four);
- four.setLocation(50, 650);
- four.setSize(100, 100);
- four.addActionListener
- (
- new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String text = textField.getText();
- four.setText(text + "1");
- }
- }
- );
- JButton five = new JButton("5");
- pane.add(five);
- five.setLocation(150, 650);
- five.setSize(100, 100);
- five.addActionListener
- (
- new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String text = textField.getText();
- five.setText(text + "1");
- }
- }
- );
- JButton six = new JButton("6");
- pane.add(six);
- six.setLocation(250, 650);
- six.setSize(100, 100);
- six.addActionListener
- (
- new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String text = textField.getText();
- six.setText(text + "6");
- }
- }
- );
- JButton seven = new JButton("7");
- pane.add(seven);
- seven.setLocation(50, 550);
- seven.setSize(100, 100);
- seven.addActionListener
- (
- new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String text = textField.getText();
- seven.setText(text + "7");
- }
- }
- );
- JButton eight = new JButton("8");
- pane.add(eight);
- eight.setLocation(150, 550);
- eight.setSize(100, 100);
- eight.addActionListener
- (
- new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String text = textField.getText();
- eight.setText(text + "8");
- }
- }
- );
- JButton nine= new JButton("9");
- pane.add(nine);
- nine.setLocation(250, 550);
- nine.setSize(100, 100);
- nine.addActionListener
- (
- new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String text = textField.getText();
- nine.setText(text + "9");
- }
- }
- );
- JButton minus= new JButton("--");
- pane.add(minus);
- minus.setLocation(450, 650);
- minus.setSize(100, 100);
- minus.addActionListener
- (
- new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String text = textField.getText();
- minus.setText(text + "--");
- }
- }
- );
- JButton divide= new JButton("÷");
- pane.add(divide);
- divide.setLocation(450, 450);
- divide.setSize(100, 100);
- divide.addActionListener
- (
- new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String text = textField.getText();
- divide.setText(text + "÷");
- }
- }
- );
- JButton times= new JButton("x");
- pane.add(times);
- times.setLocation(450, 550);
- times.setSize(100, 100);
- times.addActionListener
- (
- new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String text = textField.getText();
- times.setText(text + "x");
- }
- }
- );
- JButton three= new JButton("3");
- pane.add(three);
- three.setLocation(250, 750);
- three.setSize(100, 100);
- three.addActionListener
- (
- new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String text = textField.getText();
- three.setText(text + "3");
- }
- }
- );
- JButton equals = new JButton("=");
- pane.add(equals);
- equals.setLocation(450, 850);
- equals.setSize(100, 100);
- equals.addActionListener
- (
- new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String text = textField.getText();
- equals.setText(text + "=");
- }
- }
- );
- JButton button1 = new JButton("+");
- pane.add(button1);
- button1.setLocation(450, 750);
- button1.setSize(100, 100);
- button1.addActionListener
- (
- new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String text = textField.getText();
- button1.setText(text + "+");
- }
- }
- );
- // Add window listener. to window
- this.addWindowListener
- (
- new WindowAdapter() {
- public void windowClosing(WindowEvent e) {
- MyWindowFrame.this.windowClosed();
- }
- }
- );
- }
- /**
- * Shutdown procedure when run as an application.
- */
- protected void windowClosed() {
- // TODO: Check if it is safe to close the application
- // Exit application.
- System.exit(0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement