Jmenus не появляются, когда я запускаю программу - PullRequest
0 голосов
/ 03 апреля 2020

JMenus не выскакивают, когда я запускаю программу, но они всплывают, когда вы открываете окно gui программа с неисправными меню
все остальное работает нормально, просто Jmenus не появляются во время выполнения я не уверен, в чем проблема? иногда появляется всплывающее меню операций, предполагается, что эта программа рассчитывает будущее значение, поэтому оно принимает несколько целочисленных входных данных и выводит будущее значение, но единственная проблема с ним заключается в том, что JMenus не отображаются во время выполнения.

   import java.awt.*;
   import java.awt.event.*;
   import java.text.DecimalFormat;
   import javax.swing.*;
   import javax.swing.event.MenuEvent;
   import javax.swing.event.MenuListener;





    //[1]Create a class InvestCalc
    //[2]futureValue = investAmount * (1 + monthlyInterestRate)years * 12
    //[3]Use text fields for investment amount, annual interest rate, years, and future value


    //[4]The Operation menu contains two items: Calculate and Exit.                                                                                     
    //[5]Help menu displays how the future value is calculated in a JOptionPane



    public class InvestCalc implements ActionListener, MenuListener  {

    JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    JMenuBar menuBar;

    private JLabel label1,label2,label3,label4;
    private JTextField textField1,textField2,textField3,textField4;
    private JButton button;
      //constructor
    public InvestCalc() {
        frame.setTitle("Assignment 11");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

     frame.add(panel);

      frame.setSize(400,300);

        //adding the menu
        JMenuBar menuBar = new JMenuBar();
        frame.setJMenuBar(menuBar);
        JMenu operations = new JMenu("Operations");
        menuBar.add(operations);


        JMenuItem cal = new JMenuItem("Calculate");
        operations.add(cal);
        cal.addActionListener(null);
        cal.addActionListener(new event());


        JMenuItem exit = new JMenuItem("Exit");
        operations.add(exit);


        JMenu help = new JMenu("Help");
        menuBar.add(help);
        help.addMenuListener(this);






            //Exit button
    class exitAction implements ActionListener{

            public void actionPerformed (ActionEvent e) {
                System.exit(0);


            }
        }

    exit.addActionListener(new exitAction());





        //layout for panel 
        panel.setLayout(null);
        label1 = new JLabel("Investment Amount");
        panel.add(label1);
        label1.setBounds(20,35,165,25);
        textField1 = new JTextField(15);
        panel.add(textField1);
        textField1.setBounds(170, 35, 200, 25);

        label2 = new JLabel("Years");
        panel.add(label2);
        label2.setBounds(20,65, 165, 25);
        textField2 = new JTextField(15);
        panel.add(textField2);
        textField2.setBounds(170, 65, 200,25);


        label3 = new JLabel("Annual interest Rate");
        panel.add(label3);
        label3.setBounds(20,95,165,25);
        textField3 = new JTextField(15);
        panel.add(textField3);
        textField3.setBounds(170, 95, 200, 25);

        label4 = new JLabel("Future Value");
        panel.add(label4);
        label4.setBounds(20,125,165,25);
        textField4 = new JTextField(15);
        panel.add(textField4);
        textField4.setBounds(170,125,200,25);

        textField4.setBackground(Color.lightGray);
        textField4.setEditable(false);




        button = new JButton("Calculate");
        panel.add(button);
        button.setBounds(20, 175, 350, 30);
        event e = new event();
        button.addActionListener(e);


    }






//this section is for the calculate button on the button of the panel
public class event implements ActionListener{
    public void actionPerformed(ActionEvent e)
    {


        String x = textField1.getText();
        double investmentAmount = Double.parseDouble(x);

        String y = textField2.getText();
        int Years = Integer.parseInt(y);

        String z = textField3.getText();
        double annualInterestRate = Double.parseDouble(z);

        double FutureRate = investmentAmount * Math.pow((1 + (annualInterestRate/100)),(Years));

        DecimalFormat df = new DecimalFormat();
        df.setMaximumFractionDigits(2);
        textField4.setText("$"+String.valueOf(df.format(FutureRate)));
    }}




//main method
public static void main (String[] args) {

    //constructor
    new InvestCalc();

}



@Override
public void menuCanceled(MenuEvent e) {
    // TODO Auto-generated method stub

}



@Override
public void menuDeselected(MenuEvent e) {
    // TODO Auto-generated method stub

}


//clicking of the help menu
@Override
public void menuSelected(MenuEvent e) {
    // TODO Auto-generated method stub
    JOptionPane.showMessageDialog(null,"This is the Formula used to determine future value\n"
            +"futureValue = investAmount * (1 + monthlyInterestRate)years * 12");

}



@Override
public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub

}


}
...