java JPanel未显示,缺少什么或哪里出错了

ecbunoof  于 2023-02-15  发布在  Java
关注(0)|答案(1)|浏览(168)

我的代码中缺少什么,当我运行代码时,它没有给出错误,但面板没有显示。有办法显示我的代码中的输入字段吗?
代码需要从用户获取输入,点击按钮,然后计算应付余额,以显示所有信息,如客户名称、姓名、余额、折扣、服务费、应付余额和类别代码。

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

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class CustomerInformation {
    private static final double DISCOUNT_RATE = 0.03;
    private static final double SERVICE_CHARGE_RATE = 0.015;

    public static void main(String[] args) {
        JFrame frame = new JFrame("Customer Information");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 300);
        frame.setLayout(new GridLayout(0, 1));

        JPanel panel1 = new JPanel();
        panel1.add(new JLabel("Customer Number:"));
        JTextField customerNumberField = new JTextField(20);
        panel1.add(customerNumberField);
        frame.add(panel1);

        JPanel panel2 = new JPanel();
        panel2.add(new JLabel("Name:"));
        JTextField nameField = new JTextField(20);
        panel2.add(nameField);
        frame.add(panel2);

        JPanel panel3 = new JPanel();
        panel3.add(new JLabel("Balance:"));
        JTextField balanceField = new JTextField(20);
        panel3.add(balanceField);
        frame.add(panel3);

        JPanel panel4 = new JPanel();
        panel4.add(new JLabel("Code:"));
        JTextField codeField = new JTextField(20);
        panel4.add(codeField);
        frame.add(panel4);

        JButton button = new JButton("Submit");
        frame.add(button);

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String customerNumber = customerNumberField.getText();
                String name = nameField.getText();
                double balance = Double.parseDouble(balanceField.getText());
                int code = Integer.parseInt(codeField.getText());

                double balanceDue = 0;
                String codeCategory = "";

                switch (code) {
                    case 1:
                        balanceDue = balance - (balance * DISCOUNT_RATE);
                        codeCategory = "current";
                        break;
                    case 2:
                        balanceDue = balance;
                        codeCategory = "no discount";
                        break;
                    case 3:
                        balanceDue = balance + (balance * SERVICE_CHARGE_RATE);
                        codeCategory = "no discount";
                        break;
                    default:
                        codeCategory = "invalid code";
                        break;
                }

                JOptionPane.showMessageDialog(frame,
            "Customer Number: " + customerNumber + "\n" +
                    "Name: " + name + ".n" +
                    "Balance" + balance + "/n" +
                    "Discount" + DISCOUNT_RATE + "/n" +
                    "Service rate" + SERVICE_CHARGE_RATE + "/n" +
                    "Balance due" + balanceDue + "/n" +
                    "Code category" + codeCategory
                );
            }
        });
    }
}
xxls0lw8

xxls0lw81#

你从来不把你的框架显示出来。
很可能有更好的方法来实现这一点--我已经有很多年没有写过Swing了--但是仅仅在主方法的底部添加frame.setVisible(true);似乎就可以了。

相关问题