JAVA 가계부 #2

Programming/JAVA,JSP 2014. 1. 14. 20:15 Posted by TanSanC
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.BufferedReader;

import java.io.FileReader;

import java.io.FileWriter;

import java.util.StringTokenizer;

 

import javax.swing.JComboBox;

import javax.swing.RowSorter;

import javax.swing.table.TableModel;

import javax.swing.table.TableRowSorter;

 

/*

 * To change this template, choose Tools | Templates

 * and open the template in the editor.

 */

 

/**

 *

 * @author Suser

 */

public class NewJFrame extends javax.swing.JFrame implements ActionListener {

 

    /**

     * Creates new form NewJFrame

     */

    public NewJFrame() {

       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">//GEN-BEGIN:initComponents

    private void initComponents() {

 

       jLabel1 = new javax.swing.JLabel();

      String[] monthArray = { "1", "2", "3", "4", "5", "6", "7", "8",

              "9", "10", "11", "12" };

       jComboBox1 = new JComboBox(monthArray);

       jComboBox1.addActionListener(this);

 

       jScrollPane1 = new javax.swing.JScrollPane();

       jTable1 = new javax.swing.JTable();

       jScrollPane2 = new javax.swing.JScrollPane();

       jTable2 = new javax.swing.JTable();

       jLabel2 = new javax.swing.JLabel();

       jButton1 = new javax.swing.JButton();

       jButton2 = new javax.swing.JButton();

       jButton3 = new javax.swing.JButton();

       jButton4 = new javax.swing.JButton();

       jButton5 = new javax.swing.JButton();

       jButton6 = new javax.swing.JButton();

       jButton7 = new javax.swing.JButton();

       jButton8 = new javax.swing.JButton();

       jButton9 = new javax.swing.JButton();

 

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

 

      jLabel1.setFont(new java.awt.Font("바탕", 1, 24)); // NOI18N

       jLabel1.setForeground(new java.awt.Color(255, 153, 153));

      jLabel1.setText("2014 월별 MoneyBook");

 

       int i = 1;

       jTable1.setModel(new javax.swing.table.DefaultTableModel(

              new Object[][] {

                     { (i++) + "", null, null, null, null, null, null },

                     { (i++) + "", null, null, null, null, null, null },

                     { (i++) + "", null, null, null, null, null, null },

                     { (i++) + "", null, null, null, null, null, null },

                     { (i++) + "", null, null, null, null, null, null },

                     { (i++) + "", null, null, null, null, null, null },

                     { (i++) + "", null, null, null, null, null, null },

                     { (i++) + "", null, null, null, null, null, null },

                     { (i++) + "", null, null, null, null, null, null },

                     { (i++) + "", null, null, null, null, null, null },

                     { (i++) + "", null, null, null, null, null, null },

                     { (i++) + "", null, null, null, null, null, null },

                     { (i++) + "", null, null, null, null, null, null },

                     { (i++) + "", null, null, null, null, null, null },

                     { (i++) + "", null, null, null, null, null, null },

                     { (i++) + "", null, null, null, null, null, null },

                     { (i++) + "", null, null, null, null, null, null },

                     { (i++) + "", null, null, null, null, null, null },

                     { (i++) + "", null, null, null, null, null, null },

                     { (i++) + "", null, null, null, null, null, null },

                     { (i++) + "", null, null, null, null, null, null },

                     { (i++) + "", null, null, null, null, null, null },

                     { (i++) + "", null, null, null, null, null, null },

                     { (i++) + "", null, null, null, null, null, null },

                     { (i++) + "", null, null, null, null, null, null },

                     { (i++) + "", null, null, null, null, null, null },

                     { (i++) + "", null, null, null, null, null, null },

                     { (i++) + "", null, null, null, null, null, null },

                     { (i++) + "", null, null, null, null, null, null },

                     { (i++) + "", null, null, null, null, null, null },

                     { (i++) + "", null, null, null, null, null, null } },

              new String[] { "날짜", "수입 항목", "내용", "금액", "지출 항목", "내용", "금액" }));

       jScrollPane1.setViewportView(jTable1);

       // jTable1.getColumnModel().getColumn(0).setCellEditor(new

       // DatePickerCellEditor());

 

       RowSorter<TableModel> sorter = new TableRowSorter<TableModel>(

              jTable1.getModel());

       jTable1.setRowSorter(sorter);

 

        jTable1.setAutoCreateColumnsFromModel(true);

 

       loadFile();

 

       jTable2.setModel(new javax.swing.table.DefaultTableModel(

             new Object[][] { { null, null, null, null, null } },

              new String[] { "전월 이월금", "수입 합계", "지출 합계", " 합계", "잔액" }));

       jScrollPane2.setViewportView(jTable2);

 

       jButton1.setText("항목검색");

       jButton1.addActionListener(new java.awt.event.ActionListener() {

           public void actionPerformed(java.awt.event.ActionEvent evt) {

              jButton1ActionPerformed(evt);

           }

       });

 

       jButton2.setText("오랜순 정렬");

 

       jButton3.setText("낮은금액순정렬");

 

       jButton4.setText("잔액고침");

       jButton4.addActionListener(new java.awt.event.ActionListener() {

           public void actionPerformed(java.awt.event.ActionEvent evt) {

              loadFile();

           }

 

       });

 

       jButton5.setText("사용방법");

 

       jButton6.setText("내보내기");

       jButton6.addActionListener(new java.awt.event.ActionListener() {

           public void actionPerformed(java.awt.event.ActionEvent evt) {

              saveFile();

           }

 

       });

 

       jButton7.setText("항목검색");

       jButton7.addActionListener(new java.awt.event.ActionListener() {

           public void actionPerformed(java.awt.event.ActionEvent evt) {

              jButton7ActionPerformed(evt);

           }

       });

 

       jButton8.setText("높은금액순정렬");

       jButton8.addActionListener(new java.awt.event.ActionListener() {

           public void actionPerformed(java.awt.event.ActionEvent evt) {

              jButton8ActionPerformed(evt);

           }

       });

 

       jButton9.setText("최신순 정렬");

 

       javax.swing.GroupLayout layout = new javax.swing.GroupLayout(

              getContentPane());

       getContentPane().setLayout(layout);

       layout.setHorizontalGroup(layout

           .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

              .addGroup(

                      layout.createSequentialGroup()

                            .addContainerGap()

                            .addGroup(

                                    layout.createParallelGroup(

                                           javax.swing.GroupLayout.Alignment.LEADING)

                                           .addGroup(

                                                  layout.createSequentialGroup()

                                                        .addComponent(

                                                               jComboBox1,

                                                                javax.swing.GroupLayout.PREFERRED_SIZE,

                                                               102,

                                                                javax.swing.GroupLayout.PREFERRED_SIZE)

                                                        .addContainerGap(

                                                                javax.swing.GroupLayout.DEFAULT_SIZE,

                                                               Short.MAX_VALUE))

                                           .addGroup(

                                                  layout.createSequentialGroup()

                                                        .addGroup(

                                                               layout.createParallelGroup(

                                                                       javax.swing.GroupLayout.Alignment.TRAILING,

                                                                      false)

                                                                      .addGroup(

                                                                              layout.createSequentialGroup()

                                                                                    .addGap(10,

                                                                                           10,

                                                                                            10)

                                                                                    .addComponent(

                                                                                           jLabel1,

                                                                                            javax.swing.GroupLayout.DEFAULT_SIZE,

                                                                                            javax.swing.GroupLayout.DEFAULT_SIZE,

                                                                                            Short.MAX_VALUE))

                                                                      .addComponent(

                                                                             jScrollPane2,

                                                                              javax.swing.GroupLayout.Alignment.LEADING)

                                                                      .addComponent(

                                                                             jScrollPane1,

                                                                              javax.swing.GroupLayout.Alignment.LEADING,

                                                                              javax.swing.GroupLayout.DEFAULT_SIZE,

                                                                             518,

                                                                             Short.MAX_VALUE))

                                                        .addGroup(

                                                               layout.createParallelGroup(

                                                                       javax.swing.GroupLayout.Alignment.LEADING)

                                                                      .addGroup(

                                                                              layout.createSequentialGroup()

                                                                                    .addGap(65,

                                                                                           65,

                                                                                           65)

                                                                                    .addComponent(

                                                                                            jLabel2))

                                                                      .addGroup(

                                                                              layout.createSequentialGroup()

                                                                                    .addGap(18,

                                                                                           18,

                                                                                           18)

                                                                                     .addGroup(

                                                                                            layout.createParallelGroup(

                                                                                                   javax.swing.GroupLayout.Alignment.LEADING)

                                                                                                   .addComponent(

                                                                                                         jButton5)

                                                                                                   .addComponent(

                                                                                                         jButton2)

                                                                                                   .addComponent(

                                                                                                         jButton1)

                                                                                                   .addComponent(

                                                                                                         jButton3)

                                                                                                    .addComponent(

                                                                                                         jButton4)

                                                                                                   .addComponent(

                                                                                                         jButton7)

                                                                                                   .addComponent(

                                                                                                         jButton8)

                                                                                                   .addComponent(

                                                                                                         jButton9)

                                                                                                   .addComponent(

                                                                                                         jButton6))))

                                                        .addGap(0,

                                                               38,

                                                               Short.MAX_VALUE)))));

       layout.setVerticalGroup(layout

           .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

              .addGroup(

                      layout.createSequentialGroup()

                             .addComponent(jComboBox1)

                            .addGap(4, 4, 4)

                            .addComponent(jLabel1,

                                    javax.swing.GroupLayout.PREFERRED_SIZE,

                                   33,

                                    javax.swing.GroupLayout.PREFERRED_SIZE)

                            .addPreferredGap(

                                javax.swing.LayoutStyle.ComponentPlacement.RELATED,

                                    javax.swing.GroupLayout.DEFAULT_SIZE,

                                    Short.MAX_VALUE)

                            .addGroup(

                                    layout.createParallelGroup(

                                           javax.swing.GroupLayout.Alignment.LEADING,

                                          false)

                                           .addComponent(

                                                  jButton5,

                                                  javax.swing.GroupLayout.DEFAULT_SIZE,

                                                  javax.swing.GroupLayout.DEFAULT_SIZE,

                                                  Short.MAX_VALUE)

                                           .addComponent(

                                                  jScrollPane2,

                                                  javax.swing.GroupLayout.DEFAULT_SIZE,

                                                  59, Short.MAX_VALUE))

                            .addGroup(

                                    layout.createParallelGroup(

                                           javax.swing.GroupLayout.Alignment.LEADING,

                                          false)

                                           .addGroup(

                                                  layout.createSequentialGroup()

                                                        .addGap(42, 42,

                                                               42)

                                                        .addComponent(

                                                               jLabel2)

                                                        .addPreferredGap(

                                                            javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

                                                        .addComponent(

                                                               jButton1)

                                                        .addGap(14, 14,

                                                               14)

                                                        .addComponent(

                                                               jButton2)

                                                        .addGap(14, 14,

                                                               14)

                                                        .addComponent(

                                                               jButton9)

                                                        .addGap(18, 18,

                                                               18)

                                                        .addComponent(

                                                               jButton7)

                                                        .addGap(18, 18,

                                                               18)

                                                        .addComponent(

                                                               jButton8)

                                                        .addGap(18, 18,

                                                               18)

                                                        .addComponent(

                                                               jButton3)

                                                         .addGap(40, 40,

                                                               40)

                                                        .addComponent(

                                                               jButton4)

                                                        .addPreferredGap(

                                                            javax.swing.LayoutStyle.ComponentPlacement.RELATED,

                                                                javax.swing.GroupLayout.DEFAULT_SIZE,

                                                               Short.MAX_VALUE)

                                                        .addComponent(

                                                               jButton6))

                                           .addGroup(

                                                  layout.createSequentialGroup()

                                                        .addGap(18, 18,

                                                                18)

                                                        .addComponent(

                                                               jScrollPane1,

                                                                javax.swing.GroupLayout.PREFERRED_SIZE,

                                                                javax.swing.GroupLayout.DEFAULT_SIZE,

                                                                javax.swing.GroupLayout.PREFERRED_SIZE)))

                            .addContainerGap(

                                    javax.swing.GroupLayout.DEFAULT_SIZE,

                                    Short.MAX_VALUE)));

 

       pack();

    }// </editor-fold>//GEN-END:initComponents

 

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jComboBox1ActionPerformed

       // TODO add your handling code here:

    }// GEN-LAST:event_jComboBox1ActionPerformed

 

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButton1ActionPerformed

       // TODO add your handling code here:

    }// GEN-LAST:event_jButton1ActionPerformed

 

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButton7ActionPerformed

       // TODO add your handling code here:

    }// GEN-LAST:event_jButton7ActionPerformed

 

    private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButton8ActionPerformed

       // TODO add your handling code here:

    }// GEN-LAST:event_jButton8ActionPerformed

 

    /**

     * @param args

     *            the command line arguments

     */

    public static void main(String args[]) {

       /* Set the Nimbus look and feel */

       // <editor-fold defaultstate="collapsed"

       // desc=" Look and feel setting code (optional) ">

       /*

        * If Nimbus (introduced in Java SE 6) is not available, stay with the

        * default look and feel. For details see

        * http://download.oracle.com/javase

        * /tutorial/uiswing/lookandfeel/plaf.html

        */

       try {

           for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager

                  .getInstalledLookAndFeels()) {

             if ("Nimbus".equals(info.getName())) {

              javax.swing.UIManager.setLookAndFeel(info.getClassName());

                  break;

              }

           }

       } catch (ClassNotFoundException ex) {

       java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(

                  java.util.logging.Level.SEVERE, null, ex);

       } catch (InstantiationException ex) {

       java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(

                  java.util.logging.Level.SEVERE, null, ex);

       } catch (IllegalAccessException ex) {

       java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(

                  java.util.logging.Level.SEVERE, null, ex);

       } catch (javax.swing.UnsupportedLookAndFeelException ex) {

       java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(

                  java.util.logging.Level.SEVERE, null, ex);

       }

       // </editor-fold>

 

       /* Create and display the form */

       java.awt.EventQueue.invokeLater(new Runnable() {

           public void run() {

              new NewJFrame().setVisible(true);

           }

       });

    }

 

    // Variables declaration - do not modify//GEN-BEGIN:variables

    private javax.swing.JButton jButton1;

    private javax.swing.JButton jButton2;

    private javax.swing.JButton jButton3;

    private javax.swing.JButton jButton4;

    private javax.swing.JButton jButton5;

    private javax.swing.JButton jButton6;

    private javax.swing.JButton jButton7;

    private javax.swing.JButton jButton8;

    private javax.swing.JButton jButton9;

    private javax.swing.JComboBox jComboBox1;

    private javax.swing.JLabel jLabel1;

    private javax.swing.JLabel jLabel2;

    private javax.swing.JScrollPane jScrollPane1;

    private javax.swing.JScrollPane jScrollPane2;

    private javax.swing.JTable jTable1;

    private javax.swing.JTable jTable2;

    // End of variables declaration//GEN-END:variables

    int thisMonth = 1;

 

    @Override

    public void actionPerformed(ActionEvent arg0) {

       // TODO Auto-generated method stub

       if (arg0.getSource() == jComboBox1) {

           String str = (String) jComboBox1.getSelectedItem();

           jLabel1.setText("2014 " + str + " MoneyBook");

           // "1"

           // "1"

           thisMonth = Integer.parseInt(str.charAt(0) + "");

       }

 

    }

 

    private void saveFile() {

       try {

           FileWriter out = null;

           out = new FileWriter(thisMonth + ".txt");

           String str = "";

           for (int i = 0; i < 31; i++) {

              str += (String) jTable1.getModel().getValueAt(i, 0);

              str += "\t";

              str += (String) jTable1.getModel().getValueAt(i, 1);

              str += "\t";

              str += (String) jTable1.getModel().getValueAt(i, 2);

              str += "\t";

              str += (String) jTable1.getModel().getValueAt(i, 3);

              str += "\t";

              str += (String) jTable1.getModel().getValueAt(i, 4);

              str += "\t";

              str += (String) jTable1.getModel().getValueAt(i, 5);

              str += "\t";

              str += (String) jTable1.getModel().getValueAt(i, 6);

              str += "\n";

           }

           out.write(str);

           out.flush();

           out.close();

       } catch (Exception e) {

 

       }

    }

 

    private void loadFile() {

       // TODO Auto-generated method stub

 

       try {

           FileReader in = new FileReader(thisMonth + ".txt");

           BufferedReader br = new BufferedReader(in);

           String thisLine;

           int rowIndex = 0;

           while ((thisLine = br.readLine()) != null) { // while loop begins

 

              int columnIndex = 0;

              StringTokenizer tokenizer = new StringTokenizer(thisLine, "\t"); // 설정

              while (tokenizer.hasMoreTokens()) {

                  String token = tokenizer.nextToken();

                  System.out.println(token + " columnIndex " + columnIndex

                         + " rowIndex " + rowIndex);

                  if (token.equals("null")) {

                     token = " ";

                  }

                  jTable1.getModel().setValueAt(token, rowIndex, columnIndex);

 

                  columnIndex++;

              }

 

              rowIndex++;

           }

       } catch (Exception e) {

           e.printStackTrace();

       }

    }

}

 

'Programming > JAVA,JSP' 카테고리의 다른 글

JAVA 공던지기 게임 완성  (0) 2014.01.16
JAVA 공던지기 게임  (0) 2014.01.16
JAVA 가계부  (0) 2014.01.14
JAVA 쿵쿵따 게임 #2 중복 탐지  (0) 2014.01.09
JAVA 쿵쿵따 게임  (0) 2014.01.09