英文:
The Jmenus are not popping up when i run the program
问题
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import javax.swing.*;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;
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;
public InvestCalc() {
frame.setTitle("Assignment 11");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.add(panel);
frame.setSize(400, 300);
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);
class exitAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
exit.addActionListener(new exitAction());
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);
}
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)));
}
}
public static void main(String[] args) {
new InvestCalc();
}
@Override
public void menuCanceled(MenuEvent e) {
}
@Override
public void menuDeselected(MenuEvent e) {
}
@Override
public void menuSelected(MenuEvent e) {
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) {
}
}
Please note that this code might contain mistakes or issues as I'm providing a direct translation. Make sure to review the translated code and test it to ensure its correctness and functionality.
英文:
The JMenus are not popping up when i run the program but they will pop up when you expand the window
gui program with faulty menus
everything else works fine but its just that the Jmenus are not popping up on run time im not sure what the problem is?
the operations menu will sometimes pop up, this program is supposed to calculate future value so it takes in several integer inputs and outputs a future value but the only problem with it is the JMenus are not appearing on run time.
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
}
}
专注分享java语言的经验与见解,让所有开发者获益!
评论