package Caculater; import java.awt.Button; import java.awt.Font; import java.awt.TextArea; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JTextArea; import javax.swing.JTextField; public class Caculater extends JFrame { private JTextField resultText = new JTextField(""); public Caculater() { setSize(420, 410); setLocation(700, 300); setVisible(true); setLayout(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); /*菜单栏*/ JMenuBar mb=new JMenuBar(); JMenu m1=new JMenu("编辑"); JMenuItem mnuCopy=new JMenuItem("复制(C)"); JMenuItem mnuPaste=new JMenuItem("粘贴(P)"); setJMenuBar(mb); mb.add(m1); m1.add(mnuCopy); m1.add(mnuPaste); /*字体*/ Font font=new Font("", ALLBITS, 20); /*结果框*/ resultText.setBounds(0, 0, 400, 50); resultText.setFont(font); add(resultText); /*按键 baskpace*/ Button baskpace=new Button("baskpace"); baskpace.setBounds(0, 50, 200, 50); add(baskpace); baskpace.addActionListener(new Funtion(resultText)); Button c=new Button("C"); c.setBounds(200, 50, 200, 50); add(c); c.addActionListener(new Funtion(resultText)); Button siven=new Button("7"); siven.setBounds(0, 100, 100, 50); add(siven); siven.addActionListener(new Funtion(resultText)); Button eight=new Button("8"); eight.setBounds(100, 100, 100, 50); add(eight); eight.addActionListener(new Funtion(resultText)); Button nine=new Button("9"); nine.setBounds(200, 100, 100, 50); add(nine); nine.addActionListener(new Funtion(resultText)); Button div=new Button("/"); div.setBounds(300, 100, 100, 50); add(div); div.addActionListener(new Funtion(resultText)); Button four=new Button("4"); four.setBounds(0, 150, 100, 50); add(four); four.addActionListener(new Funtion(resultText)); Button fine=new Button("5"); fine.setBounds(100, 150, 100, 50); add(fine); Button six=new Button("6"); six.setBounds(200, 150, 100, 50); add(six); Button mul=new Button("*"); mul.setBounds(300, 150, 100, 50); add(mul); Button one=new Button("1"); one.setBounds(0, 200, 100, 50); add(one); one.addActionListener(new Funtion(resultText)); Button two=new Button("2"); two.setBounds(100, 200, 100, 50); add(two); Button three=new Button("3"); three.setBounds(200, 200, 100, 50); add(three); Button sub=new Button("-"); sub.setBounds(300, 200, 100, 50); add(sub); Button zero=new Button("0"); zero.setBounds(0, 250, 100, 50); add(zero); Button addsub=new Button("+/-"); addsub.setBounds(100, 250, 100, 50); add(addsub); Button point=new Button("."); point.setBounds(200, 250, 100, 50); add(point); Button add=new Button("+"); add.setBounds(300, 250, 100, 50); add(add); Button onex=new Button("1/x"); onex.setBounds(0, 300, 100, 50); add(onex); Button hund=new Button("%"); hund.setBounds(100, 300, 100, 50); add(hund); Button sqrt=new Button("sqrt"); sqrt.setBounds(200, 300, 100, 50); add(sqrt); Button result=new Button("="); result.setBounds(300, 300, 100, 50); add(result); } public static void main(String[] args) { new Caculater(); } }
funtion
package Caculater; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JTextField; public class Funtion implements ActionListener { JTextField resultText; public Funtion(JTextField resultText){ this.resultText=resultText; } public void actionPerformed(ActionEvent e) { if(e.getActionCommand().equals("1")) { resultText.setText("123456789"); } } }