Search

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
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

 

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextField;

 

class MyFrame extends JFrame {

   private JButton button;

   private JTextField text, result;

 

   public MyFrame() {

       setSize(300, 130);

       setTitle("제곱 계산하기");

   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 

       ButtonListener listener = new ButtonListener(); // 리스너 객체 생성

 

       JPanel panel = new JPanel();

       panel.add(new JLabel("숫자 입력: ")); // 레이블 생성

       text = new JTextField(15); // 컬럼수가 15 텍스트 필드 생성

       text.addActionListener(listener); // 텍스트 필드에 리스너 연결

       panel.add(text);

 

       panel.add(new JLabel("제곱한 : "));

       result = new JTextField(15); // 결과를 나타낼 텍스트 필드

       result.setEditable(false); // 편집 불가 설정

       panel.add(result);

       button = new JButton("OK");

       button.addActionListener(listener);

       panel.add(button);

       add(panel);

       setVisible(true);

   }

 

   // 텍스트 필드와 버튼의 액션 이벤트 처리

   private class ButtonListener implements ActionListener {

       public void actionPerformed(ActionEvent e) {

          if (e.getSource() == button || e.getSource() == text) {

             String name = text.getText();

             int value = 0;

             try {

                value = Integer.parseInt(name);

 

                 boolean isPrimeNumber = false;

 

                 // 계산

                 int count = 0;

                 for (int i = 1; i <= value; i++) {

                    // 계산

                    if (value % i == 0) {

                       count++;

                    }

 

                 }

 

                 if (count == 2) {

                    isPrimeNumber = true;

                 }

 

                 if (isPrimeNumber) {

                    result.setText("소수입니다.");

                 } else {

                    result.setText("소수가 아닙니다.");

                 }

            } catch (NumberFormatException e1) {

                 result.setText("숫자를 입력해주세요!!");

             }

             text.requestFocus();

          }

       }

   }

}

 

public class TextFieldTest extends JFrame {

   public static void main(String[] args) {

       new MyFrame();

   }

}

 

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

JAVA 파일 검색 코드  (0) 2013.12.28
JAVA GUI 계산기 소스코드  (0) 2013.12.22
JAVA 소수를 판단하는 GUI 프로그램  (0) 2013.12.21
JAVA 버블 정렬  (0) 2013.11.30
JAVA JTable 실습 예제  (0) 2013.08.24

JAVA 실습 과제 , GUI

실습과제 모음 2013. 8. 12. 14:17 Posted by TanSanC
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.
package com.tansanc.tistory;

import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

class MyFrame extends JFrame implements KeyListener
{
	public MyFrame()
	{
		this.setSize(300, 200);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setTitle("이벤트 예제");
		JLabel staticText = new JLabel("Hello World");
		add(staticText, BorderLayout.NORTH);
		JTextField inputText = new JTextField("입력창");
		add(inputText, BorderLayout.CENTER);
		JLabel resultText = new JLabel("Hello World");
		add(resultText, BorderLayout.SOUTH);
		
		inputText.addKeyListener(this);
		
		setVisible(true);
	}
	@Override
	public void keyPressed(KeyEvent e)
	{		
	}
	@Override
	public void keyReleased(KeyEvent e)
	{		
	}
	@Override
	public void keyTyped(KeyEvent e)
	{		
	}
}

public class Test
{
	public static void main(String[] args)
	{
		MyFrame t = new MyFrame();
	}
}

'실습과제 모음' 카테고리의 다른 글

JAVA 실습 2013 11 23  (0) 2013.11.23
C 언어 과제 11/23  (0) 2013.11.23
JAVA 실습용  (0) 2013.08.07
JAVA 실습 0801  (0) 2013.08.01
JAVA 실습과제 0303 ~ 0309  (0) 2013.03.03

JAVA 채팅 프로그램 + GUI

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

Chat 실습.zip

JAVA 채팅 프로그램 + GUI

 

 

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

JAVA 데이터베이스 실습 예제 UI  (0) 2013.02.24
JAVA JDBC 튜토리얼 예제 사이트  (0) 2013.02.24
RadioButton exam  (0) 2013.02.03
[IT]자바의 위기, 한국 IT의 시금석  (0) 2013.02.02
JAVA 비행기게임  (0) 2013.01.09