基本的GUI窗口没有出现

时间:2019-03-18 19:16:14

标签: java swing

做一个基本的两面板代码面板是为了提供信息并提供一个用于输入数据的框,第二个只是退出或执行。最初,将出现空白窗口,但是在添加panelTwo之后,该窗口不再出现。

该程序编译并没有显示任何错误,除了在主类中,它告诉我与新SalesClassGUI();有关的“新实例已忽略”。不确定如何进行任何帮助将不胜感激!

public class SalesClassGUI extends JFrame {

    private JPanel panelOne;
    private JPanel panelTwo;

    private JLabel associateNameLabel;
    private JLabel associateNumberLabel;
    private JLabel productSoldLabel;
    private JLabel productPriceLabel;
    private JLabel stateCodeLabel;
    private JLabel regionCodeLabel;
    private JLabel productNumberLabel;
    private JLabel productDescripLabel;

    private JTextField associateNameField;
    private JTextField associateNumberField;
    private JTextField productSoldField;
    private JTextField productPriceField;
    private JTextField stateCodeField;
    private JTextField regionCodeField;
    private JTextField productNumberField;
    private JTextField productDescripField;

    private JButton calcButton;
    private JButton exitButton;

    private final int WINDOW_WIDTH = 500;
    private final int WINDOW_HEIGHT = 500;

    public void SalesClassGUI() {

        setTitle("Associate Tracker");
        setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout (new GridLayout (8,1)); 
        buildPanel();
        add(panelOne);
        add(panelTwo);
        setVisible(true);
    }

    private void buildPanel() {

        associateNameLabel = new JLabel("Enter name of associate");
        associateNumberLabel = new JLabel("Enter associate number");
        productSoldLabel = new JLabel("Enter number of product sold");
        productPriceLabel = new JLabel("Enter price of the product");
        stateCodeLabel = new JLabel("Enter state code");
        regionCodeLabel = new JLabel("Enter region code");
        productNumberLabel = new JLabel("Enter product number");
        productDescripLabel = new JLabel("Enter product description");

        associateNameField = new JTextField();
        associateNumberField = new JTextField();
        productSoldField = new JTextField();
        productPriceField = new JTextField();
        stateCodeField = new JTextField();
        regionCodeField = new JTextField();
        productNumberField = new JTextField();
        productDescripField = new JTextField();

        //associate name
        panelOne = new JPanel();
        /*panelOne.setLayout(new GridLayout(8, 2));*/


        panelOne.add(associateNameLabel);
        panelOne.add(associateNameField);

        //associate number
        panelOne.add(associateNumberLabel);
        panelOne.add(associateNumberField);

        //prouct sold
        panelOne.add(productSoldLabel);
        panelOne.add(productSoldField);

        //product price
        panelOne.add(productPriceLabel);
        panelOne.add(productPriceField);

        //state code
        panelOne = new JPanel();
        panelOne.add(stateCodeLabel);
        panelOne.add(stateCodeField);

        //region code
        panelOne.add(regionCodeLabel);
        panelOne.add(regionCodeField);

        //product number
        panelOne.add(productNumberLabel);
        panelOne.add(productNumberField);

        //product description
        panelOne.add(productDescripLabel);
        panelOne.add(productDescripField);

        panelTwo = new JPanel();

        panelTwo.setLayout(new GridLayout(1, 2));

        // Define the button
        calcButton = new JButton("Calculate");

        // Add the buttons to the panel
        panelTwo.add(calcButton);
        panelTwo.add(exitButton);

    }

    public static void main(String[] args) {
        SalesClassGUI salesClassGUI = new SalesClassGUI();
    }
}

1 个答案:

答案 0 :(得分:0)

您的课程没有构造函数。因此,当您调用keyboard-shortcuts="ctrl-ArrowRight scoped"时,将调用默认的构造函数,但不会执行任何操作。然后您的程序就终止了。

要正确地声明一个构造函数,您需要省略返回类型。

更改:

new SalesClassGUI()

收件人:

public void SalesClassGUI() {

注意:您将获得NPE,因为您的ctor有一些错误,但这将消除“什么都不会发生”的问题。我是通过在调试器中逐步执行代码来找到的。您还应该学会这样做。

另外,另一种好的调试技术是仅添加一些打印语句来跟踪程序的流程,并验证代码是否到达特定点。这使我想起了一些严重的问题,我需要使用调试器。

public SalesClassGUI() {

当“ ctor”没有打印时,我知道确实有问题。