计算器
实现的功能效果:
1、界面
按钮、输出框、背景图
2、计算
加、减、乘、除、清零
知识点:
1、绘制窗体,设置窗体标题、大小,固定窗体大小、位置,设置关闭方式、窗体可见
2、从电脑本地导入图片并绘制到窗体
3、 非顶级容器或组件的大小设置
4、单行文本框与多行文本域的添加
5、用数组批量添加按钮等组件
6、给窗体容器、组件加监听
7、在监听类中处理各类动作事件
8、把图片路径加到Lable上可以直接显示图片
9、设置按钮透明度、按钮字体颜色
10、设置布局
实现思路:
把计算器看做一个对象,创建这样一个对象就需要一个类,那么就有相应的属性和方法
属性:
窗体JFrame、按钮、背景图、文本框
方法:
按键监听方法,在监听类中写出对应的事件处理方法
通过方法实现---->点击按钮输出相应的值在文本框,点击等于号则输出结果
//这是有主方法的类
public class Counter extends JPanel {
public ImageIcon im;
public Image img;
public Graphics g;
public static void main(String[] args) {
Counter counter = new Counter();
counter.run();
}
public void run() {
CouterListener couterListener = new CouterListener();
// 窗体属性
JFrame frame = new JFrame("Keivn专属计算器");
FlowLayout fl = new FlowLayout(FlowLayout.CENTER, 10, 10);// 上下左右间距为10
frame.setLayout(fl);
frame.setSize(270, 550);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(3);
frame.setResizable(false);// 固定窗体的大小
// 背景
im = new ImageIcon("plane\\cat_500_500.jpg");
JLabel label = new JLabel(im);
label.setPreferredSize(new Dimension(250, 200));// 非顶级容器大小设置方法
frame.add(label);
// 文本域
JTextArea field = new JTextArea();// 多行文本域
field.setPreferredSize(new Dimension(250, 50));
field.setRows(3);// 设置文本域的行数
frame.add(field);
couterListener.setJText(field);
// 按钮
String btArray[] = { "1", "2", "3", "+", "4", "5", "6", "-", "7", "8",
"9", "*", ".", "0", "=", "/" };
this.setLayout(new GridLayout(4, 4, 10, 10));
frame.add(this);
for (int i = 0; i < btArray.length; i++) {
JButton button = new JButton(BtArray[i]);
button.setFont(new Font("宋体", Font.PLAIN, 30));// 设置按钮字体、大小、颜色
button.setForeground(Color.yellow);
button.setPreferredSize(new Dimension(50, 50));
// button.setOpaque(false);//设置按钮透明度
// button.setBackground(new Color(255, 255, 255));
button.setContentAreaFilled(false); // 设置按钮透明度(方法二), 只须加上此句
this.add(button);
button.addActionListener(couterListener);// 给按钮加监听
}
frame.setVisible(true);
g = frame.getGraphics();
}
}
这是监听类
public class CouterListener implements ActionListener {
private JTextArea JF;
public int x = 0;
public int flag = 1;
public Double resulte = 0.0, temp;
public String num[] = new String[3];
public String count;
public void setJText(JTextArea F) {// 接受文本框,用于监听输出
JF = F;
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand() == "+" || e.getActionCommand() == "-"
|| e.getActionCommand() == "*" || e.getActionCommand() == "/") {
JF.setText(JF.getText() + e.getActionCommand() + "\n");
if (flag == 1)
resulte = temp;
count = e.getActionCommand();
flag = 0;
} else if (e.getActionCommand() == "=") {
if (count == "+") {
resulte = resulte + temp;
JF.setText(resulte + "");
} else if (count == "-") {
resulte = resulte - temp;
JF.setText(resulte + "");
} else if (count == "*") {
resulte = resulte * temp;
JF.setText(resulte + "");
} else if (count == "/") {
resulte = resulte / temp;
JF.setText(resulte + "");
}
} else {// 如果输入非数字输出到文本框并换行
JF.setText(JF.getText() + e.getActionCommand());
if (flag == 0) {
num = JF.getText().split("\\n");
temp = Double.parseDouble(num[1]);
} else {
temp = Double.parseDouble(JF.getText());
}
}
}
}
提示:Ctrl+Shift+O 可以快捷导入所有包