아래 그림과 같은 간단한 계산기 소프트웨어를 개발하려고합니다. 사용자에게 첫 번째 숫자를 입력 한 다음 두 번째 숫자를 입력하고 사용자에게 (추가 또는 빼기) 중 하나를 선택하도록 요청하고, 결과를 아래 상자에 마지막으로 표시해야합니다. 지금까지 GUI를 수행 했으므로이 코드를 내가 수행 한 코드에 어떻게 추가 할 수 있는지 알고 싶습니다. 여기 간단한 계산기 (추가 - 서브)
는import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class SimpleMath extends JFrame /*implements ActionListener*/
{
private JLabel label ; //Firstnumber
private JLabel labe2; // Second number
private JLabel labe3; // result
private JButton Add;// add
private JButton Sub; // subtrack
private JTextField inputLine1;// enter the firstnumber
private JTextField inputLine2;//enter the second number
private JTextArea textArea;//the result
public static void main (String [] args)
{
}
public SimpleMath() {
Container contentPane = getContentPane();
setSize (300, 300);
setResizable (false);
setTitle ("Simple Math");
setLocation (200, 300);
contentPane.setLayout(null);
//lebal 1
label = new JLabel("First number") ;
label. setBounds(15 , 30 , 150 , 50) ;
contentPane.add(label);
//lebal2
labe2 = new JLabel("Second number") ;
label. setBounds(15 , 30 ,1700 , 70) ;
contentPane.add(label);
//lebal3
labe3 = new JLabel("Result") ;
label. setBounds(15 , 30 ,200 , 100) ;
contentPane.add(label);
//text input1 first number
inputLine1 = new JTextField();
inputLine1.setColumns(10);
inputLine1.setFont(new Font("Courier", Font.PLAIN, 14));
inputLine1. setBounds(15 , 70 , 150 , 25) ;
contentPane.add(inputLine1);
// text input2 second number
inputLine2 = new JTextField();
inputLine2.setColumns(10);
inputLine2.setFont(new Font("Courier", Font.PLAIN, 14));
inputLine2. setBounds(15 , 70 , 150 , 25) ;
contentPane.add(inputLine2);
// add button
Add= new JButton("search ");
Add. setBounds(200 , 70 , 220 , 60) ;
contentPane.add(Add) ;
// sub button
Sub= new JButton("search ");
Sub. setBounds(200 , 70 , 240 , 60) ;
contentPane.add(Sub) ;
//text area that will show the result of the add and sub
textArea = new JTextArea();
textArea.setColumns(5);
textArea.setRows(2);
textArea.setBorder(BorderFactory.createLineBorder(Color.red));
textArea.setEditable(false);
textArea. setBounds(40 , 250 , 300 , 100) ;
contentPane.add(textArea);
return ; }}
당신의 JFrame의의 인스턴스를 만들고, 당신의 주요 방법은 당신은 단지 당신의 버튼에 이벤트 리스너를 부착 그들 작업을하고 –
꽤 비어 /javase/tutorial/uiswing/events/actionlistener.html – GoldRoger
http://docs.oracle.com을 결과를 표시 할 필요가 – RKC