Reputation: 45
Just finished coding this but for some reason when I run it, my window won't show at all! Anyone have any ideas?
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();
}
}
The ExplanationPanel()
, InputPanel()
, and OutputPanel()
classes are simple and shouldn't get in the way of the code. I just run it, than it all of a sudden says it's terminated, and no window, at all! Not even an empty frame!
Upvotes: 1
Views: 80
Reputation: 347184
You code has no executable constructor (only the default constructor, which isn't doing anything for you)
Based on what I can tell from you code, you want to change public void RetailPriceCalculatorGUI() {
to public RetailPriceCalculator() {
Having said, I encourage you to avoid extending directly from top level containers like JFrame
, you're locking yourself into a single use case and you're not adding any new/reusable functionality to the class. Better to start with a JPanel
and build up the UI from then add it to what ever container you want
Upvotes: 3
Reputation: 3409
Seem you are having typo in RetailPriceCalculator
method.
Change public void RetailPriceCalculatorGUI()
to constructor public RetailPriceCalculator()
or update your main method to:
new RetailPriceCalculator().RetailPriceCalculatorGUI();
Upvotes: 3