我如何才能使此文本字段和下拉菜单正常工作以完成我想要的工作?

时间:2019-03-25 23:09:32

标签: java swing drop-down-menu textinput

我不希望有人为我做这项工作,但是我很难自行寻找问题的确切答案。我正在尝试创建一个文本字段,用户必须在其中输入1到3之间的一个整数,然后输入一个下拉菜单来选择口味(想使用下拉菜单作为提交方式或获取菜单的方式)本身保持选中状态)。提交后,我希望弹出一个带有彩绘冰淇淋圆锥形的窗口,该圆锥形会根据口味和勺子的数量来改变颜色,从而也能准确地适应用户的输入。

我一直在尝试与JOptionPanel一起工作,因为我认为也许与某种ActionListener设置相结合可以为我解决问题,但是我似乎找不到所需的方法或建议。工作就是这样完成的。

public void createGUI()
{
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    Container window = getContentPane();
        window.setLayout(new BorderLayout());

    GridLayout mainLayout = new GridLayout(2,1);
    GridLayout topLayout = new GridLayout(1,2);
    GridLayout menuLayout = new GridLayout(6,2);

    JLabel storeFront = new JLabel("Ice Cream Shop");
    JLabel orderHere = new JLabel("Order Here");

    ImageIcon storeImage = new ImageIcon("storefront.jpeg");
        JLabel store = new JLabel(storeImage);

    ImageIcon flavorsImage = new ImageIcon("flavors.jpeg");
        JLabel flavors = new JLabel(flavorsImage);

    JTextField numberScoops = new JTextField(5);


    JMenuBar flavorMenu = new JMenuBar();
        JMenu flavorIceCream = new JMenu();

        JMenuItem chocolate = new JMenuItem("Chocolate");
        JMenuItem vanilla = new JMenuItem("Vanilla");
        JMenuItem strawberry = new JMenuItem("Strawberry");
        flavorIceCream.add(chocolate);
        flavorIceCream.add(vanilla);
        flavorIceCream.add(strawberry);

        flavorMenu.add(flavorIceCream);

    JPanel iceCreamShop = new JPanel();
        iceCreamShop.setBackground(Color.gray);
        iceCreamShop.add(store, BorderLayout.CENTER);

    JPanel iceCreamMenu = new JPanel();
        iceCreamMenu.setBackground(Color.gray);
            JPanel menuPanel = new JPanel();
                menuPanel.setBackground(Color.gray);
                menuPanel.setLayout(menuLayout);

                menuPanel.add(new JLabel("Chocolate"));
                menuPanel.add(new JLabel("$2.00/scoop"));
                menuPanel.add(new JLabel("Vanilla"));
                menuPanel.add(new JLabel("$2.00/scoop"));
                menuPanel.add(new JLabel("Strawberry"));
                menuPanel.add(new JLabel("$2.25/scoop"));
                menuPanel.add(new JLabel("How Many Scoops?"));
                menuPanel.add(numberScoops);
                menuPanel.add(new JLabel("what flavor?"));
                menuPanel.add(flavorMenu);
                menuPanel.add(new JButton("clear"));
                menuPanel.add(new JButton("submit"));
        iceCreamMenu.add(menuPanel);

    JPanel iceCreamOrder = new JPanel();
        iceCreamOrder.setLayout(topLayout);
        iceCreamOrder.add(iceCreamShop);
        iceCreamOrder.add(iceCreamMenu);

    JPanel mainPanel = new JPanel();
        mainPanel.setLayout (mainLayout);
        mainPanel.setBackground(Color.gray);
        mainPanel.add(iceCreamOrder);
        mainPanel.add(flavors, BorderLayout.CENTER);

    window.add(mainPanel, BorderLayout.CENTER);

}

0 个答案:

没有答案