JAVA GUI 계산기 소스코드

Programming/JAVA,JSP 2013. 12. 22. 17:55 Posted by TanSanC
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;


import javax.swing.JButton;


/*

 * To change this template, choose Tools | Templates

 * and open the template in the editor.

 */


/**

 * 

 * @author Administrator

 */

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


/**

* Creates new form NewJFrame

*/

public NewJFrame() {

initComponents();

toDo();

}


private void toDo() {

// TODO Auto-generated method stub

jButton2.addActionListener(this);

jButton3.addActionListener(this);

jButton4.addActionListener(this);

jButton5.addActionListener(this);

jButton6.addActionListener(this);

jButton7.addActionListener(this);

jButton8.addActionListener(this);

jButton9.addActionListener(this);

jButton10.addActionListener(this);

jButton13.addActionListener(this);

jButton14.addActionListener(this);

jButton15.addActionListener(this);

jButton16.addActionListener(this);

jButton17.addActionListener(this);

jButton18.addActionListener(this);

}


@SuppressWarnings("unchecked")

// <editor-fold defaultstate="collapsed" desc="Generated Code">

private void initComponents() {


jButton2 = new javax.swing.JButton();

jTextField3 = new javax.swing.JTextField();

jButton3 = new javax.swing.JButton();

jButton4 = new javax.swing.JButton();

jButton7 = new javax.swing.JButton();

jButton5 = new javax.swing.JButton();

jButton6 = new javax.swing.JButton();

jButton8 = new javax.swing.JButton();

jButton9 = new javax.swing.JButton();

jButton10 = new javax.swing.JButton();

jButton13 = new javax.swing.JButton();

jButton14 = new javax.swing.JButton();

jButton15 = new javax.swing.JButton();

jButton16 = new javax.swing.JButton();

jButton17 = new javax.swing.JButton();

jButton18 = new javax.swing.JButton();

jToggleButton1 = new javax.swing.JToggleButton();


setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);


jTextField3.setText("");


jButton2.setText("1");


jButton3.setText("2");


jButton4.setText("3");


jButton5.setText("4");


jButton6.setText("5");


jButton7.setText("6");


jButton8.setText("7");


jButton9.setText("8");


jButton10.setText("9");


jButton13.setText("0");


jButton14.setText("+");


jButton15.setText("/");


jButton16.setText("x");


jButton17.setText("-");


jButton18.setText("=");


jToggleButton1.setText("jToggleButton1");


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

getContentPane());

getContentPane().setLayout(layout);

layout.setHorizontalGroup(layout

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

.addComponent(jTextField3)

.addGroup(

layout.createSequentialGroup()

.addGroup(

layout.createParallelGroup(

javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(

layout.createParallelGroup(

javax.swing.GroupLayout.Alignment.TRAILING)

.addGroup(

layout.createSequentialGroup()

.addComponent(

jButton5)

.addPreferredGap(

javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addComponent(

jButton6)

.addPreferredGap(

javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addComponent(

jButton7))

.addGroup(

layout.createSequentialGroup()

.addComponent(

jButton2)

.addPreferredGap(

javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addComponent(

jButton3)

.addPreferredGap(

javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addComponent(

jButton4)))

.addGroup(

layout.createSequentialGroup()

.addComponent(

jButton9)

.addPreferredGap(

javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addGroup(

layout.createParallelGroup(

javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(

jButton13)

.addGroup(

layout.createSequentialGroup()

.addComponent(

jButton10)

.addPreferredGap(

javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addComponent(

jButton8)))))

.addPreferredGap(

javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addGroup(

layout.createParallelGroup(

javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(

layout.createSequentialGroup()

.addGroup(

layout.createParallelGroup(

javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(

jButton16)

.addComponent(

jButton15))

.addPreferredGap(

javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addComponent(

jButton18))

.addGroup(

layout.createParallelGroup(

javax.swing.GroupLayout.Alignment.TRAILING)

.addComponent(

jButton17)

.addComponent(

jButton14)))

.addPreferredGap(

javax.swing.LayoutStyle.ComponentPlacement.RELATED,

javax.swing.GroupLayout.DEFAULT_SIZE,

Short.MAX_VALUE)

.addComponent(jToggleButton1,

javax.swing.GroupLayout.PREFERRED_SIZE,

70,

javax.swing.GroupLayout.PREFERRED_SIZE)

.addContainerGap()));

layout.setVerticalGroup(layout

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

.addGroup(

layout.createSequentialGroup()

.addComponent(jTextField3,

javax.swing.GroupLayout.PREFERRED_SIZE,

70,

javax.swing.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(

javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addGroup(

layout.createParallelGroup(

javax.swing.GroupLayout.Alignment.LEADING,

false)

.addGroup(

layout.createSequentialGroup()

.addGroup(

layout.createParallelGroup(

javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(

jButton2)

.addComponent(

jButton3)

.addComponent(

jButton4))

.addPreferredGap(

javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addGroup(

layout.createParallelGroup(

javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(

jButton5)

.addComponent(

jButton6)

.addComponent(

jButton7))

.addPreferredGap(

javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addGroup(

layout.createParallelGroup(

javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(

jButton9)

.addComponent(

jButton10)

.addComponent(

jButton8))

.addPreferredGap(

javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addComponent(

jButton13))

.addGroup(

layout.createSequentialGroup()

.addComponent(

jButton14)

.addPreferredGap(

javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addComponent(

jButton17)

.addPreferredGap(

javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addComponent(

jButton16)

.addPreferredGap(

javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addComponent(

jButton15))

.addComponent(

jButton18,

javax.swing.GroupLayout.DEFAULT_SIZE,

javax.swing.GroupLayout.DEFAULT_SIZE,

Short.MAX_VALUE)

.addComponent(

jToggleButton1,

javax.swing.GroupLayout.DEFAULT_SIZE,

javax.swing.GroupLayout.DEFAULT_SIZE,

Short.MAX_VALUE))

.addContainerGap(

javax.swing.GroupLayout.DEFAULT_SIZE,

Short.MAX_VALUE)));


pack();

}// </editor-fold>


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

private javax.swing.JButton jButton10;

private javax.swing.JButton jButton13;

private javax.swing.JButton jButton14;

private javax.swing.JButton jButton15;

private javax.swing.JButton jButton16;

private javax.swing.JButton jButton17;

private javax.swing.JButton jButton18;

private javax.swing.JButton jButton2;

private javax.swing.JButton jButton3;

private javax.swing.JButton jButton4;

private javax.swing.JButton jButton7;

private javax.swing.JButton jButton5;

private javax.swing.JButton jButton6;

private javax.swing.JButton jButton8;

private javax.swing.JButton jButton9;

private javax.swing.JTextField jTextField3;

private javax.swing.JToggleButton jToggleButton1;


// End of variables declaration

@Override

public void actionPerformed(ActionEvent arg0) {

// TODO Auto-generated method stub

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

jTextField3.setText(jTextField3.getText() + 1);

} else if (arg0.getSource() == jButton3) {

jTextField3.setText(jTextField3.getText() + "2");

} else if (arg0.getSource() == jButton4) {

jTextField3.setText(jTextField3.getText() + "3");

} else if (arg0.getSource() == jButton7) {

jTextField3.setText(jTextField3.getText() + "4");

} else if (arg0.getSource() == jButton5) {

jTextField3.setText(jTextField3.getText() + "5");

} else if (arg0.getSource() == jButton6) {

jTextField3.setText(jTextField3.getText() + "6");

} else if (arg0.getSource() == jButton8) {

jTextField3.setText(jTextField3.getText() + "7");

} else if (arg0.getSource() == jButton9) {

jTextField3.setText(jTextField3.getText() + "8");

} else if (arg0.getSource() == jButton10) {

jTextField3.setText(jTextField3.getText() + "9");

} else if (arg0.getSource() == jButton13) {

jTextField3.setText(jTextField3.getText() + "0");

} else if (arg0.getSource() == jButton14) {// +

tempValue = Integer.parseInt(jTextField3.getText());

operator = '+';

jTextField3.setText("");

} else if (arg0.getSource() == jButton15) {// /

tempValue = Integer.parseInt(

jTextField3.getText());

operator = '/';

jTextField3.setText("");

} else if (arg0.getSource() == jButton16) {// x

tempValue = Integer.parseInt(jTextField3.getText());

operator = '*';

jTextField3.setText("");

} else if (arg0.getSource() == jButton17) {// -

tempValue = Integer.parseInt(jTextField3.getText());

operator = '-';

jTextField3.setText("");

} else if (arg0.getSource() == jButton18) {// -

int tempValue2 = Integer.parseInt(

jTextField3.getText());

int resultValue = 0;

if (operator == '+') {

resultValue = tempValue + tempValue2;

} else if (operator == '-') {

resultValue = tempValue - tempValue2;

} else if (operator == '*') {

resultValue = tempValue * tempValue2;

} else if (operator == '/') {

resultValue = tempValue / tempValue2;

}

jTextField3.setText("" + resultValue);

}

}


int tempValue;

char operator;

}



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

Calendar #1  (0) 2013.12.31
JAVA 파일 검색 코드  (0) 2013.12.28
JAVA 소수를 구하는 프로그램 GUI #2  (0) 2013.12.21
JAVA 소수를 판단하는 GUI 프로그램  (0) 2013.12.21
JAVA 버블 정렬  (0) 2013.11.30