본문 바로가기

IT

ListFrame .java

// Fig. 14.23: ListFrame.java

// JList that displays a list of colors.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;


public class ListFrame extends JFrame  implements ActionListener

{

  private JTextField input, // text field with set size

  textField50000,

  textField10000,

  textField5000,

  textField1000,

  textField500,

  textField100,

  textField50,

  textField10,

  textField1;

  

  private JLabel a; // text field with set size

  

  private JButton submitButton; // button with just text

     // ListFrame constructor add JScrollPane containing JList to JFrame

    

  


  JPanel homePane = new JPanel();

  JPanel inFieldPane = new JPanel();


JPanel submitPane = new JPanel();

     

JPanel outFieldPane= new JPanel();

  

  public ListFrame()

  {

  super( "Money Changer" );

  setLayout(new BorderLayout());

  

  getContentPane().setBackground( Color.PINK );

     

  input = new JTextField( 10 );

  submitButton = new JButton("계산");

  submitButton.addActionListener(this);


  TextFieldHandler Thandler = new TextFieldHandler();

  ButtonHandler Bhandler = new ButtonHandler();

  a = new JLabel( "금액" );

inFieldPane.setLayout(new GridLayout(1,3,20,2));

inFieldPane.add(a);

a.setHorizontalAlignment(JLabel.CENTER);

inFieldPane.add(input);

inFieldPane.setBackground( Color.PINK );

inFieldPane.add(submitButton);

submitButton.addActionListener( Bhandler );

input.addActionListener( Thandler );

super.add(inFieldPane,"North");



textField50000 = new JTextField( 10 );

textField50000.setEditable( false );

textField50000.setHorizontalAlignment(JTextField.CENTER);

textField10000 = new JTextField( 10 );

textField10000.setEditable( false );

textField10000.setHorizontalAlignment(JTextField.CENTER);

textField5000 = new JTextField( 10 );

textField5000.setEditable( false );

textField5000.setHorizontalAlignment(JTextField.CENTER);

textField1000 = new JTextField( 10 );

textField1000.setEditable( false );

textField1000.setHorizontalAlignment(JTextField.CENTER);

textField500 = new JTextField( 10 );

textField500.setEditable( false );

textField500.setHorizontalAlignment(JTextField.CENTER);

textField100 = new JTextField( 10 );

textField100.setEditable( false );

textField100.setHorizontalAlignment(JTextField.CENTER);

textField50 = new JTextField( 10 );

textField50.setEditable( false );

textField50.setHorizontalAlignment(JTextField.CENTER);

textField10 = new JTextField( 10 );

textField10.setEditable( false );

textField10.setHorizontalAlignment(JTextField.CENTER);

textField1 = new JTextField( 10 );

textField1.setEditable( false );

textField1.setHorizontalAlignment(JTextField.CENTER);

submitPane.setLayout(new GridLayout(10,3,15,2));

submitPane.setBackground( Color.PINK );


  a = new JLabel( "오만원" );

submitPane.add(a);

a.setHorizontalAlignment(JLabel.RIGHT);

submitPane.add(textField50000);

submitPane.add(new JLabel( " " ));


  a = new JLabel( "만원" );

submitPane.add(a);

a.setHorizontalAlignment(JLabel.RIGHT);

submitPane.add(textField10000);

submitPane.add(new JLabel( " " ));


  a = new JLabel( "오천원" );

submitPane.add(a);

a.setHorizontalAlignment(JLabel.RIGHT);

submitPane.add(textField5000);

submitPane.add(new JLabel( " " ));


  a = new JLabel( "천원" );

submitPane.add(a);

a.setHorizontalAlignment(JLabel.RIGHT);

submitPane.add(textField1000);

submitPane.add(new JLabel( " " ));


  a = new JLabel( "오백원" );

submitPane.add(a);

a.setHorizontalAlignment(JLabel.RIGHT);

submitPane.add(textField500);

submitPane.add(new JLabel( " " ));


  a = new JLabel( "백원" );

submitPane.add(a);

a.setHorizontalAlignment(JLabel.RIGHT);

submitPane.add(textField100);

submitPane.add(new JLabel( " " ));


  a = new JLabel( "오십원" );

submitPane.add(a);

a.setHorizontalAlignment(JLabel.RIGHT);

submitPane.add(textField50);

submitPane.add(new JLabel( " " ));


  a = new JLabel( "십원" );

submitPane.add(a);

a.setHorizontalAlignment(JLabel.RIGHT);

submitPane.add(textField10);

submitPane.add(new JLabel( " " ));


  a = new JLabel( "일원" );

submitPane.add(a);

a.setHorizontalAlignment(JLabel.RIGHT);

submitPane.add(textField1);

submitPane.add(new JLabel( " " ));

      

super.add(submitPane,"Center");


// outFieldPane.setPreferredSize(new Dimension(26, 40));

outFieldPane.add(new JLabel( "2010105084 이진우" ));

outFieldPane.setBackground( Color.PINK );

super.add(outFieldPane,"South");

     

  } // end ListFrame constructor


  private class TextFieldHandler implements ActionListener 

  {

     // process textfield events

  private int d, r;

  public void actionPerformed( ActionEvent event )

  {

  String string = ""; // declare string to display


        // user pressed Enter in JTextField textField1

  if ( event.getSource() == input )

  {

  int inputValue = Integer.parseInt(input.getText());


  textField50000.setText("0");

  textField10000.setText("0");

  textField5000.setText("0");

  textField1000.setText("0");

  textField500.setText("0");

  textField100.setText("0");

  textField50.setText("0");

  textField10.setText("0");

  textField1.setText("0");

  

  if(inputValue > 50000)

  {

  d = inputValue/50000; 

  r = inputValue%50000; 

  textField50000.setText(String.valueOf(d));  

  inputValue = r;

  }

  if(inputValue > 10000)

  {

  d = inputValue/10000; 

  r = inputValue%10000; 

  textField10000.setText(String.valueOf(d));  

  inputValue = r;

  }

  if(inputValue > 5000)

  {

  d = inputValue/5000; 

  r = inputValue%5000; 

  textField5000.setText(String.valueOf(d));  

  inputValue = r;

  }

  if(inputValue > 1000)

  {

  d = inputValue/1000; 

  r = inputValue%1000; 

  textField1000.setText(String.valueOf(d));  

  inputValue = r;

  }

  if(inputValue > 500)

  {

  d = inputValue/500; 

  r = inputValue%500; 

  textField500.setText(String.valueOf(d));  

  inputValue = r;

  }

  if(inputValue > 100)

  {

  d = inputValue/100; 

  r = inputValue%100; 

  textField100.setText(String.valueOf(d));  

  inputValue = r;

  }

  if(inputValue > 50)

  {

  d = inputValue/50; 

  r = inputValue%50; 

  textField50.setText(String.valueOf(d));  

  inputValue = r;

  }

  if(inputValue > 10)

  {

  d = inputValue/10; 

  r = inputValue%10; 

  textField10.setText(String.valueOf(d));  

  inputValue = r;

  }

  if(inputValue > 1)

  {

  d = inputValue/1; 

  r = inputValue%1; 

  textField1.setText(String.valueOf(d));  

  inputValue = r;

  }

  }

  } // end method actionPerformed

  } // end private inner class TextFieldHandler

  

  private class ButtonHandler implements ActionListener 

  {

  private int d, r;

     // handle button event

  public void actionPerformed( ActionEvent event )

  {

        int inputValue = Integer.parseInt(input.getText());


  textField50000.setText("0");

  textField10000.setText("0");

  textField5000.setText("0");

  textField1000.setText("0");

  textField500.setText("0");

  textField100.setText("0");

  textField50.setText("0");

  textField10.setText("0");

  textField1.setText("0");

  

        if(inputValue > 50000)

        {

        d = inputValue/50000; 

        r = inputValue%50000; 

        textField50000.setText(String.valueOf(d));  

        inputValue = r;

        }

        if(inputValue > 10000)

        {

        d = inputValue/10000; 

        r = inputValue%10000; 

        textField10000.setText(String.valueOf(d));  

        inputValue = r;

        }

        if(inputValue > 5000)

        {

        d = inputValue/5000; 

        r = inputValue%5000; 

        textField5000.setText(String.valueOf(d));  

        inputValue = r;

        }

        if(inputValue > 1000)

        {

        d = inputValue/1000; 

        r = inputValue%1000; 

        textField1000.setText(String.valueOf(d));  

        inputValue = r;

        }

        if(inputValue > 500)

        {

        d = inputValue/500; 

        r = inputValue%500; 

        textField500.setText(String.valueOf(d));  

        inputValue = r;

        }

        if(inputValue > 100)

        {

        d = inputValue/100; 

        r = inputValue%100; 

        textField100.setText(String.valueOf(d));  

        inputValue = r;

        }

        if(inputValue > 50)

        {

        d = inputValue/50; 

        r = inputValue%50; 

        textField50.setText(String.valueOf(d));  

        inputValue = r;

        }

        if(inputValue > 10)

        {

        d = inputValue/10; 

        r = inputValue%10; 

        textField10.setText(String.valueOf(d));  

        inputValue = r;

        }

        if(inputValue > 1)

        {

        d = inputValue/1; 

        r = inputValue%1; 

        textField1.setText(String.valueOf(d));  

        inputValue = r;

        }

     } // end method actionPerformed

  } // end private inner class ButtonHandler

}

/**************************************************************************

 * (C) Copyright 1992-2012 by Deitel & Associates, Inc. and               *

 * Pearson Education, Inc. All Rights Reserved.                           *

 *                                                                        *

 * DISCLAIMER: The authors and publisher of this book have used their     *

 * best efforts in preparing the book. These efforts include the          *

 * development, research, and testing of the theories and programs        *

 * to determine their effectiveness. The authors and publisher make       *

 * no warranty of any kind, expressed or implied, with regard to these    *

 * programs or to the documentation contained in these books. The authors *

 * and publisher shall not be liable in any event for incidental or       *

 * consequential damages in connection with, or arising out of, the       *

 * furnishing, performance, or use of these programs.                     *

 *************************************************************************/



'IT' 카테고리의 다른 글

sub2.java  (0) 2014.05.17
test3 .java  (0) 2014.05.17
스도쿠 판별 // 이게 완성본이 아닌건 함정  (0) 2014.05.14
파일 복사하기  (0) 2014.05.14
heap?  (0) 2014.05.14