我正在尝试开发一个简单的计算器软件,如下图所示。它应该要求用户输入第一个数字,然后是第二个数字,并为用户提供一个选择(加或减),最后在下面的框中显示结果。到目前为止,我只是做了 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 ; }}