Java 抵押计算器 GUI 问题

发布于 2024-11-24 08:44:54 字数 4060 浏览 1 评论 0原文

我对编程有点陌生,至少是用 Java 编程,我现在正在上课,我只需要有人给我指明方向。

我在让所有 JPanel 显示在应用程序屏幕上的特定位置时遇到问题。有了所有的计算,剩下的应该是小菜一碟。

这是我到目前为止所做的代码:

import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.*;
import java.text.NumberFormat;
import java.awt.GridLayout;

public class MortgageCalculatorGUI7 extends JFrame {

            JPanel panel1 = new JPanel(new FlowLayout());
            JRadioButton b7Yr = new JRadioButton("7 Years",false);
            JRadioButton b15Yr = new JRadioButton("15 Years",false);
            JRadioButton b30Yr = new JRadioButton("30 Years",false);
            JRadioButton b535Int = new JRadioButton("5.35% Interest",false);
            JRadioButton b55Int = new JRadioButton("5.5% Interest",false);
            JRadioButton b575Int = new JRadioButton("5.75% Interest",false);

            JPanel panel2 = new JPanel(new FlowLayout());
            JLabel mortgageLabel = new JLabel("Mortgage Amount $", JLabel.LEFT);
            JTextField mortgageText = new JTextField(10);
            JLabel termLabel = new JLabel("Mortgage Term (Years)", JLabel.LEFT);
            JTextField termText = new JTextField(3);
            JLabel intRateLabel = new JLabel("Interest Rate (%)", JLabel.LEFT);
            JTextField intRateText = new JTextField(5);
            JLabel mPaymentLabel = new JLabel("Monthly Payment $", JLabel.LEFT);
            JTextField mPaymentText = new JTextField(10);

            JPanel panel3 = new JPanel(new FlowLayout());
            JButton calculateButton = new JButton("CALCULATE");
            JButton clearButton = new JButton("CLEAR");
            JButton exitButton = new JButton("EXIT");


            JPanel panel4 = new JPanel(new FlowLayout());
            JLabel paymentLabel = new JLabel ("Payment #");
            JLabel balLabel = new JLabel (" Balance");
            JLabel ytdPrincLabel = new JLabel (" Principal");
            JLabel ytdIntLabel = new JLabel (" Interest");

            JPanel panel5 = new JPanel(new FlowLayout());
            JTextArea textArea = new JTextArea(10, 31);

        public MortgageCalculatorGUI7() {

            this.setLayout (new BorderLayout());
            panel1.setLayout (new FlowLayout());
            panel2.setLayout (new FlowLayout());
            panel3.setLayout (new FlowLayout());
            panel4.setLayout (new FlowLayout());
            panel5.setLayout (new FlowLayout());

            panel1.add (b7Yr);
            panel1.add (b15Yr);
            panel1.add (b30Yr);
            panel1.add (b535Int);
            panel1.add (b55Int);
            panel1.add (b575Int);

            panel2.add (mortgageLabel);
            panel2.add (mortgageText);
            panel2.add (termLabel);
            panel2.add (termText);
            panel2.add (intRateLabel);
            panel2.add (intRateText);
            panel2.add (mPaymentLabel);
            panel2.add (mPaymentText);

            panel3.add (calculateButton);
            panel3.add (clearButton);
            panel3.add (exitButton);

            panel4.add (paymentLabel);
            panel4.add (balLabel);
            panel4.add (ytdPrincLabel);
            panel4.add (ytdIntLabel);

            panel5.add (textArea);

            add(panel2, BorderLayout.NORTH);
            add(panel1, BorderLayout.CENTER);
            add(panel4, BorderLayout.AFTER_LINE_ENDS);
            add(panel5, BorderLayout.EAST);
            add(panel3, BorderLayout.SOUTH);

            exitButton.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e)
                {
                    System.exit(0);
                }});    

        }

    public static void main(String[] args) {

        MortgageCalculatorGUI7 frame = new MortgageCalculatorGUI7();
        frame.setBounds (200,200,800,500);
        frame.setTitle ("Mortgage Calculator 1.0.4");
        frame.setDefaultCloseOperation (EXIT_ON_CLOSE);
        frame.setVisible (true);

    }
}

感谢您的高级帮助。

I am sort of new to programming, well at least programming in Java, I am in a class at the moment and I just need someone to point me in a direction.

I am having an issue getting all the JPanels to show up in specific spots on the application screen. The rest should be a piece of cake with all the calculations.

Here is the code that I have done so far:

import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.*;
import java.text.NumberFormat;
import java.awt.GridLayout;

public class MortgageCalculatorGUI7 extends JFrame {

            JPanel panel1 = new JPanel(new FlowLayout());
            JRadioButton b7Yr = new JRadioButton("7 Years",false);
            JRadioButton b15Yr = new JRadioButton("15 Years",false);
            JRadioButton b30Yr = new JRadioButton("30 Years",false);
            JRadioButton b535Int = new JRadioButton("5.35% Interest",false);
            JRadioButton b55Int = new JRadioButton("5.5% Interest",false);
            JRadioButton b575Int = new JRadioButton("5.75% Interest",false);

            JPanel panel2 = new JPanel(new FlowLayout());
            JLabel mortgageLabel = new JLabel("Mortgage Amount $", JLabel.LEFT);
            JTextField mortgageText = new JTextField(10);
            JLabel termLabel = new JLabel("Mortgage Term (Years)", JLabel.LEFT);
            JTextField termText = new JTextField(3);
            JLabel intRateLabel = new JLabel("Interest Rate (%)", JLabel.LEFT);
            JTextField intRateText = new JTextField(5);
            JLabel mPaymentLabel = new JLabel("Monthly Payment $", JLabel.LEFT);
            JTextField mPaymentText = new JTextField(10);

            JPanel panel3 = new JPanel(new FlowLayout());
            JButton calculateButton = new JButton("CALCULATE");
            JButton clearButton = new JButton("CLEAR");
            JButton exitButton = new JButton("EXIT");


            JPanel panel4 = new JPanel(new FlowLayout());
            JLabel paymentLabel = new JLabel ("Payment #");
            JLabel balLabel = new JLabel (" Balance");
            JLabel ytdPrincLabel = new JLabel (" Principal");
            JLabel ytdIntLabel = new JLabel (" Interest");

            JPanel panel5 = new JPanel(new FlowLayout());
            JTextArea textArea = new JTextArea(10, 31);

        public MortgageCalculatorGUI7() {

            this.setLayout (new BorderLayout());
            panel1.setLayout (new FlowLayout());
            panel2.setLayout (new FlowLayout());
            panel3.setLayout (new FlowLayout());
            panel4.setLayout (new FlowLayout());
            panel5.setLayout (new FlowLayout());

            panel1.add (b7Yr);
            panel1.add (b15Yr);
            panel1.add (b30Yr);
            panel1.add (b535Int);
            panel1.add (b55Int);
            panel1.add (b575Int);

            panel2.add (mortgageLabel);
            panel2.add (mortgageText);
            panel2.add (termLabel);
            panel2.add (termText);
            panel2.add (intRateLabel);
            panel2.add (intRateText);
            panel2.add (mPaymentLabel);
            panel2.add (mPaymentText);

            panel3.add (calculateButton);
            panel3.add (clearButton);
            panel3.add (exitButton);

            panel4.add (paymentLabel);
            panel4.add (balLabel);
            panel4.add (ytdPrincLabel);
            panel4.add (ytdIntLabel);

            panel5.add (textArea);

            add(panel2, BorderLayout.NORTH);
            add(panel1, BorderLayout.CENTER);
            add(panel4, BorderLayout.AFTER_LINE_ENDS);
            add(panel5, BorderLayout.EAST);
            add(panel3, BorderLayout.SOUTH);

            exitButton.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e)
                {
                    System.exit(0);
                }});    

        }

    public static void main(String[] args) {

        MortgageCalculatorGUI7 frame = new MortgageCalculatorGUI7();
        frame.setBounds (200,200,800,500);
        frame.setTitle ("Mortgage Calculator 1.0.4");
        frame.setDefaultCloseOperation (EXIT_ON_CLOSE);
        frame.setVisible (true);

    }
}

Thanks for the help in advanced.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只为守护你 2024-12-01 08:44:54

FlowLayout 只是众多选项之一。我建议您查看不同的布局管理器,以找到一个适合您的需求。

A FlowLayout is only one of many options. I suggest you look at the different Layout Managers available to find one that suits your needs.

甜柠檬 2024-12-01 08:44:54

如果您对定位组件的要求不太复杂,我建议您使用空布局来使用坐标精确定位组件。

这是一个运行示例:

import javax.swing.*;
import javax.swing.border.*;

public class MortgageCalculatorGUI7 extends JFrame {


    JPanel panel1 = new JPanel();

    public MortgageCalculatorGUI7() {

        getContentPane().setLayout(null);
        setSize(400, 400);

        panel1.setLayout(null);
        panel1.setBounds(0, 0 , 400, 400);
        add(panel1);


        JLabel label1 = new JLabel("a label");
        label1.setBounds(15, 15, 150, 30);
        label1.setBorder(new BevelBorder(BevelBorder.RAISED));
        panel1.add(label1);


        JLabel label2 = new JLabel("a label");
        label2.setBounds(100, 100, 150, 30);
        label2.setBorder(new BevelBorder(BevelBorder.RAISED));
        panel1.add(label2);

        JLabel label3 = new JLabel("a label");
        label3.setBounds(200, 200, 150, 30);
        label3.setBorder(new BevelBorder(BevelBorder.RAISED));
        panel1.add(label3);


    }

    public static void main(String[] args) {
        MortgageCalculatorGUI7 mortgageCalculatorGUI7 = new MortgageCalculatorGUI7();
        mortgageCalculatorGUI7.setDefaultCloseOperation(EXIT_ON_CLOSE);
        mortgageCalculatorGUI7.setVisible(true);

    }

}

If your requirements to position components aren't too complex I would suggest you to use null layout to position components exactly using coordinates.

This is a running example:

import javax.swing.*;
import javax.swing.border.*;

public class MortgageCalculatorGUI7 extends JFrame {


    JPanel panel1 = new JPanel();

    public MortgageCalculatorGUI7() {

        getContentPane().setLayout(null);
        setSize(400, 400);

        panel1.setLayout(null);
        panel1.setBounds(0, 0 , 400, 400);
        add(panel1);


        JLabel label1 = new JLabel("a label");
        label1.setBounds(15, 15, 150, 30);
        label1.setBorder(new BevelBorder(BevelBorder.RAISED));
        panel1.add(label1);


        JLabel label2 = new JLabel("a label");
        label2.setBounds(100, 100, 150, 30);
        label2.setBorder(new BevelBorder(BevelBorder.RAISED));
        panel1.add(label2);

        JLabel label3 = new JLabel("a label");
        label3.setBounds(200, 200, 150, 30);
        label3.setBorder(new BevelBorder(BevelBorder.RAISED));
        panel1.add(label3);


    }

    public static void main(String[] args) {
        MortgageCalculatorGUI7 mortgageCalculatorGUI7 = new MortgageCalculatorGUI7();
        mortgageCalculatorGUI7.setDefaultCloseOperation(EXIT_ON_CLOSE);
        mortgageCalculatorGUI7.setVisible(true);

    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文