В настоящее время я работаю над программой заказа пиццы на затмении с использованием 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
}
}
Я ожидаю, что после нажатия кнопки для начинки она перейдет к третьему набору кнопок, чтобы получить больше возможностей для пиццы, вместо того, чтобы вообще ничего не делать.
Заранее спасибо