/**
*程序编排不怎么好
*设计界面也是按自己的意愿做的
*但是执行还好.(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);
}
}
}