Java Swing GUI взаимодействия с пользователем - PullRequest
0 голосов
/ 09 января 2019

В настоящее время я работаю над программой заказа пиццы на затмении с использованием Java.

Кажется, я застрял прямо сейчас; Я не уверен, как выставить больше взаимодействий пользователя после того, как пользователь уже выполнил одно.

Я хочу добавить еще один, чтобы при выборе топинга он переходил к другому набору кнопок.

Я не слишком знаком с графическим интерфейсом Swing, поэтому я немного запутался в том, что мне следует попробовать и сделать, чтобы добавить дополнительные кнопки в мою программу.

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.LocalDateTime;
import javax.swing.*;
//Import used to add interfaces in order to use more commands and to be able to use GUI.

public class PizzaMain implements ActionListener{
    final static String LABEL_TEXT = "Please choose the size of your pizza:";
    JFrame frame;
    JPanel contentPane;
    JLabel label;
    JButton button, button2, button3, button4, button5, button6;
//Button 4,5,6 will be used for more toppings.
//JFrame, JPanel, JLabel, & JButton is used for adding the variables of all the buttons, panels, labels, and frame so they're able to be added into the JPanel in GUI.  

    public PizzaMain() {
        frame = new JFrame("EAST SIDE PIZZA");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        contentPane = new JPanel();
        contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));
        contentPane.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));

        label = new JLabel (LABEL_TEXT);
        label.setAlignmentX(JLabel.TOP_ALIGNMENT);
        contentPane.add(label);
        label.setFont(new Font("Garamond", Font.ITALIC, 15));

        button = new JButton("Small");
        button.setAlignmentX(JButton.LEFT_ALIGNMENT);
        button.setAlignmentY(JButton.CENTER_ALIGNMENT);
        button.setActionCommand("Small");
        button.addActionListener(this);
        contentPane.add(button);
        button.setFont(new Font("Papyrus", Font.ITALIC, 13));
        button.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e){
                button.setVisible(true);
                button.setText("Pepperoni");
                button2.setText("Cheese");
                button3.setVisible(true);
                button3.setText("Pineapple");
                button4 = new JButton("Mushrooms");
                button4.setActionCommand("Mushrooms");
                button4.setFont(new Font("Papyrus", Font.ITALIC, 13));
                contentPane.add(button4);
                button5 = new JButton("Peppers");
                button5.setActionCommand("Peppers");
                button5.setFont(new Font("Papyrus", Font.ITALIC, 13));
                contentPane.add(button5);
                button6 = new JButton("Bacon");
                button6.setActionCommand("Bacon");
                button6.setFont(new Font("Papyrus", Font.ITALIC, 13));
                contentPane.add(button6);
                label.setVisible(true);
                label.setText("Please choose what topping you want on your pizza:");        
            }
        }); //Used for activating the buttons once they're pressed.

        button2 = new JButton("Medium");
        button2.setAlignmentY(JButton.CENTER_ALIGNMENT);
        button2.setActionCommand("Medium");
        button2.addActionListener(this);
        contentPane.add(button2);
        button2.setFont(new Font("Papyrus", Font.ITALIC, 13));
        button2.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e){
                button.setVisible(true);
                button.setText("Pepperoni");
                button2.setVisible(true);
                button2.setText("Cheese");
                button3.setVisible(true);
                button3.setText("Pineapple");
//Set text is to keep the buttons but change the text within them & button 4,5, and 6 are for new buttons to be created
                button4 = new JButton("Mushrooms");
                button4.setActionCommand("Mushrooms");
//setFont is for changing the font of the buttons in the actionPerformed sub-method.
                button4.setFont(new Font("Papyrus", Font.ITALIC, 13));
                contentPane.add(button4);
                button5 = new JButton("Peppers");
                button5.setActionCommand("Peppers");
                button5.setFont(new Font("Papyrus", Font.ITALIC, 13));
                contentPane.add(button5);
                button6 = new JButton("Bacon");
                button6.setActionCommand("Bacon");
                button6.setFont(new Font("Papyrus", Font.ITALIC, 13));
                contentPane.add(button6);
                label.setVisible(true);
                label.setText("Please choose what topping you want on your pizza:");    

//Text below is for creating new buttons within the actionPerformed sub-method so that I'm able to add crust options.
                if (button.getText().equals("Thin Crust"));{
                    button.setText("Thin Crust");
                }
                }
            });

        button3 = new JButton("Large");
        button3.setAlignmentY(JButton.RIGHT_ALIGNMENT);
//Right Alignment is used for changing the button's location on the panel so they're able to be lined up.
        button3.setActionCommand("Large");
        button3.addActionListener(this);
        contentPane.add(button3);
        button3.setFont(new Font("Papyrus", Font.ITALIC, 13));
        button3.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e){
                button.setVisible(true);
                button.setText("Pepperoni");
                button2.setVisible(true);
                button2.setText("Cheese");
                button3.setVisible(true);
                button3.setText("Pineapple");
                button4 = new JButton("Mushrooms");
                button4.setActionCommand("Mushrooms");
                button4.setFont(new Font("Papyrus", Font.ITALIC, 13));
                contentPane.add(button4);
                button5 = new JButton("Peppers");
                button5.setActionCommand("Peppers");
                button5.setFont(new Font("Papyrus", Font.ITALIC, 13));
                contentPane.add(button5);
                button6 = new JButton("Bacon");
                button6.setActionCommand("Bacon");
                button6.setFont(new Font("Papyrus", Font.ITALIC, 13));
                contentPane.add(button6);
                label.setVisible(true);
                label.setText("Please choose what topping you want on your pizza:");                

                button.isEnabled();
            }
        });
        frame.add(contentPane);

        frame.pack();
        frame.setVisible(true);
    }


//Used to run the GUI once the program ha sstarted running.
    private static void runGUI() {
        JFrame.setDefaultLookAndFeelDecorated(true);
//Adds borders to the GUI window.
        PizzaMain size = new PizzaMain();
    }
    public static void main(String[] args) {
//InvokeLate is used for updating the GUI; such as changing the buttons or changing a label after a button is pressed.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                runGUI();
            }
        });
    }

//Used to override since I was getting errors without it.
    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub

    }
}

Я ожидаю, что после нажатия кнопки для начинки она перейдет к третьему набору кнопок, чтобы получить больше возможностей для пиццы, вместо того, чтобы вообще ничего не делать.

Заранее спасибо

Ответы [ 2 ]

0 голосов
/ 09 января 2019
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class PizzaMain implements ActionListener {
 final static String LABEL_TEXT = "Please choose the size of your pizza:";
StringBuilder selectedOutput = new StringBuilder();
JFrame frame;
JPanel contentPane;
JLabel label;
JButton button, button2, button3, button4, button5, button6;
 /*Button 4,5,6 will be used for more toppings.
 JFrame, JPanel, JLabel, & JButton is used for adding the variables of all 
 the buttons, panels, labels, and frame so they're able to be added into the 
 JPanel in GUI.*/ 

public PizzaMain() {
    frame = new JFrame("EAST SIDE PIZZA");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    contentPane = new JPanel();
    contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));
    contentPane.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

    label = new JLabel(LABEL_TEXT);
    label.setAlignmentX(JLabel.TOP_ALIGNMENT);
    contentPane.add(label);
    label.setFont(new Font("Garamond", Font.ITALIC, 15));

    //Add other pizza buttons here  Size buttons ..Small Pizza,Large Pizza ,Medium Pizza,Family Size
    //Once these buttons are created add then to the contentPane
    //Since your user initial point of entry
    button = new JButton("Small");
    button.setAlignmentX(JButton.LEFT_ALIGNMENT);
    button.setAlignmentY(JButton.CENTER_ALIGNMENT);
    button.setActionCommand("Small");
    button.addActionListener(this);
    contentPane.add(button);
    button.setFont(new Font("Papyrus", Font.ITALIC, 13));

    JButton largePizza = new JButton("Large");
    largePizza.setAlignmentX(JButton.LEFT_ALIGNMENT);
    largePizza.setAlignmentY(JButton.CENTER_ALIGNMENT);
    largePizza.setActionCommand("Large");
    largePizza.addActionListener(this);
    contentPane.add(largePizza);

    //This panel is displayed on the second stage after the user has selected his/her Pizza size
    //Add other pizza type button  Pepperoni,Cheese,Ham,blah blah
    //add these buttons to jpanel
    //set each button actionListener  copy paste
    JPanel jPanel = new JPanel();
    jPanel.setBackground(Color.GREEN);
    jPanel.setLayout(new BoxLayout(jPanel, BoxLayout.PAGE_AXIS));
    jPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
    button4 = new JButton("Pepperoni");
    button4.setAlignmentX(JButton.LEFT_ALIGNMENT);
    button4.setAlignmentY(JButton.CENTER_ALIGNMENT);
    button4.setFont(new Font("Papyrus", Font.ITALIC, 13));
    button4.addActionListener(this);
    jPanel.add(button4);

    button5 = new JButton("Cheese");
    button5.setAlignmentX(JButton.LEFT_ALIGNMENT);
    button5.setAlignmentY(JButton.CENTER_ALIGNMENT);
    button5.setFont(new Font("Papyrus", Font.ITALIC, 13));
    button5.addActionListener(this);
    jPanel.add(button5);


    //This page is the last selection stage you can have many pages in form of JPanel as above
    //add other options here ,veggetables,topping
    //add them to jPanel2
    JPanel jPanel2 = new JPanel();
    jPanel2.setBackground(Color.RED);
    jPanel2.setLayout(new BoxLayout(jPanel2, BoxLayout.PAGE_AXIS));
    jPanel2.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
    button6 = new JButton("Vegetables");
    button6.setAlignmentX(JButton.LEFT_ALIGNMENT);
    button6.setAlignmentY(JButton.CENTER_ALIGNMENT);
    button6.setActionCommand("Small in Japen2");
    button6.setFont(new Font("Papyrus", Font.ITALIC, 13));
    button6.addActionListener(this);

    JButton button7 = new JButton("meat");
    button7.setAlignmentX(JButton.LEFT_ALIGNMENT);
    button7.setAlignmentY(JButton.CENTER_ALIGNMENT);
    button7.setActionCommand("Small in Japen2");
    button7.setFont(new Font("Papyrus", Font.ITALIC, 13));
    button7.addActionListener(this);
    jPanel2.add(button7);

    JButton backButtonToPanel1 = new JButton("<<<");
    backButtonToPanel1.setAlignmentX(JButton.LEFT_ALIGNMENT);
    backButtonToPanel1.setAlignmentY(JButton.CENTER_ALIGNMENT);
    backButtonToPanel1.setFont(new Font("Papyrus", Font.ITALIC, 13));
    backButtonToPanel1.addActionListener(this);
    jPanel2.add(backButtonToPanel1);

    JTextField output = new JTextField();
    jPanel2.add(output);

    /*button to select another option following option {pepperoni,cheese} these options are found in jPanel2
    When user chooses small pizza  contentPane will removeAll its childrens,repaint its self and validate its action
    It will repaint itself again and then add second panel
    User selected option is then stored in a StringBuilder log will show the activity
    Once done it will take the user to the next Panel for more options
    This step is used to define buttons actions*/
    button.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            selectedOutput.append("selected -> small");
            contentPane.setLayout(new CardLayout());
            contentPane.removeAll();
            contentPane.repaint();
            contentPane.revalidate();
            contentPane.repaint();
            contentPane.add(jPanel);
            contentPane.revalidate();
            System.out.println("small clicked will take you to jpanel 1");
        }
    });

    largePizza.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            selectedOutput.append("selected -> Large Pizza");
            contentPane.setLayout(new CardLayout());
            contentPane.removeAll();
            contentPane.repaint();
            contentPane.revalidate();
            contentPane.repaint();
            contentPane.add(jPanel);
            contentPane.revalidate();
            System.out.println("large clicked will take you to jpanel 1");
        }
    });

    // button to select the following optins  {pepperoni,meat} options in JPanel 2
    button4.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            selectedOutput.append("   and,pepperoni");
            output.setText(selectedOutput.toString());
            contentPane.setLayout(new CardLayout());
            contentPane.removeAll();
            contentPane.repaint();
            contentPane.revalidate();
            contentPane.repaint();
            contentPane.add(jPanel2);
            contentPane.revalidate();
            System.out.println("large clicked will take you to jpanel 2");
        }
    });

    // button to select the following optins  {pepperoni,meat} options in JPanel 2
    button5.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            selectedOutput.append("   and,cheese");
            output.setText(selectedOutput.toString());
            contentPane.setLayout(new CardLayout());
            contentPane.removeAll();
            contentPane.repaint();
            contentPane.revalidate();
            contentPane.repaint();
            contentPane.add(jPanel2);
            contentPane.revalidate();
            System.out.println("This will remain here showing user final option");
        }
    });

    button7.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            output.setText("");
            selectedOutput.append("  topped with meat");
            output.setText(selectedOutput.toString());
            contentPane.setLayout(new CardLayout());
            contentPane.removeAll();
            contentPane.repaint();
            contentPane.revalidate();
            contentPane.repaint();
            contentPane.add(jPanel2);
            contentPane.revalidate();
            System.out.println("This will remain here showing user final option");

        }
    });

    backButtonToPanel1.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            contentPane.removeAll();
            contentPane.repaint();
            contentPane.revalidate();
            contentPane.repaint();
            contentPane.add(jPanel);
            contentPane.revalidate();
            System.out.println("We are going back");

        }
    });
    frame.add(contentPane);
    frame.pack();
    frame.setVisible(true);
}

private static void runGUI() {
    JFrame.setDefaultLookAndFeelDecorated(true);
    PizzaMain size = new PizzaMain();
}

public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            runGUI();
        }
    });
}

@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub

}
}

Приведенное выше решение использует JPanels и динамический макет страницы. Как это работает Первая страница ( JPanel1 ) отображается там, где пользователь может выбрать размеры пиццы {маленькая, средняя, ​​большая} После выбора размера пиццы отображается другая страница (JPanel2) .

В JPanel2 пользователь может иметь другие варианты выбора типа пиццы, а также возможность вернуться к JPanel1.

Консоль поможет вам со всеми происходящими действиями.

* NB Я добавил JTextField на последнюю JPanel, чтобы показать все выбранные параметры пользователя.

0 голосов
/ 09 января 2019

Возможно, JCheckBoxes и JRadioButtons с одной кнопкой отправки лучше подходят для ваших нужд, чем использование кнопок для каждого выбора. Если вам все еще нужны прослушиватели, все JComponents имеют ActionListener, или слушатель лучше подходит для этого компонента, JCheckBoxes и JRadioButton также имеют ItemListener и ChangeListener.

Если вы хотите придерживаться JButtons, я бы порекомендовал переместить сгруппированные вместе кнопки в JPanel и настроить видимость для этого на true или false. Кроме того, у меня были бы кнопки + и - для более или менее начинки и целочисленное поле для хранения того, сколько из этого начинки задано пользователем, если опция двойных начинок является опцией.

Что-то, что я понял на собственном опыте, лучший способ управления элементами GUI - через JPanels. Нужен этот JLabel определенного размера или позиции? Заверните его в JPanel и придайте форму тому, что должно быть.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...