Expand|Select|Wrap|Line Numbers
- /*
- * blah.java
- *
- * Created on May 26, 2008, 9:35 AM
- */
- package blah;
- /**
- *
- * @author Ned
- */
- public class blah extends javax.swing.JFrame {
- /** Creates new form blah */
- public blah() {
- initComponents();
- }
- /** This method is called from within the constructor to
- * initialize the form.
- * WARNING: Do NOT modify this code. The content of this method is
- * always regenerated by the Form Editor.
- */
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">
- private void initComponents() {
- jButton1 = new javax.swing.JButton();
- jScrollPane1 = new javax.swing.JScrollPane();
- jTextArea1 = new javax.swing.JTextArea();
- jButton2 = new javax.swing.JButton();
- num1 = new javax.swing.JTextField();
- num2 = new javax.swing.JTextField();
- calculate_average = new javax.swing.JButton();
- average = new javax.swing.JLabel();
- jButton1.setText("jButton1");
- jButton1.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton1ActionPerformed(evt);
- }
- });
- jTextArea1.setColumns(20);
- jTextArea1.setRows(5);
- jScrollPane1.setViewportView(jTextArea1);
- jButton2.setText("jButton2");
- jButton2.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton2ActionPerformed(evt);
- }
- });
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- num1.setText("Number 1");
- num1.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- num1ActionPerformed(evt);
- }
- });
- num2.setText("Number 2");
- num2.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- num2ActionPerformed(evt);
- }
- });
- calculate_average.setText("Calculate Average");
- calculate_average.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- calculate_averageActionPerformed(evt);
- }
- });
- average.setText("Average");
- org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
- .add(layout.createSequentialGroup()
- .add(45, 45, 45)
- .add(num1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
- .add(num2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
- .add(calculate_average)
- .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
- .add(average)
- .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
- .add(layout.createSequentialGroup()
- .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
- .add(num1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
- .add(num2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
- .add(calculate_average)
- .add(average))
- .addContainerGap(271, Short.MAX_VALUE))
- );
- pack();
- }// </editor-fold>
- private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- }
- private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- }
- private void num1ActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- }
- private void num2ActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- }
- private void calculate_averageActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- int number1, number2, number_average;
- number1 = (int)((Double.parseDouble(num1.getText())));
- number2 = (int)((Double.parseDouble(num2.getText())));
- number_average = number1 + number2 / 2;
- average.setText("" + number_average);
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String args[]) {
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new blah().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JLabel average;
- private javax.swing.JButton calculate_average;
- private javax.swing.JButton jButton1;
- private javax.swing.JButton jButton2;
- private javax.swing.JScrollPane jScrollPane1;
- private javax.swing.JTextArea jTextArea1;
- private javax.swing.JTextField num1;
- private javax.swing.JTextField num2;
- // End of variables declaration
- }
Expand|Select|Wrap|Line Numbers
- package NumberAverager;
- import javax.swing.JFrame;
- import javax.swing.JButton;
- import javax.swing.JTextField;
- import javax.swing.JLabel;
- import java.awt.Container;
- import java.awt.FlowLayout;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- abstract class NumberAverager implements ActionListener
- {
- public static void main(String[] args)
- {
- //create and set the title for a new frame
- JFrame frame = new JFrame();
- frame.setTitle("Number Averager");
- //create a container
- Container contentPane = frame.getContentPane();
- //create a layout and apply it to the contentPane
- FlowLayout layout = new FlowLayout();
- frame.setLayout(layout);
- //create components
- final JTextField num1 = new JTextField("Number 1");
- final JTextField num2 = new JTextField("Number 2");
- JButton calculate_average = new JButton("Calculate average");
- final JLabel average = new JLabel("Average");
- //add the components
- contentPane.add(num1);
- contentPane.add(num2);
- contentPane.add(calculate_average);
- contentPane.add(average);
- //frame settings
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.pack();
- frame.setVisible(true);
- //actions performed
- calculate_average.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- int number1, number2, number_average;
- number1 = (int)((Double.parseDouble(num1.getText())));
- number2 = (int)((Double.parseDouble(num2.getText())));
- number_average = number1 + number2 / 2;
- average.setText("" + number_average);
- }
- });
- }
- }