简体   繁体   中英

SWING: My program runs and compiles but shows a grey window with nothing inside

import java.awt.*;
import java.io.*; 
import java.util.ArrayList;
import javax.swing.*;
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener;


public class PizzaOrder1 extends JPanel implements ActionListener {

// Used to cancel an order and clear the form.
private JButton cancelButton;

// Displays what the user ordered.
private JButton orderButton;

// Field to enter the name of the person ordering the pizza.
private JTextField nameField;

// The toppings the user would like.
private JComboBox<String> toppings = new JComboBox<String>();

private JCheckBox deliveryBox = new JCheckBox();

public void PizzaOrder1() {


    // Delivery
    JPanel deliveryPanel = new JPanel();
    deliveryPanel.add(deliveryBox);
    deliveryPanel.add(new JLabel("Delivery"));
    add(deliveryPanel);

    // Person ordering
    JPanel namePanel = new JPanel();
    namePanel.add(new JLabel("Name:  "));
    nameField = new JTextField(15);
    namePanel.add(nameField);
    add(namePanel);

    // Toppings
    toppings.addItem("Pepperoni");
    toppings.addItem("Mushrooms");
    toppings.addItem("Anchovies");
    add(toppings);


    // Order and cancel buttons
    JPanel buttonPanel = new JPanel();
    orderButton = new JButton("Place order");
    orderButton.addActionListener(this);

    buttonPanel.add(orderButton);
    cancelButton = new JButton("Cancel order");
    cancelButton.addActionListener(this);
    buttonPanel.add(cancelButton);
    add(buttonPanel);



    }


    public void actionPerformed(ActionEvent buttonEvent) {
        if (buttonEvent.getSource() == orderButton) {
            Order();
        }

        clear();
    }

    private void Order() {
        String order = nameField.getText() + " ordered a pizza with " +   toppings.getSelectedItem() + ".  ";

        if (deliveryBox.isSelected()) {
            order = order + "This pizza will be delivered.  ";
        } else {
            order = order + "This pizza will be picked up.  ";
        }

        order = order + "This pizza should be ";


        // Show the order in a popup window.
        JOptionPane.showMessageDialog(this, order);

    }

    private void clear() {
        nameField.setText("");
        deliveryBox.setSelected(false);
        toppings.setSelectedIndex(0);

    }


    public static void main(String[] args) {
        JFrame f=new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setTitle("Pizza Order");
        f.setSize(new Dimension(400, 400));

        // Create the panel that will display the image and text
        PizzaOrder1 componentsPanel = new PizzaOrder1();

        // Add the image panel to the center of the window
        Container contentPane = f.getContentPane();
        contentPane.add(componentsPanel, BorderLayout.CENTER);

        // Display the window.
        f.setVisible(true);

    } 

    }

I am running through Windows Command prompt, it compiles and runs fine but when it runs it only shows a grey window of the correct size. I am not sure where I went wrong.

This problem results from confusion of the difference between a method and a constructor.

public void PizzaOrder1() {  // this is a METHOD!

Whereas:

public PizzaOrder1() {  // this is a CONSTRUCTOR!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM