코딩 연습장/JAVA

Java awt, swing을 사용한 계산기 화면 만들기

Do아 2021. 4. 6. 03:45
728x90
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class Exam01 extends JFrame{
	private Label calc_lb= new Label("계산기", Label.CENTER);
	private TextField tf = new TextField(50);
	private Button bt0 = new Button("0");
	private Button bt1 = new Button("1");
	private Button bt2 = new Button("2");
	private Button bt3 = new Button("3");
	private Button bt4 = new Button("4");
	private Button bt5 = new Button("5");
	private Button bt6 = new Button("6");
	private Button bt7 = new Button("7");
	private Button bt8 = new Button("8");
	private Button bt9 = new Button("9");
	
	private Button plus_bt = new Button("+");
	private Button minus_bt = new Button("-");
	private Button mul_bt = new Button("*");
	private Button div_bt = new Button("/");
	
	private Button percent_bt = new Button("%");
	private Button clear_bt = new Button("C");
	private Button clr_bt = new Button("CE");
	private Button remove_bt = new Button("<-");
	private Button result_bt = new Button("=");
	private Button plusMinus_bt = new Button("+/-");
	
	private BorderLayout bl = new BorderLayout();
	private GridLayout gl = new GridLayout(2,1,5,5);
	private Panel p = new Panel();

	public Exam01() {
		
		super();
		super.setSize(500,600);
		
		this.init();
		
		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension frm = super.getSize();
		int xpos = (int)(screen.getWidth()/2 - frm.getWidth()/2);
		int ypos = (int)(screen.getHeight()/2 - frm.getHeight()/2);
		
		super.setLocation(xpos, ypos);
		super.setResizable(false);
		super.setVisible(true);
			
	}
	
	public void init() {
		
		this.setLayout(gl);
		
		this.add(tf);
		this.add(p);

		p.setLayout(new GridLayout(5,4,5,5));
		
		
		p.add(percent_bt);
		p.add(clr_bt);
		p.add(clear_bt);
		p.add(remove_bt);
		p.add(bt7);
		p.add(bt8);
		p.add(bt9);
		p.add(div_bt);
		p.add(bt4);
		p.add(bt5);
		p.add(bt6);
		p.add(mul_bt);
		p.add(bt1);
		p.add(bt2);
		p.add(bt3);
		p.add(minus_bt);
		p.add(plusMinus_bt);
		p.add(bt0);
		p.add(plus_bt);
		p.add(result_bt);
		
	}
	
	public static void main(String[] args) {
		new Exam01();
	}
}

 

 

결과 화면

 

728x90