关闭

Java实现简单的计算器(GUI编程)

标签: 编程javalayoutimportn2string
1766人阅读 评论(1) 收藏 举报
分类:

/**

*程序编排不怎么好

*设计界面也是按自己的意愿做的

*但是执行还好.(Eclipse编写代码)

*@author 毛毛

*/

 

package GUI编程;
import javax.swing.*;
import java.awt.*;
import java.awt.Event.*;
import java.awt.Component;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/*
 * 加/减, 乘/除 计算器
 * plus/minus , multiplication and division 
 */
public class CalculatorDemo extends JFrame
 implements ActionListener{

 private JLabel lbNum1, lbNum2, lbResult;
 private JTextField tfNum1, tfNum2;
 private JTextField tfResult;
 private JPanel InputPanel, RadioPanel, CalPanel;
 private JRadioButton rbPlus,rbMinus,rbMul,rbDiv;
 private JButton btnOk, btnClear;
 private ButtonGroup btg;
 
 private Container con;
 private GridBagLayout Layout;
 private GridBagConstraints cons;
 
 int n1, n2, sum;
 
 public CalculatorDemo(){
  
  super("计算器示例");
  setSize(350,260);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  con = this.getContentPane();
  con.setLayout(new GridLayout(3,1));
 }
 
 //以设定的网格包布局向面板中添加组件
 private void addComponent(JPanel panel, GridBagLayout Layout,
   GridBagConstraints gbc, Component com, int row, int column,
   int numRows, int numColumns, int Weightx, int Weighty){
  
  gbc.gridx = row;
  gbc.gridy = column;
  gbc.gridwidth = numRows;
  gbc.gridheight = numColumns;
  gbc.weightx = Weightx;
  gbc.weighty = Weighty;
  
  Layout.setConstraints(com, gbc);
  panel.add(com);
 }
 
 public void setLayout(){
  
  Layout = new GridBagLayout();
  cons = new GridBagConstraints();
  InputPanel = new JPanel();
  InputPanel.setLayout(Layout);
  
  cons.anchor = GridBagConstraints.CENTER;
 
  lbNum1 = new JLabel("数字1");
  cons.fill = GridBagConstraints.NONE;
  addComponent(InputPanel, Layout, cons, lbNum1,
    0, 0, 1, 1, 6, 0);  
  //创建数字1文本框和输入面板
  tfNum1 = new JTextField();
  cons.fill = GridBagConstraints.HORIZONTAL;
  addComponent(InputPanel, Layout, cons, tfNum1,
    1, 0, 1, 1, 80, 100);
    
  lbNum2 = new JLabel("数字2");
  cons.fill = GridBagConstraints.NONE;
  addComponent(InputPanel, Layout, cons, lbNum2,
    0, 1, 1, 1, 6, 0);  
  //创建数字2文本框和输入面板
  tfNum2 = new JTextField();
  cons.fill = GridBagConstraints.HORIZONTAL;
  addComponent(InputPanel, Layout, cons, tfNum2,
    1, 1, 1, 1, 80, 100);
  
  
  lbResult = new JLabel("结果");
  cons.fill = GridBagConstraints.NONE;
  addComponent(InputPanel, Layout, cons, lbResult,
    0, 2, 1, 1, 6, 0);  
  //创建姓名文本框和输入面板
  tfResult = new JTextField();
  cons.fill = GridBagConstraints.HORIZONTAL;
  addComponent(InputPanel, Layout, cons, tfResult,
    1, 2, 1, 1, 80, 100);
  
  //添加按钮
  RadioPanel = new JPanel();
  RadioPanel.setLayout(new GridLayout(1,4));
  
  rbPlus = new JRadioButton("加");
  rbPlus.addActionListener(this);
  //rbPlus.setSelected(true);
  
  rbMinus = new JRadioButton("减");
  rbMinus.addActionListener(this);
  
  rbMul = new JRadioButton("乘");
  rbMul.addActionListener(this);
  
  rbDiv = new JRadioButton("除"); 
  rbDiv.addActionListener(this);
  
  btg = new ButtonGroup();
  btg.add(rbPlus);
  btg.add(rbMinus);
  btg.add(rbMul);
  btg.add(rbDiv);
  RadioPanel.add(rbPlus);
  RadioPanel.add(rbMinus);
  RadioPanel.add(rbMul);
  RadioPanel.add(rbDiv);
  
  CalPanel = new JPanel();
  CalPanel.setLayout(null);
  
  btnOk = new JButton("运  算");
  CalPanel.add(btnOk);
  btnOk.setSize(70, 30);
  btnOk.setBounds(20, 20, 70, 30);
  
  btnClear = new JButton("清  除");
  CalPanel.add(btnClear);
  btnClear.setSize(70, 30);
  btnClear.setBounds(120,20,70,30);
    
  //将面板添加到框架
  con.add(InputPanel);
  con.add(RadioPanel);
  con.add(CalPanel);
  
  //监听器
  btnOk.addActionListener(this);
  btnClear.addActionListener(this);
 }
 public static void main(String[] args) {
  
        CalculatorDemo frame = new CalculatorDemo();
        frame.setLayout();
        frame.show();
 }

 @Override
 public void actionPerformed(ActionEvent e) {
  Double Result = null;
  Double n1,n2;
  String s1,s2;
  String ss;
  n1 = Double.parseDouble(tfNum1.getText());
  n2 = Double.parseDouble(tfNum2.getText());
  
  if(e.getSource() == btnOk){   
   if(rbPlus.isSelected()){    
    Result = n1 + n2;    
   }
   else if(rbMinus.isSelected()){
    Result = n1 - n2;   
   }
   else if(rbMul.isSelected()){
    Result = n1 * n2;    
   }
   else if(rbDiv.isSelected()){
    Result = n1 / n2;    
   }
   else{
    
    JOptionPane.showMessageDialog(null, "没有选中按钮",
      "提示", JOptionPane.INFORMATION_MESSAGE);
   }
      ss = Result.toString();
   tfResult.setText(ss);
   InputPanel.add(tfResult); 
   } 
  if(e.getSource() == btnClear){
   tfNum1.setText("");
   tfNum2.setText("");
   tfResult.setText("");
   InputPanel.add(tfNum1);
   InputPanel.add(tfNum2);
   InputPanel.add(tfResult); 
  }
 } 
}
 

0
0

猜你在找
【直播】机器学习&数据挖掘7周实训--韦玮
【套餐】系统集成项目管理工程师顺利通关--徐朋
【直播】3小时掌握Docker最佳实战-徐西宁
【套餐】机器学习系列套餐(算法+实战)--唐宇迪
【直播】计算机视觉原理及实战--屈教授
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之矩阵--黄博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之凸优化--马博士
【套餐】Javascript 设计模式实战--曾亮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:22896次
    • 积分:418
    • 等级:
    • 排名:千里之外
    • 原创:7篇
    • 转载:19篇
    • 译文:0篇
    • 评论:4条
    文章分类