如何从主类调用其他JFrame

时间:2019-12-17 19:39:39

标签: java swing

嗨,我希望能够运行我的主类,并单击“将来值”,然后弹出其他JFrame类FV1。我让它工作了一次,但是随后它停止工作,并且当我单击按钮时,程序冻结了。我不确定该怎么办了,我被困住了。我不确定我一次如何使它工作,也不确定为什么它停止工作。

import java.awt.EventQueue;

import javax.swing.JFrame;
import java.awt.Color;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JToggleButton;
import javax.swing.JFormattedTextField;
import java.awt.BorderLayout;
import java.awt.Font;
import javax.swing.SwingConstants;
import com.jgoodies.forms.factories.DefaultComponentFactory;
import javax.swing.JRadioButton;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JTextPane;

public class Summative {


    private JFrame startFrame;
    private JTextField txtMyFirstText;
    private JTextField txtClearMe;
    private JTextField txtPleaseSelectOne;
    private final Action action = new StartFV();
    private final Action action_1 = new StartPV();


    /**
     * Launch the application.
     * @wbp.parser.entryPoint
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Summative window = new Summative();
                    window.startFrame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Summative() {
        initialize();
    }

    private void initialize() {

        startFrame = new JFrame();
        startFrame.getContentPane().setLayout(null);
        startFrame.setBounds(100, 100, 700, 800);
        ImageIcon Piggy = new ImageIcon(getClass().getResource("/images/Logo.png"));

        String iconPath = "/images/Logo.png";
        ImageIcon icon = new ImageIcon(getClass().getResource(iconPath));
        JLabel label = new JLabel(icon);
        label.setBounds(148, 163, 353, 200);
        label.setVerticalAlignment(SwingConstants.TOP);
        startFrame.getContentPane().add(label);



        JButton btnStart = new JButton("Start");
        btnStart.setToolTipText("Start");
        btnStart.setAction(action);
        btnStart.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {



            }
        });
        btnStart.setAction(action);
        btnStart.setBounds(267, 607, 132, 36);
        startFrame.getContentPane().add(btnStart);





        txtPleaseSelectOne = new JTextField();
        txtPleaseSelectOne.setFont(new Font("Tahoma", Font.PLAIN, 13));
        txtPleaseSelectOne.setText("Select one to solve for");
        txtPleaseSelectOne.setBounds(267, 573, 140, 23);
        startFrame.getContentPane().add(txtPleaseSelectOne);
        txtPleaseSelectOne.setColumns(10);
        txtPleaseSelectOne.setEditable(false);

        JButton btnExit = new JButton("Exit");
        btnExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        btnExit.setBounds(571, 714, 103, 36);
        startFrame.getContentPane().add(btnExit);

        JButton btnPresentValue = new JButton("Present Value");
        btnPresentValue.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        btnPresentValue.setAction(action_1);
        btnPresentValue.setBounds(267, 654, 132, 36);
        startFrame.getContentPane().add(btnPresentValue);

        JTextPane txtpnFinancialValuesCalculator = new JTextPane();
        txtpnFinancialValuesCalculator.setFont(new Font("Tahoma", Font.PLAIN, 50));
        txtpnFinancialValuesCalculator.setText("  Financial Values Calculator");
        txtpnFinancialValuesCalculator.setBounds(10, 429, 664, 74);
        startFrame.getContentPane().add(txtpnFinancialValuesCalculator);
        txtpnFinancialValuesCalculator.setEditable(false);



    }
    private class StartFV extends AbstractAction {
        public StartFV() {
            putValue(NAME, "Future Value");

        }
        public void actionPerformed(ActionEvent e) {
            FV1 top = new FV1();


        }
    }
    private class StartPV extends AbstractAction {
        public StartPV() {
            putValue(NAME, "Present Value");

        }
        public void actionPerformed(ActionEvent e) {
            PV bottom = new PV();
        }
    }
}

我要调用的JFrame类

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.border.EmptyBorder;

public class FV1 extends JFrame {

    private JPanel contentPane;
    private JFrame FV1;
    private JTextField txtInvestmentAmount;
    private JTextField txtInterestRate;
    private JTextField txtNumberOfInterest;
    private JTextField textField_1;
    private JTextField textField;
    private JTextField textField_2;
    private JTextPane txtpnFutureValue;
    private JTextField textField_3;
    private JButton btnCalculate;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    FV1 frame = new FV1();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */


        /**
         * Create the application.
         */
        public FV1() {
            initialize();
        }

        /**
         * Initialize the contents of the frame.
         */
        private void initialize() {
            FV1 = new JFrame();
            FV1.setBounds(100, 100, 700, 800);
            FV1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            FV1.getContentPane().setLayout(null);
            FV1.setLocationRelativeTo(null);
            FV1.setResizable(false);

            JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
            tabbedPane.setBounds(0, 0, 684, 750);
            FV1.getContentPane().add(tabbedPane);

            Scanner input = new Scanner(System.in);
            Double P; //Investment Amount
            P = input.nextDouble();
            Double R; //Interest Rate
            R = input.nextDouble();
            R = 1+(R/100);
            Double N; //Number of Years
            N = input.nextDouble();



            JPanel panel_1 = new JPanel();
            tabbedPane.addTab("Compound Interest", null, panel_1, null);
            panel_1.setLayout(null);

            JPanel panel_2 = new JPanel();
            tabbedPane.addTab("Annuities", null, panel_2, null);
            panel_2.setLayout(null);

            JPanel panel = new JPanel();
            tabbedPane.addTab("Simple Interest", null, panel, null);
            panel.setLayout(null);

            txtInvestmentAmount = new JTextField();
            txtInvestmentAmount.setFont(new Font("Tahoma", Font.PLAIN, 20));
            txtInvestmentAmount.setText("Investment Amount");
            txtInvestmentAmount.setBounds(22, 35, 189, 43);
            panel.add(txtInvestmentAmount);
            txtInvestmentAmount.setColumns(10);
            txtInvestmentAmount.setEditable(false);

            txtInterestRate = new JTextField();
            txtInterestRate.setFont(new Font("Tahoma", Font.PLAIN, 20));
            txtInterestRate.setText("Interest Rate %");
            txtInterestRate.setBounds(22, 265, 164, 43);
            panel.add(txtInterestRate);
            txtInterestRate.setColumns(10);
            txtInterestRate.setEditable(false);

            txtNumberOfInterest = new JTextField();
            txtNumberOfInterest.setFont(new Font("Tahoma", Font.PLAIN, 20));
            txtNumberOfInterest.setText("Duration in Years");
            txtNumberOfInterest.setBounds(23, 446, 176, 43);
            panel.add(txtNumberOfInterest);
            txtNumberOfInterest.setColumns(10);
            txtNumberOfInterest.setEditable(false);

            textField_1 = new JTextField();
            textField_1.setBounds(32, 89, 142, 20);
            panel.add(textField_1);
            textField_1.setColumns(10);



            textField = new JTextField();
            textField.setBounds(32, 319, 142, 20);
            panel.add(textField);
            textField.setColumns(10);


            textField_2 = new JTextField();
            textField_2.setBounds(32, 500, 154, 20);
            panel.add(textField_2);
            textField_2.setColumns(10);

            txtpnFutureValue = new JTextPane();
            txtpnFutureValue.setFont(new Font("Tahoma", Font.PLAIN, 20));
            txtpnFutureValue.setText("Future Value");
            txtpnFutureValue.setBounds(397, 549, 124, 31);
            panel.add(txtpnFutureValue);

            textField_3 = new JTextField();
            textField_3.setFont(new Font("Tahoma", Font.PLAIN, 15));
            textField_3.setBounds(346, 591, 217, 43);
            panel.add(textField_3);
            textField_3.setColumns(10);

            btnCalculate = new JButton("Calculate!");
            btnCalculate.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                }
            });
            btnCalculate.setBounds(207, 603, 89, 23);
            panel.add(btnCalculate);
            FV1.setVisible(true);

        }

    }

0 个答案:

没有答案