我自己做出了外部外形模块,找了很多资料,大致的思路是:
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();
}
}
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();
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货