Advertisement
EugenyB

Lab4Java

Nov 15th, 2014
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.62 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package gui;
  7.  
  8. import lab3.Logic;
  9.  
  10. /**
  11.  *
  12.  * @author Eugeny
  13.  */
  14. public class MainFrame extends javax.swing.JFrame {
  15.  
  16.     double[] arr;
  17.    
  18.     /**
  19.      * Creates new form MainFrame
  20.      */
  21.     public MainFrame() {
  22.         initComponents();
  23.     }
  24.  
  25.     /**
  26.      * This method is called from within the constructor to initialize the form.
  27.      * WARNING: Do NOT modify this code. The content of this method is always
  28.      * regenerated by the Form Editor.
  29.      */
  30.     @SuppressWarnings("unchecked")
  31.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  32.     private void initComponents() {
  33.  
  34.         jPanel1 = new javax.swing.JPanel();
  35.         jLabel1 = new javax.swing.JLabel();
  36.         jTextField1 = new javax.swing.JTextField();
  37.         jButton1 = new javax.swing.JButton();
  38.         jPanel2 = new javax.swing.JPanel();
  39.         jLabel2 = new javax.swing.JLabel();
  40.         jLabel3 = new javax.swing.JLabel();
  41.         jLabel4 = new javax.swing.JLabel();
  42.         jLabel5 = new javax.swing.JLabel();
  43.         jLabel6 = new javax.swing.JLabel();
  44.         jLabel7 = new javax.swing.JLabel();
  45.  
  46.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  47.  
  48.         jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
  49.  
  50.         jLabel1.setText("Массив");
  51.  
  52.         jButton1.setText("Вычислить");
  53.         jButton1.addActionListener(new java.awt.event.ActionListener() {
  54.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  55.                 jButton1ActionPerformed(evt);
  56.             }
  57.         });
  58.  
  59.         javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
  60.         jPanel1.setLayout(jPanel1Layout);
  61.         jPanel1Layout.setHorizontalGroup(
  62.             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  63.             .addGroup(jPanel1Layout.createSequentialGroup()
  64.                 .addContainerGap()
  65.                 .addComponent(jLabel1)
  66.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  67.                 .addComponent(jTextField1)
  68.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  69.                 .addComponent(jButton1)
  70.                 .addContainerGap())
  71.         );
  72.         jPanel1Layout.setVerticalGroup(
  73.             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  74.             .addGroup(jPanel1Layout.createSequentialGroup()
  75.                 .addContainerGap()
  76.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  77.                     .addComponent(jLabel1)
  78.                     .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  79.                     .addComponent(jButton1))
  80.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  81.         );
  82.  
  83.         jLabel2.setText("nMin=");
  84.  
  85.         jLabel3.setText(" ");
  86.  
  87.         jLabel4.setText("nMax=");
  88.  
  89.         jLabel5.setText(" ");
  90.  
  91.         jLabel6.setText("Максимальное отрицательное = ");
  92.  
  93.         jLabel7.setText(" ");
  94.  
  95.         javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
  96.         jPanel2.setLayout(jPanel2Layout);
  97.         jPanel2Layout.setHorizontalGroup(
  98.             jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  99.             .addGroup(jPanel2Layout.createSequentialGroup()
  100.                 .addContainerGap()
  101.                 .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  102.                     .addGroup(jPanel2Layout.createSequentialGroup()
  103.                         .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  104.                             .addComponent(jLabel4)
  105.                             .addComponent(jLabel2))
  106.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  107.                         .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  108.                             .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  109.                             .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
  110.                     .addGroup(jPanel2Layout.createSequentialGroup()
  111.                         .addComponent(jLabel6)
  112.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  113.                         .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, 186, Short.MAX_VALUE)))
  114.                 .addContainerGap())
  115.         );
  116.         jPanel2Layout.setVerticalGroup(
  117.             jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  118.             .addGroup(jPanel2Layout.createSequentialGroup()
  119.                 .addContainerGap()
  120.                 .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  121.                     .addComponent(jLabel2)
  122.                     .addComponent(jLabel3))
  123.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  124.                 .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  125.                     .addComponent(jLabel4)
  126.                     .addComponent(jLabel5))
  127.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  128.                 .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  129.                     .addComponent(jLabel6)
  130.                     .addComponent(jLabel7))
  131.                 .addContainerGap(30, Short.MAX_VALUE))
  132.         );
  133.  
  134.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  135.         getContentPane().setLayout(layout);
  136.         layout.setHorizontalGroup(
  137.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  138.             .addGroup(layout.createSequentialGroup()
  139.                 .addContainerGap()
  140.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  141.                     .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  142.                     .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  143.                 .addContainerGap())
  144.         );
  145.         layout.setVerticalGroup(
  146.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  147.             .addGroup(layout.createSequentialGroup()
  148.                 .addContainerGap()
  149.                 .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  150.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  151.                 .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  152.                 .addContainerGap(134, Short.MAX_VALUE))
  153.         );
  154.  
  155.         pack();
  156.     }// </editor-fold>                        
  157.  
  158.     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  159.         inputArray();
  160.         Logic logic = new Logic();
  161.         int nMax = logic.nMax(arr);
  162.         int nMin = logic.nMin(arr);
  163.         jLabel3.setText(String.format("%d", nMin));
  164.         jLabel5.setText(String.format("%d", nMax));
  165.         double[] neg = logic.negative(arr);
  166.         int nMaxNeg = logic.nMax(neg);
  167.         double maxNeg = neg[nMaxNeg];
  168.         jLabel7.setText(String.format("%6.2f", maxNeg));
  169.     }                                        
  170.  
  171.     /**
  172.      * @param args the command line arguments
  173.      */
  174.     public static void main(String args[]) {
  175.         /* Set the Nimbus look and feel */
  176.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  177.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  178.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  179.          */
  180.         try {
  181.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  182.                 if ("Nimbus".equals(info.getName())) {
  183.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  184.                     break;
  185.                 }
  186.             }
  187.         } catch (ClassNotFoundException ex) {
  188.             java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  189.         } catch (InstantiationException ex) {
  190.             java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  191.         } catch (IllegalAccessException ex) {
  192.             java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  193.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  194.             java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  195.         }
  196.         //</editor-fold>
  197.  
  198.         /* Create and display the form */
  199.         java.awt.EventQueue.invokeLater(new Runnable() {
  200.             public void run() {
  201.                 new MainFrame().setVisible(true);
  202.             }
  203.         });
  204.     }
  205.  
  206.     // Variables declaration - do not modify                    
  207.     private javax.swing.JButton jButton1;
  208.     private javax.swing.JLabel jLabel1;
  209.     private javax.swing.JLabel jLabel2;
  210.     private javax.swing.JLabel jLabel3;
  211.     private javax.swing.JLabel jLabel4;
  212.     private javax.swing.JLabel jLabel5;
  213.     private javax.swing.JLabel jLabel6;
  214.     private javax.swing.JLabel jLabel7;
  215.     private javax.swing.JPanel jPanel1;
  216.     private javax.swing.JPanel jPanel2;
  217.     private javax.swing.JTextField jTextField1;
  218.     // End of variables declaration                  
  219.  
  220.     private void inputArray() {
  221.         String s = jTextField1.getText();
  222.         String[] split = s.split(" ");
  223.         arr = new double[split.length];
  224.         for (int i = 0; i < arr.length; i++) {
  225.             arr[i] = Double.parseDouble(split[i]);
  226.         }
  227.     }
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement