package Calculator;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.AbstractListModel;
import javax.swing.Box;
import javax.swing.ComboBoxEditor;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
/**
* JButton 代表Swing按钮,按钮中可以带一些图片或者文字
JCheckbox 代表Swing中的复选框组件
JCombox 代表下拉列表框,可以在下拉显示区域显示多个选项
JFrame(这个经常使用) 代表Swing的框架类
JDialog 代表Swing版本的对话框
JLabel 代表Swing中的标签组件
JRadioButton 代表Swing的单选按钮
JList 代表能够再用化界面中显示一系列条目的组件
JTextField 代表文本框
JPasswordField 代表密码框
JTextArea 代表Swing中的文本区域
JOptionPane 代表Swing中的一些对话框
* @author Administrator
*
*/
public class Caclour extends JFrame {
private JPanel contentPane;//容器
private JTextField textField;//代表文本框
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
JComboBox comboBox;
public String m;
/**
* Launch the application.
*/
public static void main(String[] args) {
//队列,进程执行完立即销毁
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Caclour frame = new Caclour();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Caclour() {
//默认点关闭时只是隐藏窗体,,在后台进程中还可以看到
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 305, 275);//组件x,y,width,height
contentPane = new JPanel();//容器
//设置窗口的边窗宽度啊,间距啦。。就是窗口的属性
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
//设置其 Content Pane 的
setContentPane(contentPane);
//设置内容窗口的布局是空布局,自己随便布局
contentPane.setLayout(null);
//标签
JLabel num1 = new JLabel("操作数1:");
//设置字体,宋体,细体,16号子
num1.setFont(new Font("宋体", Font.PLAIN, 16));
//租金啊x,y,width,height
num1.setBounds(39, 10, 100, 20);
//在容器里增加这个标签
contentPane.add(num1);
textField = new JTextField();//文本框
textField.setBounds(151, 11, 128, 21);
contentPane.add(textField);
//设置这个文本框有10列
textField.setColumns(10);
JLabel num2 = new JLabel("操作数2:");
num2.setFont(new Font("", Font.PLAIN, 16));
num2.setBounds(39, 62, 72, 20);
contentPane.add(num2);
textField_1 = new JTextField();
textField_1.setBounds(151, 63, 128, 21);
contentPane.add(textField_1);
textField_1.setColumns(10);
JLabel type = new JLabel("计算类型");
type.setFont(new Font("", Font.PLAIN, 16));
type.setBounds(39, 121, 89, 27);
contentPane.add(type);
JLabel result = new JLabel("结果为");
result.setFont(new Font("", Font.PLAIN, 16));
result.setBounds(151, 184, 72, 22);
contentPane.add(result);
textField_2 = new JTextField();
textField_2.setFont(new Font("", Font.PLAIN, 16));
textField_2.setBounds(213, 186, 66, 21);
contentPane.add(textField_2);
textField_2.setColumns(10);
//代表下拉列表框,可以在下拉显示区域显示多个选项
comboBox = new JComboBox();
//comboBox.addActionListener(new BtnActionListener());
//setModel必须先定义一个DefaultTableModel 类型,之后直接将设置的类型给comboBox赋值即可。
comboBox.setModel(new DefaultComboBoxModel(new String[] {"+", "-", "*", "/"}));
//comboBox.addItem("+");
//comboBox.addItem("-");
//comboBox.addItem("*");
//comboBox.addItem("/");
comboBox.setFont(new Font("", Font.PLAIN, 16));
comboBox.setBounds(151, 125, 46, 20);
contentPane.add(comboBox);
JButton btn = new JButton("计算");
//为btn控件添加一个点击监听事件
btn.addActionListener(new BtnActionListener());
btn.setFont(new Font("", Font.PLAIN, 16));
btn.setBounds(39, 182, 72, 27);
contentPane.add(btn);
}
private class BtnActionListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
//组件.setText(String str);将组件上的文字内容设置为str
m=comboBox.getSelectedItem().toString();
if(m=="+")
{
String s=textField.getText();
double a=Double.parseDouble(s);
String s1=textField_1.getText();
double b=Double.parseDouble(s1);
double c=a+b;
String d=Double.toString(c);
textField_2.setText(d);
}
else if(m=="-")
{
String s=textField.getText();
double a=Double.parseDouble(s);
String s1=textField_1.getText();
double b=Double.parseDouble(s1);
double c=a-b;
String d=Double.toString(c);
textField_2.setText(d);
}
else if(m=="*")
{
String s=textField.getText();
double a=Double.parseDouble(s);
String s1=textField_1.getText();
double b=Double.parseDouble(s1);
double c=a*b;
String d=Double.toString(c);
textField_2.setText(d);
}
else if(m=="/")
{
String s=textField.getText();
double a=Double.parseDouble(s);
String s1=textField_1.getText();
double b=Double.parseDouble(s1);
if(b==0)
{
JOptionPane.showInputDialog("除数不能为0");
}
else if(b!=0)
{
double c=a/b;
String d=Double.toString(c);
textField_2.setText(d);
}
}
//textField_2.setText(textField.getText()+textField_1.getText());
}
}
}
实现结果如下: