为什么我的窗户没有出现?

时间:2017-02-08 02:23:06

标签: java swing user-interface

刚刚完成编码,但由于某些原因,当我运行它时,我的窗口根本不显示!有人有什么想法吗?

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class RetailPriceCalculator extends JFrame
{
private InputPanel inputs;
private OutputPanel outputs;
private ExplanationPanel banner;
private JPanel buttonPanel;
private JButton calcButton;
private JButton exitButton;

public void RetailPriceCalculatorGUI()
{
    setTitle("Retail Price Calculator");

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    setLayout(new BorderLayout());

    banner = new ExplanationPanel();
    inputs = new InputPanel();
    outputs = new OutputPanel();

    buildButtonPanel();

    add(banner, BorderLayout.NORTH);
    add(inputs, BorderLayout.WEST);
    add(outputs, BorderLayout.EAST);
    add(buttonPanel, BorderLayout.SOUTH);

    pack();
    setVisible(true);
}

public void buildButtonPanel()
{
    buttonPanel = new JPanel();

    calcButton = new JButton("Calculate");
    exitButton = new JButton("Exit");

    calcButton.addActionListener(new CalcButtonListener());
    exitButton.addActionListener(new ExitButtonListener());

    buttonPanel.add(calcButton);
    buttonPanel.add(exitButton);
}

private class CalcButtonListener implements ActionListener
{
    public void actionPerformed (ActionEvent e)
    {
        double wholesale, markup, retail;

        wholesale = inputs.getWholesale();
        markup = inputs.getMarkup();

        retail = wholesale * (1 + markup);

        outputs.setRetail(retail);
    }
}

private class ExitButtonListener implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        System.exit(0);
    }
}

public static void main(String[] args)
    {
        new RetailPriceCalculator();
    } 

}

ExplanationPanel()InputPanel()OutputPanel()类很简单,不应妨碍代码。我只是运行它,而不是突然说它终止了,根本没有窗口!甚至不是一个空框架!

2 个答案:

答案 0 :(得分:3)

似乎你在RetailPriceCalculator方法中输错了。

public void RetailPriceCalculatorGUI()更改为构造函数public RetailPriceCalculator()

或将您的主要方法更新为:

new RetailPriceCalculator().RetailPriceCalculatorGUI();

答案 1 :(得分:2)

您的代码没有可执行构造函数(只有默认构造函数,它不会为您做任何事情)

根据我从代码中可以看出的内容,您希望将public void RetailPriceCalculatorGUI() {更改为public RetailPriceCalculator() {

话虽如此,我建议您避免直接从顶级容器(例如JFrame)进行扩展,将自己锁定在一个用例中,并且不会添加任何新的/可重用的功能班级。最好从JPanel开始并构建UI,然后将其添加到您想要的容器