我编写的Java计算器代码,但是我想实现菜单栏里复制粘贴的功能,求大神帮忙帮我看看要加什么代码才能实现 万分感谢 还要交作业
import java.awt.event.*;
import java.awt.event.KeyEvent;
import javax.swing.*;
public class calculator extends JFrame implements ActionListener{
JTextArea text;
public static void main(String args[]){
calculator m = new calculator();
m.setBounds(100,100,315,365);
m.setTitle("精简计算器");
}
public calculator(){
menu();
calculate();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void menu(){
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu menuFile = new JMenu("查看(V)");
JMenu menuFile1 = new JMenu("编辑(E)");
menuFile1.setMnemonic(KeyEvent.VK_E);
JMenu menuFile2 = new JMenu("帮助(H)");
menuFile2.setMnemonic(KeyEvent.VK_H);
menuBar.add(menuFile);
menuBar.add(menuFile1);
menuBar.add(menuFile2);
JMenu itemSuper = new JMenu("高级");
JMenu itemPrime = new JMenu("初级");
menuFile.add(itemSuper);
menuFile.add(itemPrime);
JMenu item1 = new JMenu("复制(C)");
JMenu item2 = new JMenu("粘贴(P)");
menuFile1.add(item1);
menuFile1.add(item2);
JMenu item3 = new JMenu("关于计算器");
menuFile2.add(item3);
item1.addActionListener(this);
}
void calculate(){
text = new JTextArea();
add(text);
JButton button1 = new JButton("1");
JButton button2 = new JButton("2");
JButton button3 = new JButton("3");
JButton button4 = new JButton("4");
JButton button5 = new JButton("5");
JButton button6 = new JButton("6");
JButton button7 = new JButton("7");
JButton button8 = new JButton("8");
JButton button9 = new JButton("9");
JButton button10 = new JButton("0");
JButton button11 = new JButton("+");
JButton button12 = new JButton("-");
JButton button13 = new JButton("*");
JButton button14 = new JButton("/");
JButton button15 = new JButton(".");
JButton button16 = new JButton("=");
JButton button17 = new JButton("C");
JButton button18 = new JButton("CE");
add(button1);
add(button2);
add(button3);
add(button11);
add(button4);
add(button5);
add(button6);
add(button12);
add(button7);
add(button8);
add(button9);
add(button13);
add(button15);
add(button10);
add(button16);
add(button14);
add(button17);
add(button18);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button11.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
button6.addActionListener(this);
button12.addActionListener(this);
button7.addActionListener(this);
button8.addActionListener(this);
button9.addActionListener(this);
button13.addActionListener(this);
button15.addActionListener(this);
button10.addActionListener(this);
button16.addActionListener(this);
button14.addActionListener(this);
button17.addActionListener(this);
button18.addActionListener(this);
setLayout(null);
text.setBounds(0,0,300,50);
button1.setBounds(0,50,75,50);
button2.setBounds(75,50,75,50);
button3.setBounds(150,50,75,50);
button11.setBounds(225,50,75,50);
button4.setBounds(0,100,75,50);
button5.setBounds(75,100,75,50);
button6.setBounds(150,100,75,50);
button12.setBounds(225,100,75,50);
button7.setBounds(0,150,75,50);
button8.setBounds(75,150,75,50);
button9.setBounds(150,150,75,50);
button13.setBounds(225,150,75,50);
button15.setBounds(0,200,75,50);
button10.setBounds(75,200,75,50);
button16.setBounds(150,200,75,50);
button14.setBounds(225,200,75,50);
button17.setBounds(0,250,150,50);
button18.setBounds(150,250,150,50);
text.setFont(new Font("黑体",Font.BOLD,35));
}
double result = 0;
String s = "";
public void operation(){
if(s == "+")
result = result + Double.parseDouble(text.getText());
else if(s == "-")
result = result - Double.parseDouble(text.getText());
else if(s == "*")
result = result * Double.parseDouble(text.getText());
else if(s == "/")
result = result / Double.parseDouble(text.getText());
else
result = Double.parseDouble(text.getText());
}
public void actionPerformed(ActionEvent e){
System.out.print(e.getActionCommand());
try{
String fuhao = e. getActionCommand();
if(fuhao.equals("+"))
{
operation();
text.setText("");
s = "+";
}
else if(fuhao.equals("-"))
{
operation();
text.setText("");
s = "-";
}
else if(fuhao.equals("*"))
{
operation();
text.setText("");
s = "*";
}
else if(fuhao.equals("/"))
{
operation();
text.setText("");
s = "/";
}
else if(fuhao.equals("="))
{
operation();
text.setText(String.valueOf(result));
s="";
System.out.print(result);
}
else if(fuhao.equals("C"))
{
text.setText("");
}
else if(fuhao.equals("CE"))
{
text.setText("");
}
else
text.setText(text.getText()+e.getActionCommand());
}
catch(Exception exp)
{
text.setText("出错");
}
}
}
相关推荐
java 做的一个小计算器(带菜单栏和托盘功能)大家可以看一下
在前人的基础上开发的java 的一个小计算器(带菜单栏和托盘功能)大家可以看一下
java编写的计算器,能实现加减乘除
java编写的计算器,能实现加减乘除。 //***该梦幻计算器与Windows附件自带计算器的标准版功能、界面相仿***// //******但还不支持键盘操作,如有需要,下一次等级将满足你**********// import java.awt.*; import java.lang.*; import javax.swing.*; import javax.swing.event.*; import java.awt.event.*; import java.awt.Component.*; import java.awt.color.*; import java.text.DecimalFormat; //*****************************************************************************// //*****************************************************************************// public class Calculator implements ActionListener //导入动作监听接口 { //******************************// //*****设计计算器界面的单位*****// JFrame frame; //定义框架 JTextField textAnswer; //定义输出显示框 JPanel panel, panel1, panel2, panel3;//定义面板作为子容器,从而达到要求的界面效果 JMenuBar mainMenu; JTextField textMemory; JLabel labelMemSpace; //labelMemSpace单纯做摆设,控制面板的形状 JButton buttonBk, buttonCe, buttonC; JButton button[]; JButton buttonMC, buttonMR, buttonMS, buttonMAdd; JButton buttonDot, buttonAddAndSub, buttonAdd, buttonSub, buttonMul,buttonDiv, buttonMod; JButton buttonSqrt, buttonDao, buttonEqual; JMenu editMenu, viewMenu, helpMenu; JMenuItem copyItem, pasteItem, tItem, sItem, numberGroup, topHelp, aboutCal,myCollege; DecimalFormat df; //设置数据输出精度 boolean clickable; //控制当前能否按键 double memoryd; //使用内存中存储的数字 int memoryi; double vard, answerd; //用来保存double型数据的中间值(vard)和最后结果(answerd) short key = -1, prekey = -1; //key用来保存当前进行何种运算,prekey用来保存前次进行何种运算 boolean clear = false; String copy; //做复制用 JTextArea help; //帮助 JScrollPane scrollHelp; static boolean One =true; //******************// //*****构造函数开始*****// public Calculator() { clickable = true; answerd = 0; frame = new JFrame("梦幻计算器--SWE08008"); frame.setResizable(false);//不允许改变计算器的大小 frame.setLocation(250,150);//设置计算器的起始位置 frame.setBackground(Color.YELLOW); df = new DecimalFormat("0.##############"); //设置数据输出精度(对于double型值)
Java多功能计算器源代码
我搜藏的仅供参考 Java多功能计算器源代码 Java多功能计算器源代码
JAVA中用GUI编程实现计算器模拟
JAVA中用GUI编程实现计算器模拟,并附带源代码。上传内容仅供参考。
Java编写简易计算器.doc
用Java设计了一个简单的计算器,能够实现基本的运算,包括加减乘除等功能,以及清除保存等功能。


