How to put an operation in file menu JAVA?
-
/** * @(#)SimpleCalculator.java * * * @author * @version 1.00 2012/3/26 */ import java.awt.event.*; import javax.swing.*; import java.awt.*; public class SimpleCalculator extends JFrame implements ActionListener{ // Text fields for Number 1, Number 2, and Result private JTextField jtfNum1, jtfNum2, jtfResult; // Buttons "Add", "Subtract", "Multiply" and "Divide" private JButton jbtAdd, jbtSub, jbtMul, jbtDiv, jbtExit; // Default Constructor public SimpleCalculator() { // Panel p1 to hold text fields and labels JPanel p1 = new JPanel(); p1.setLayout(new FlowLayout()); p1.add(new JLabel("Number 1")); p1.add(jtfNum1 = new JTextField(3)); p1.add(new JLabel("Number 2")); p1.add(jtfNum2 = new JTextField(3)); p1.add(new JLabel("Result")); p1.add(jtfResult = new JTextField(8)); jtfResult.setEditable(false); jtfResult.setHorizontalAlignment(SwingCo... // Panel p2 to hold buttons JPanel p2 = new JPanel(); p2.setLayout(new FlowLayout()); p2.add(jbtAdd = new JButton("Add")); p2.add(jbtSub = new JButton("Substract")); p2.add(jbtMul = new JButton("Multiply")); p2.add(jbtDiv = new JButton("Divide")); p2.add(jbtExit = new JButton("Exit")); //Set mnemonic keys jbtAdd.setMnemonic('A'); jbtSub.setMnemonic('S'); jbtMul.setMnemonic('M'); jbtDiv.setMnemonic('D'); jbtExit.setMnemonic('E'); // Add panels to the frame getContentPane().setLayout(new BorderLayout()); getContentPane().add(p1, BorderLayout.CENTER); getContentPane().add(p2, BorderLayout.SOUTH); // Register listeners jbtAdd.addActionListener(this); jbtSub.addActionListener(this); jbtMul.addActionListener(this); jbtDiv.addActionListener(this); jbtExit.addActionListener(this); } public void actionPerformed(ActionEvent e) { String actionCommand = e.getActionCommand(); if (e.getSource() instanceof JButton) {if("Add".equals(actionCommand)) calculate('+'); else if("Substract".equals(actionCommand)) calculate('-'); else if ("Multiply".equals(actionCommand)) calculate('*'); else if("Divide".equals(actionCommand)) calculate('/'); else if("Exit".equals(actionCommand)) System.exit(0); } } private void calculate (char operator){ int num1 = (Integer.parseInt(jtfNum1.getText().trim... int num2 = (Integer.parseInt(jtfNum2.getText().trim... int result = 0; switch(operator) { case '+': result= num1+num2; break; case '-':result= num1-num2; break; case '*':result=num1*num2; break; case '/':result=num1/num2; break; } jtfResult.setText(String.valueOf(result)... } // Main Method public static void main(String[] args) { SimpleCalculator frame = new SimpleCalculator(); frame.setSize(500,100); frame.setTitle("Simple Calculator"); frame.setDefaultCloseOperation(JFrame.EX... frame.setVisible(true); frame.setLocationRelativeTo(null); frame.setSize(400,200); frame.setVisible(true); JMenuBar jmb = new JMenuBar(); frame.setJMenuBar(jmb); JMenu fileMenu = new JMenu("Operation"); JMenu helpMenu = new JMenu("Exit"); jmb.add(fileMenu); jmb.add(helpMenu); fileMenu.add(new JMenuItem("Add")); fileMenu.add(new JMenuItem("Subtract")); fileMenu.add(new JMenuItem("Multiply")); fileMenu.add(new JMenuItem("Devide")); } } When I click the math operation at file menu, then it will be count.. please.
-
Answer:
You have add listeners for menuitems also. I guess same actionPerformed can be modified. Why did you bring menubar, menuitems to main?
Aku at Yahoo! Answers Visit the source
Other answers
You have add listeners for menuitems also. I guess same actionPerformed can be modified. Why did you bring menubar, menuitems to main?
James Bond
Related Q & A:
- How to Read xml file in java?Best solution by Stack Overflow
- How to write XML file with Java and read it?Best solution by Stack Overflow
- How to search for a particular string in a text file using java?Best solution by Stack Overflow
- How do I get to the messenger menu?Best solution by Yahoo! Answers
- How to put a video file on to a disc?Best solution by wikihow.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.