⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 9_2_030421217.java

📁 开始学JAVA老师布置的作业一学期的哦希望新手有用
💻 JAVA
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculate implements ActionListener
{
   
    String s="",s1;
    double d1,d2;
JFrame jf = new JFrame("计算器") ;


JTextField tf = new JTextField();

public void init()
{
Container c=jf.getContentPane();
tf.setHorizontalAlignment(JTextField.RIGHT);
c.add(tf,"North");

JPanel pn3 = new JPanel(new BorderLayout());
c.add(pn3,"Center");

JPanel pn2 = new JPanel();
pn2.setLayout(new BorderLayout());

JPanel pn1 = new JPanel();

pn1.setLayout(new GridLayout(4,4));

pn3.add(pn2,"North");
pn3.add(pn1);

//设置按钮
JButton b = new JButton("CLEAR");
b.setToolTipText("请按清除键!");
b.setForeground(Color.RED);
b.setBackground(Color.YELLOW);
b.addActionListener(this);
pn2.add(b,"Center");
b = new JButton("OFF");
b.setToolTipText("请按退出键!");
b.addActionListener(this);
b.setForeground(Color.RED);
b.setBackground(Color.ORANGE);
pn2.add(b,"East");
b = new JButton("1");
b.addActionListener(this);
pn1.add(b);
b = new JButton("2");
b.addActionListener(this);
pn1.add(b);
b = new JButton("3");
b.addActionListener(this);
pn1.add(b);
b = new JButton("+");
b.setForeground(Color.BLUE);
b.addActionListener(this);
pn1.add(b);
b = new JButton("4");
b.addActionListener(this);
pn1.add(b);
b = new JButton("5");
b.addActionListener(this);
pn1.add(b);
b = new JButton("6");
b.addActionListener(this);
pn1.add(b);
b = new JButton("-");
b.setForeground(Color.BLUE);
b.addActionListener(this);
pn1.add(b);
b = new JButton("7");
b.addActionListener(this);
pn1.add(b);
b = new JButton("8");
b.addActionListener(this);
pn1.add(b);
b = new JButton("9");
b.addActionListener(this);
pn1.add(b);
b = new JButton("*");
b.setForeground(Color.BLUE);
b.addActionListener(this);
pn1.add(b);
b = new JButton("0");
b.addActionListener(this);
pn1.add(b);
b = new JButton(".");
b.addActionListener(this);
pn1.add(b);
b = new JButton("=");
b.setForeground(Color.RED);
b.addActionListener(this);
pn1.add(b);
b = new JButton("\\");
b.setForeground(Color.BLUE);
b.addActionListener(this);
pn1.add(b);

jf.setSize(300,300);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void actionPerformed(ActionEvent e)
{
        String command = e.getActionCommand();
        tf.setText(tf.getText()+command);
        if(command.equals("CLEAR")) 
        {
        s1=null;
        s="";
            tf.setText("");
        }
       
        else if(command.equals("OFF")) System.exit(0);
       
        else if(!command.equals("*")&&!command.equals("\\")
           &&!command.equals("+")&&!command.equals("-")
           &&!command.equals("="))
        {
        if(s1==null)
            s1 = command;
            else s1+=command;
    d1 = new Double(s1).doubleValue();
    try
    {
    if(s.equals("+")) d1 = d1+d2;//加
    else if(s.equals("-")) d1 = d2-d1;//减
    else if(s.equals("*")) d1 = d1*d2;//乘
    else if(s.equals("\\"))d1 = d2/d1;//除
    }
    catch(Exception ex)
    {
    tf.setText("Error");
    System.out.println(ex.getMessage());
    }
    
}

else if(!command.equals("=")) //判断输入是否为+ - * \ 
{
    s = command;
s1 = null;
d2 = d1;
}

else//输入=时,显示运算结果
{

tf.setText(tf.getText()+d1);

}

}

public static void main(String [] args)
{
   
new Calculate().init();

}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -