简体   繁体   English

按钮未出现在Swing GUI中

[英]Button not appearing in Swing GUI

For my second programming assignment in my Java class, we have to create a Pizza Shop menu GUI. 对于我的Java类中的第二个编程任务,我们必须创建一个Pizza Shop菜单GUI。 Everything appears on my GUI (including choices, boxes, radio buttons, etc.) except for the button ("Process Selection") you are supposed to click for calculating the total cost. 除了您应该单击以计算总成本的按钮(“过程选择”)以外,所有内容都出现在我的GUI上(包括选择,框,单选按钮等)。 The following is my code: 以下是我的代码:

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

public class PizzaShop extends JFrame {

    private Topping t;
    private PizzaSize ps;
    private PizzaType pt;
    private JPanel buttonPanel;
    private JButton ProcessSelection;

    public PizzaShop() {
        super("Welcome To Home Pizza Shop");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());
        t = new Topping();
        ps = new PizzaSize();
        pt = new PizzaType();
        createPanel();
        add(t, BorderLayout.NORTH);
        add(ps, BorderLayout.WEST);
        add(pt, BorderLayout.CENTER);
        setVisible(true);
    }

    private void createPanel() {
        buttonPanel = new JPanel();
        ProcessSelection = new JButton("Process Selection");
        ProcessSelection.addActionListener(new calButton());
        buttonPanel.add(ProcessSelection);
    }

    private class calButton implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            double subtotal;
            subtotal = t.getTopping() + ps.getPizzaSize();
            JOptionPane.showMessageDialog(null, "Your Order \n" + "Pizza Type" + pt.getPizzaType() + "\n" + "Amount Due" + subtotal);
        }
    }

    private class ExitButtonListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    }
}

Did I forget to do add some code somewhere? 我忘了在某个地方添加一些代码吗? I'm boggled. 我很困惑

You are not adding the buttonPanel to the main view 您没有将buttonPanel添加到主视图

UPDATE : you should do this: 更新 :您应该这样做:

private void createPanel() {
    buttonPanel = new JPanel();
    ProcessSelection = new JButton("Process Selection");
    ProcessSelection.addActionListener(new calButton());
    buttonPanel.add(ProcessSelection);
    add(buttonPanel, BorderLayout.SOUTH);
}

您需要将按钮面板添加到框架:

add(buttonPanel, BorderLayout.SOUTH);

Change your createPanel() to this: 将您的createPanel()更改为此:

private JPanel createPanel() {
    buttonPanel = new JPanel();
    ProcessSelection = new JButton("Process Selection");
    ProcessSelection.addActionListener(new calButton());
    buttonPanel.add(ProcessSelection);
    return buttonPanel;
 }

And add the code below to your PizzaShop() method: 并将以下代码添加到PizzaShop()方法中:

add(createPanel(), BorderLayout.SOUTH);

Or you can just follow what Adel Boutros said. 或者,您可以按照Adel Boutros所说的去做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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