我编写的Java计算器代码,但是我想实现菜单栏里复制粘贴的功能,求大神帮忙帮我看看要加什么代码才能实现 万分感谢 还要交作业

weixin_39121826 2017-06-13 09:23:34
import java.awt.*;
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("出错");
}

}

}
...全文
210 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_39121826 2017-06-13
  • 打赏
  • 举报
回复
我编写的Java计算器代码,但是我想实现菜单栏里复制粘贴的功能,求大神帮忙帮我看看要加什么代码才能实现 万分感谢 还要交作业

50,523

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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