/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package test1exp; import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * * @author jcmt */ public class Addition extends JFrame implements ActionListener { private JButton bButton; private JButton cButton; private JFrame bwindow; private JPanel panel; private JTextField text; private Dog bud; private JLabel dogsAge; private JLabel label3; private Integer n; private int ck=0, aP=0; public Addition() { final int WINDOW_WIDTH = 350; final int WINDOW_HEIGHT = 250; n = new Integer(0); bwindow = new JFrame("Sun"); /* bwindow.setTitle("a blue? window");*/ bwindow.setSize(WINDOW_WIDTH, WINDOW_HEIGHT); bwindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); bwindow.setBackground(Color.YELLOW); /* JLabel label = new JLabel("Enter first integer: "); JTextField text = new JTextField(10); JButton button = new JButton("Calculate"); JPanel panel = new JPanel(); panel.add(label); panel.add(text); panel.add(button); bwindow.add(panel); * */ bwindow.setVisible(true); } public void AddBWindow( String[] args) { final int WINDOW_WIDTH = 350; final int WINDOW_HEIGHT = 250; bwindow = new JFrame("Sky - a blue window"); /*bwindow.setTitle("a blue? window"); */ bwindow.setBackground(Color.BLUE); bwindow.setSize(WINDOW_WIDTH, WINDOW_HEIGHT); bwindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JLabel label = new JLabel("Enter first integer: "); text = new JTextField(10); label3 = new JLabel(); bButton = new JButton("Square me"); cButton = new JButton("Cube me"); Dog bud = new Dog("Buddy"); Integer ageinfo = bud.getAge(); /*dogsAge = new JLabel(); */ JLabel label2 = new JLabel(" The dog's age is "+ ageinfo); panel = new JPanel(); panel.add(label); panel.add(text); panel.add(label3); panel.add(bButton); panel.add(cButton); panel.add(label2); bButton.setActionCommand("square"); cButton.setActionCommand("cube"); /*bButton.ButtonHandler bHandler = new ButtonHandler();*/ bButton.addActionListener(this); cButton.addActionListener(this); /* ButtonHandler cHandler = new ButtonHandler(); cButton.addActionListener(cHandler); */ bwindow.add(panel); bwindow.setVisible(true); /* String firstNumber = JOptionPane.showInputDialog("Enter first integer: "); String secondNumber = JOptionPane.showInputDialog("Enter second integer: "); int number1 = Integer.parseInt(firstNumber); int number2 = Integer.parseInt(secondNumber); */ } private int checkn() { n = 0; ck += 1; /*System.out.println("Entering checkn - iteration "+ ck);*/ try { n = Integer.parseInt(text.getText()); } catch (Exception exc) { System.out.println("Something wrong with input value "); } finally { if (n == 0) { label3.setText(" Your input was either 0 or invalid "); } else { label3.setText(" Your input was "+ n +" "); } } return n; } /* private class Bbc extends ActionListener */ @Override public void actionPerformed(ActionEvent e) { aP += 1; /*System.out.println("Entering actPerf - iteration "+ aP);*/ int k = checkn(); if ("square".equals(e.getActionCommand())) { JOptionPane.showMessageDialog(Addition.this, String.format("Your square is %d",k*k)); } else if ("cube".equals(e.getActionCommand())) { JOptionPane.showMessageDialog(Addition.this, String.format("Your cube is %d",k*k*k)); } } }