我编写的Java计算器代码,但是我想实现菜单栏里复制粘贴的功能,求大神帮忙帮我看看要加什么代码才能实现 万分感谢 还要交作业
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("出错");
}
}
}
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的GUI(图形用户界面)组件创建了一个具有菜单栏和系统托盘功能的小型计算器应用。
java编写的计算器,能实现加减乘除
java编写的计算器,能实现加减乘除。//***该梦幻计算器与Windows附件自带计算器的标准版功能、界面相仿***////******但还不支持键盘操作,如有需要,下一次等级将满足你*******
Java多功能计算器源代码
### Java多功能计算器源代码解析#### 一、项目概述根据提供的信息,这段代码实现了一个基于Java Swing的图形用户界面(GUI)的多功能计算器。
JAVA中用GUI编程实现计算器模拟
**编程实现**:基于前面的设计,编写实际的代码来实现这些功能。#### 二、程序设计细节##### 1.
Java编写简易计算器.doc
在本Java程序中,我们创建了一个简易计算器应用,它具备基本的数学运算功能,如加、减、乘、除,并且支持清除、保存等操作。


