말 시급. 자바 GUI 할 줄 아는 천재들 있어..?
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
public class Calculator extends JFrame implements ActionListener {
private JPanel contentPane;
private JPanel pnDisplay;
private JPanel pnInput;
private JPanel pnHistory;
private JTextField textField;
private JButton btn7;
private JButton btn8;
private JButton btn9;
private JButton btnAdd;
private JButton btnClearAll;
private JButton btn4;
private JButton btn5;
private JButton btn6;
private JButton btnMinus;
private JButton btnClearText;
private JButton btn1;
private JButton btn2;
private JButton btn3;
private JButton btnMultiply;
private JButton btnspuare;
private JButton btn0;
private JButton btnDot;
private JButton btnEqual;
private JButton btnDivide;
private JButton btnfrac;
private JList list;
// String to store input data
private String num1;
private String num2;
private String operator;
private final String NONE = "NONE";
private DefaultListModel<String> listData;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Calculator frame = new Calculator();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Calculator() {
/*
* GUI code
*/
setTitle("Simple Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
pnDisplay = new JPanel();
contentPane.add(pnDisplay, BorderLayout.NORTH);
pnDisplay.setLayout(new GridLayout(0, 1, 0, 0));
textField = new JTextField();
textField.setHorizontalAlignment(SwingConstants.RIGHT);
textField.setFont(new Font("Courier New", Font.PLAIN, 28));
pnDisplay.add(textField);
textField.setColumns(10);
pnInput = new JPanel();
contentPane.add(pnInput, BorderLayout.CENTER);
pnInput.setLayout(new GridLayout(4, 5, 5, 5));
btn7 = new JButton("7");
btn7.setFont(new Font("굴림", Font.BOLD, 14));
btn7.addActionListener(this);
pnInput.add(btn7);
btn8 = new JButton("8");
btn8.setFont(new Font("굴림", Font.BOLD, 14));
btn8.addActionListener(this);
pnInput.add(btn8);
btn9 = new JButton("9");
btn9.setFont(new Font("굴림", Font.BOLD, 14));
btn9.addActionListener(this);
pnInput.add(btn9);
btnAdd = new JButton("+");
btnAdd.setFont(new Font("굴림", Font.BOLD, 14));
btnAdd.addActionListener(this);
pnInput.add(btnAdd);
btnClearAll = new JButton("C");
btnClearAll.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// reset all the data
textField.setText("");
num1 = NONE;
num2 = NONE;
operator = NONE;
}
});
btnClearAll.setFont(new Font("굴림", Font.BOLD, 14));
pnInput.add(btnClearAll);
btn4 = new JButton("4");
btn4.setFont(new Font("굴림", Font.BOLD, 14));
btn4.addActionListener(this);
pnInput.add(btn4);
btn5 = new JButton("5");
btn5.setFont(new Font("굴림", Font.BOLD, 14));
btn5.addActionListener(this);
pnInput.add(btn5);
btn6 = new JButton("6");
btn6.setFont(new Font("굴림", Font.BOLD, 14));
btn6.addActionListener(this);
pnInput.add(btn6);
btnMinus = new JButton("-");
btnMinus.setFont(new Font("굴림", Font.BOLD, 14));
btnMinus.addActionListener(this);
pnInput.add(btnMinus);
btnClearText = new JButton("CE");
btnClearText.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// reset the textfield only
textField.setText("");
textField.setEditable(true);
}
});
btnClearText.setFont(new Font("굴림", Font.BOLD, 14));
pnInput.add(btnClearText);
btn1 = new JButton("1");
btn1.setFont(new Font("굴림", Font.BOLD, 14));
btn1.addActionListener(this);
pnInput.add(btn1);
btn2 = new JButton("2");
btn2.setFont(new Font("굴림", Font.BOLD, 14));
btn2.addActionListener(this);
pnInput.add(btn2);
btn3 = new JButton("3");
btn3.setFont(new Font("굴림", Font.BOLD, 14));
btn3.addActionListener(this);
pnInput.add(btn3);
btnMultiply = new JButton("x");
btnMultiply.setFont(new Font("굴림", Font.BOLD, 14));
btnMultiply.addActionListener(this);
pnInput.add(btnMultiply);
btnsquare = new JButton("x²");
btnsquare.setFont(new Font("굴림", Font.BOLD, 14));
btnsquare.addActionListener(this);
pnInput.add(btnsquare);
btn0 = new JButton("0");
btn0.setFont(new Font("굴림", Font.BOLD, 14));
btn0.addActionListener(this);
pnInput.add(btn0);
btnDot = new JButton(".");
btnDot.setFont(new Font("굴림", Font.BOLD, 14));
pnInput.add(btnDot);
btnEqual = new JButton("=");
btnEqual.setFont(new Font("굴림", Font.BOLD, 14));
btnEqual.addActionListener(this);
pnInput.add(btnEqual);
btnDivide = new JButton("/");
btnDivide.setFont(new Font("굴림", Font.BOLD, 14));
btnDivide.addActionListener(this);
pnInput.add(btnDivide);
btnfrac = new JButton("1/x");
btnfrac.setFont(new Font("굴림", Font.BOLD, 14));
btnfrac.addActionListener(this);
pnInput.add(btnfrac);
pnHistory = new JPanel();
contentPane.add(pnHistory, BorderLayout.EAST);
pnHistory.setPreferredSize(new Dimension(120, 200));
list DefaultListModel();
pnHistory.setLayout(new BorderLayout(5, 5));
list = new JList(listData);
pnHistory.add(list);
/*
* End of GUI code
*/
// initialize data
num1 = NONE;
num2 = NONE;
operator = NONE;
}
// Receive the events from all buttons
@Override
public void actionPerformed(ActionEvent e) {
// Get the string from the button
String s = e.getActionCommand();
// Check if the button is number
if (s.compareToIgnoreCase("0") >= 0 && s.compareToIgnoreCase("9") <= 0) {
// if textFiled is for inputting (editable) then append the number
// else the set editable and set the number to the text
if (textField.isEditable()) {
// insert the number to last position of textField
String curText = textField.getText();
textField.setText(curText + s);
} else {
textField.setEditable(true);
textField.setText(s);
}
}
// Check if the button is operator
else if (s.equals("+") || s.equals("-") || s.equals("x")
|| s.equals("/")) {
// check if there is operator exist
if (operator.equals(NONE)) {
// update the data
num1 = textField.getText();
} else {
// get the number2
num2 = textField.getText();
// calculate the previous operator before store the new one
num1 = doMath(num1, operator, num2);
}
// display the result
textField.setText(num1);
textField.setEditable(false);
// reset the input filed
operator = s;
num2 = NONE;
}
/**
* To do
* Add action listener for buttons x^2, 1/x
*/
// check if button is '='
else if (s.equals("=")) {
// get the number2
num2 = textField.getText();
// calculate the previous operator before store the new one
num1 = doMath(num1, operator, num2);
// display the result
textField.setText(num1);
textField.setEditable(false);
// reset the input filed
operator = NONE;
num1 = NONE;
num2 = NONE;
}
}
/**
* Do the operator and return the value in String.
*
* @param num1 1st number
* @param num2 2nd number
* @param op the operator
* @return result
*/
private String doMath(String num1, String op, String num2) {
// convert to number
int number1 = Integer.parseInt(num1);
int number2 = Integer.parseInt(num2);
// do the operator
switch (op) {
case "+":
number1 = number1 + number2;
break;
case "-":
number1 = number1 - number2;
break;
case "x":
number1 = number1 * number2;
break;
case "/":
number1 = number1 / number2;
break;
case "x²":
number1 = number1 * number1;
case "1/x":
number1 = 1 / number1;
/**
* To do
* Add the operator like x^2, 1/x
*/
default:
break;
}
// convert to string and return it
return ("" + number1);
}
}
여기서 x^2이랑 1/x 구하는 코드 짤 수 있는사람 있을까..?
제발 도와줘 난 똥멍청이이ㅑ.....ㅜㅠ
와 게이야 의지의 한국인이네
근데 저기 밑에 만든 거 아니야?