大学刚接触java,期末编写一个亲戚计算器,求指教

LINMINGLEA 2017-06-15 10:51:11
我自己做出了外部外形模块,找了很多资料,大致的思路是:
1,先创建外部界面,外圈用BorderLayout,内部用GridLayout,做出计算器面板。
2,为每个button注册监听器并且实现点击按钮上面的JTextArea板块会输出相应的按钮内容。
3,把每种情况都用if,else提前编写好,使用时可以满足所有计算。
但现在卡在了第二监听器注册完之后没办法实现点击之后JTextArea会输出对应文字,还有第三部没有任何思路,不知从何处下手。。。

下面是我的代码,求各位大神帮忙修改完成。。。我的第一个java项目,实在是没办法了。。
package aa;
import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

@SuppressWarnings("serial")
public class Calculator extends JFrame{

public Calculator() {
// TODO 自动生成的方法存根
JFrame cal=new JFrame("亲戚计算器");
cal.setLayout(new BorderLayout());
cal.setSize(500, 600);
cal.setLocationRelativeTo(null);
JTextArea tf = new JTextArea(6,24);
tf.setBorder(BorderFactory.createLineBorder(Color.red,2));

tf.setEditable(false);
JPanel jp = new JPanel();
jp.setLayout(new GridLayout(4,4));

ButtonListener btnListener=new ButtonListener();

Button bt1=new Button("父");
bt1.setBounds(5, 120, 55, 55);
bt1.setBackground(new Color(95, 95, 95));
bt1.setForeground(Color.white);
bt1.setFont(new Font("宋体", Font.BOLD, 20));
jp.add(bt1);
bt1.addActionListener(btnListener);

Button bt2=new Button("母");
bt2.setBounds(5, 120, 55, 55);
bt2.setBackground(new Color(95, 95, 95));
bt2.setForeground(Color.white);
bt2.setFont(new Font("宋体", Font.BOLD, 20));
jp.add(bt2);
bt2.addActionListener(btnListener);

Button bt3=new Button("CE");
bt3.setBounds(5, 120, 55, 55);
bt3.setBackground(new Color(95, 95, 95));
bt3.setForeground(Color.white);
bt3.setFont(new Font("宋体", Font.BOLD, 20));
jp.add(bt3);
bt3.addActionListener(btnListener);

Button bt4=new Button("AC");
bt4.setBounds(5, 120, 55, 55);
bt4.setBackground(new Color(95, 95, 95));
bt4.setForeground(Color.white);
bt4.setFont(new Font("宋体", Font.BOLD, 20));
jp.add(bt4);
bt4.addActionListener(btnListener);

Button bt5=new Button("兄");
bt5.setBounds(5, 120, 55, 55);
bt5.setBackground(new Color(95, 95, 95));
bt5.setForeground(Color.white);
bt5.setFont(new Font("宋体", Font.BOLD, 20));
jp.add(bt5);
bt5.addActionListener(btnListener);

Button bt6=new Button("弟");
bt6.setBounds(5, 120, 55, 55);
bt6.setBackground(new Color(95, 95, 95));
bt6.setForeground(Color.white);
bt6.setFont(new Font("宋体", Font.BOLD, 20));
jp.add(bt6);
bt6.addActionListener(btnListener);

Button bt7=new Button("姐");
bt7.setBounds(5, 120, 55, 55);
bt7.setBackground(new Color(95, 95, 95));
bt7.setForeground(Color.white);
bt7.setFont(new Font("宋体", Font.BOLD, 20));
jp.add(bt7);
bt7.addActionListener(btnListener);

Button bt8=new Button("妹");
bt8.setBounds(5, 120, 55, 55);
bt8.setBackground(new Color(95, 95, 95));
bt8.setForeground(Color.white);
bt8.setFont(new Font("宋体", Font.BOLD, 20));
jp.add(bt8);
bt8.addActionListener(btnListener);

Button bt9=new Button("夫");
bt9.setBounds(5, 120, 55, 55);
bt9.setBackground(new Color(95, 95, 95));
bt9.setForeground(Color.white);
bt9.setFont(new Font("宋体", Font.BOLD, 20));
jp.add(bt9);
bt9.addActionListener(btnListener);

Button bt10=new Button("妻");
bt10.setBounds(5, 120, 55, 55);
bt10.setBackground(new Color(95, 95, 95));
bt10.setForeground(Color.white);
bt10.setFont(new Font("宋体", Font.BOLD, 20));
jp.add(bt10);
bt10.addActionListener(btnListener);

Button bt11=new Button("子");
bt11.setBounds(5, 120, 55, 55);
bt11.setBackground(new Color(95, 95, 95));
bt11.setForeground(Color.white);
bt11.setFont(new Font("宋体", Font.BOLD, 20));
jp.add(bt11);
bt11.addActionListener(btnListener);

Button bt12=new Button("女");
bt12.setBounds(5, 120, 55, 55);
bt12.setBackground(new Color(95, 95, 95));
bt12.setForeground(Color.white);
bt12.setFont(new Font("宋体", Font.BOLD, 20));
jp.add(bt12);
bt12.addActionListener(btnListener);

Button bt13=new Button("+");
bt13.setBounds(5, 120, 55, 55);
bt13.setBackground(new Color(95, 95, 95));
bt13.setForeground(Color.white);
bt13.setFont(new Font("宋体", Font.BOLD, 20));
jp.add(bt13);
bt13.addActionListener(btnListener);

Button bt14=new Button("=");
bt14.setBounds(5, 120, 55, 55);
bt14.setBackground(new Color(95, 95, 95));
bt14.setForeground(Color.white);
bt14.setFont(new Font("宋体", Font.BOLD, 20));
jp.add(bt14);
bt14.addActionListener(btnListener);

Button bt15=new Button("");
bt15.setBounds(5, 120, 55, 55);
bt15.setBackground(new Color(95, 95, 95));
bt15.setForeground(Color.white);
bt15.setFont(new Font("宋体", Font.BOLD, 20));
jp.add(bt15);
bt15.addActionListener(btnListener);

Button bt16=new Button("");
bt16.setBounds(5, 120, 55, 55);
bt16.setBackground(new Color(95, 95, 95));
bt16.setForeground(Color.white);
bt16.setFont(new Font("宋体", Font.BOLD, 20));
jp.add(bt16);
bt16.addActionListener(btnListener);


cal.add(tf,BorderLayout.NORTH);
cal.add(jp,BorderLayout.CENTER);
cal.setVisible(true);


}

class ButtonListener implements ActionListener{

@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根


}

}

public static void main(String[] args){
new Calculator();
}
}
...全文
540 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
LINMINGLEA 2017-06-15
  • 打赏
  • 举报
回复
引用 1 楼 我爱娃哈哈的回复:
我觉得难得不是技术 是亲戚的关系 反正我是理不清
但是我现在技术也有很大的难题啊第一次做自己的java项目,我们连传智播客那本《java基础入门》都没讲完,就给我们留这种的作业,唉。
我爱娃哈哈 2017-06-15
  • 打赏
  • 举报
回复
我觉得难得不是技术 是亲戚的关系 反正我是理不清

62,629

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧