日期:2014-05-20 浏览次数:20960 次
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.*; import javax.swing.*; public class CaluateFrame extends JFrame implements ActionListener{ /** * @author: mafer */ //面板和滚动条 private JPanel jp;//总的面板 private JPanel jp1,jsp_jp2 = null; private JScrollPane jsp = null; //输入框 private JTextField jtf; //数字键 private JButton jb0,jb1,jb2,jb3,jb4,jb5,jb6,jb7,jb8,jb9; //运算符键 private JButton jbDian,jbAdd,jbSub,jbSum,jbMul,jbChu,jbYuShu,jbZhengFU; //功能键 private JButton jbDel,jbClear; //存储 每次结果的文本框 private JTextArea jtaResult; //设置程序坐标及大小 参数 private static final Rectangle size = new Rectangle(100, 100, 280, 400); Font f = new Font("宋体",Font.PLAIN,28); Font f2 = new Font("宋体",Font.PLAIN,18); //界面制作 CaluateFrame(){ this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setBounds(size); // this.setResizable(false); //设置大小不可调整 jp = new JPanel(new BorderLayout()); // jp = new JPanel(); // this.setContentPane(jp); this.add(jp); //在北边添加 输入框 jtf = new JTextField("", 10); jtf.setFont(f); jp.add(jtf,BorderLayout.NORTH); //在中间添加按钮控件 //jb0,jb1,jb2,jb3,jb4,jb5,jb6,jb7,jb8,jb9; jb0 = new JButton("0"); jb1 = new JButton("1"); jb2 = new JButton("2"); jb3 = new JButton("3"); jb4 = new JButton("4"); jb5 = new JButton("5"); jb6 = new JButton("6"); jb7 = new JButton("7"); jb8 = new JButton("8"); jb9 = new JButton("9"); //jbDian,jbAdd,jbSum,jbCheng,jbChu,jbYuShu,jbZhengFU; jbDian = new JButton("."); jbAdd = new JButton("+"); jbSub = new JButton("-"); jbMul = new JButton("*"); jbChu = new JButton("/"); jbYuShu = new JButton("%"); jbZhengFU = new JButton("+_"); jbSum = new JButton("="); //jbDel,jbClear jbDel = new JButton("del"); jbClear = new JButton("c"); //添加上述按钮到中间面板中 jp1 = new JPanel(new GridLayout(5,4)); jp1.add(jbDel); jp1.add(jbClear); jp1.add(jbZhengFU); jp1.add(jbYuShu); //第一行 jp1.add(jb1); jp1.add(jb2); jp1.add(jb3); jp1.add(jbAdd); //第二行 jp1.add(jb4); jp1.add(jb5); jp1.add(jb6); jp1.add(jbSub); //第三行 jp1.add(jb7); jp1.add(jb8); jp1.add(jb9); jp1.add(jbMul); //第四行 jp1.add(jb0); jp1.add(jbDian); jp1.add(jbSum); jp1.add(jbChu); //第五行 jp.add(jp1,BorderLayout.CENTER); //南部 ---显示每次计算后的表达式 jtaResult = new JTextArea("显示每次的表达式\n",4,30); jtaResult.setFont(f2); jsp_jp2 = new JPanel(); jsp = new JScrollPane(jsp_jp2,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED); jsp_jp2.add(jtaResult); jp.add(jsp,BorderLayout.SOUTH); // this.add(jtf); // this.add(jp1); // this.getContentPane().add(jsp); } public static void main(String[] args) { // TODO Auto-generated method stub new CaluateFrame(); } public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub } }