java GUI比萨店
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java GUI比萨店相关的知识,希望对你有一定的参考价值。
/**
* Created by Sahil Pattni on 25-Oct-16.
*/
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class GUI extends JFrame implements ActionListener
{
JRadioButton size = new JRadioButton("Small");
JRadioButton size_med = new JRadioButton("Medium");
JRadioButton size_large = new JRadioButton("Large");
String[] style = {"Margherita", "Prosciutto", "Diavola", "Verdure", "Calzone"};
JComboBox<String> pizza_style = new JComboBox<>(style);
JCheckBox garlic = new JCheckBox("Garlic");
JCheckBox chili = new JCheckBox("Jalapenos");
JCheckBox cheese = new JCheckBox("Extra Cheese");
JCheckBox bacon = new JCheckBox("Bacon");
JButton submit = new JButton("Submit");
JTextArea text = new JTextArea();
public GUI(String title)
{
//Set title and size
this.setTitle(title);
this.setSize(700,400);
//Assign button groups
ButtonGroup radio = new ButtonGroup();
radio.add(size);
radio.add(size_med);
radio.add(size_large);
size.setSelected(true);
//Add Panels
JPanel sizee = new JPanel(new FlowLayout());
JPanel stylee = new JPanel(new FlowLayout());
JPanel toppingse = new JPanel(new FlowLayout());
JPanel output = new JPanel(new FlowLayout());
JPanel options = new JPanel(new GridLayout(3, 1));
JPanel textbox = new JPanel(new FlowLayout());
JPanel biggest = new JPanel(new GridLayout(0, 2));
//create buttons and selection menus
text.setPreferredSize(new Dimension(325, 300));
//add buttons and selection menus
sizee.add(size);
sizee.add(size_med);
sizee.add(size_large);
stylee.add(pizza_style);
toppingse.add(garlic);
toppingse.add(chili);
toppingse.add(cheese);
toppingse.add(bacon);
textbox.add(text);
//Get container and set layout
Container contentPane = this.getContentPane();
contentPane.setLayout(new FlowLayout());
options.add(sizee);
options.add(stylee);
options.add(toppingse);
biggest.add(options);
biggest.add(textbox);
contentPane.add(biggest);
contentPane.add(submit);
//Add Borders and Titles
Border optionsBorder = BorderFactory.createTitledBorder("Options");
options.setBorder(optionsBorder);
Border sizeBorder = BorderFactory.createTitledBorder("Select your pizza size");
sizee.setBorder(sizeBorder);
Border styleBorder = BorderFactory.createTitledBorder("Select your pizza style");
stylee.setBorder(styleBorder);
Border toppingsBorder = BorderFactory.createTitledBorder("Choose your toppings");
toppingse.setBorder(toppingsBorder);
//Add ActionL
submit.addActionListener(this);
//set visibility
this.setVisible(true);
}
public void actionPerformed(ActionEvent clicked)
{
if (clicked.getSource() == submit)
{
text.setText("Your custom pizza:\n" +
"--------------------\n");
//Size
if (size.isSelected())
{
text.append("Size: Small\n");
}
else if (size_med.isSelected())
{
text.append("Size: Medium\n");
}
else if (size_large.isSelected())
{
text.append("Size: Large\n");
}
//Style
text.append("Style: " + style[pizza_style.getSelectedIndex()] + "\n");
text.append("\nToppings: \n");
//Toppings
if (garlic.isSelected())
{
text.append("Garlic\n");
}
if (chili.isSelected())
{
text.append(("Jalapenos\n"));
}
if (cheese.isSelected())
{
text.append("Extra Cheese\n");
}
if (bacon.isSelected())
{
text.append("Bacon\n");
}
}
}
public static void main(String[] args) {
GUI pizza = new GUI("Pizza Shop");
}
}
以上是关于java GUI比萨店的主要内容,如果未能解决你的问题,请参考以下文章
题解 P7619 [COCI2011-2012#2] RASPORED