📄 mcdonald.java
字号:
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class McDonald
extends JFrame
implements FocusListener, ItemListener, ActionListener, Runnable {
Thread t;
JLabel lbl1, lbl2, lbl3, lbl4, lbl5, lbl6, lbl7, lbl8, co;
JTextField txt1, txt2, txt3, txt4, txt5, txt6;
JTextArea tat1;
JRadioButton rd1, rd2;
JCheckBox chk1, chk2, chk3, chk4, chk5, chk6, chk7, chk8;
JComboBox cbo1;
JButton cmd1, cmd2;
ButtonGroup bg;
JPanel pan1, pan2, pan3, pan4;
String gstr1 = "";
String gstr[] = {
"Today", "Tomarrow", "day after tomarrow"};
McDonald() {
t = new Thread(this);
setBounds(50, 50, 450, 560);
getContentPane().setLayout(new BorderLayout());
pan1 = new JPanel();
pan2 = new JPanel();
pan3 = new JPanel();
pan1.setLayout(new FlowLayout());
lbl1 = new JLabel("", new ImageIcon("images\\mc_logo.gif"),
JLabel.CENTER);
co = new JLabel("Date");
getContentPane().add("North", pan1);
getContentPane().add("Center", pan2);
getContentPane().add("South", pan3);
pan1.add(lbl1);
pan1.add(co);
pan2.setLayout(null);
pan3.setLayout(new FlowLayout());
lbl2 = new JLabel("Customer Name: ");
lbl2.setBounds(20, 40, 150, 20);
pan2.add(lbl2);
txt1 = new JTextField();
txt1.setBounds(180, 40, 200, 20);
pan2.add(txt1);
txt1.addFocusListener(this);
lbl3 = new JLabel("Current Address: ");
lbl3.setBounds(20, 65, 150, 20);
pan2.add(lbl3);
tat1 = new JTextArea("", 150, 70);
tat1.setLineWrap(true);
JScrollPane jp = new JScrollPane(tat1);
pan2.add(jp);
jp.setBounds(180, 65, 200, 70);
lbl4 = new JLabel("Phone Number: ");
lbl4.setBounds(20, 140, 150, 20);
pan2.add(lbl4);
txt2 = new JTextField();
txt2.setBounds(180, 140, 200, 20);
pan2.add(txt2);
txt2.addFocusListener(this);
lbl5 = new JLabel("Email id: ");
lbl5.setBounds(20, 165, 150, 20);
pan2.add(lbl5);
txt3 = new JTextField();
txt3.setBounds(180, 165, 200, 20);
pan2.add(txt3);
txt3.addFocusListener(this);
bg = new ButtonGroup();
rd1 = new JRadioButton("Pizza");
bg.add(rd1);
rd1.setBounds(40, 190, 100, 20);
pan2.add(rd1);
rd2 = new JRadioButton("Burger");
bg.add(rd2);
rd2.setBounds(180, 190, 80, 20);
pan2.add(rd2);
rd1.addItemListener(this);
rd2.addItemListener(this);
lbl6 = new JLabel(" ", new ImageIcon("images\\pizza.jpg"), JLabel.CENTER);
lbl6.setBounds(280, 190, 150, 80);
pan2.add(lbl6);
ButtonGroup bg1 = new ButtonGroup();
chk1 = new JCheckBox("Capcicum Pizza");
chk1.setBounds(10, 220, 130, 20);
chk2 = new JCheckBox("Cheese Pizza ");
chk2.setBounds(140, 220, 130, 20);
chk3 = new JCheckBox("Onion Pizza ");
chk3.setBounds(10, 250, 130, 20);
chk4 = new JCheckBox("Tomato Pizza ");
chk4.setBounds(140, 250, 130, 20);
bg.add(chk1);
pan2.add(chk1);
bg.add(chk2);
pan2.add(chk2);
bg.add(chk3);
pan2.add(chk3);
bg.add(chk4);
pan2.add(chk4);
chk5 = new JCheckBox("kampung burger");
chk5.setBounds(10, 220, 130, 20);
chk6 = new JCheckBox("Love Burger");
chk6.setBounds(140, 220, 130, 20);
chk7 = new JCheckBox("Mcpepper burger");
chk7.setBounds(10, 250, 130, 20);
chk8 = new JCheckBox("mctowkay burger");
chk8.setBounds(140, 250, 130, 20);
bg.add(chk5);
pan2.add(chk5);
bg.add(chk6);
pan2.add(chk6);
bg.add(chk7);
pan2.add(chk7);
bg.add(chk8);
pan2.add(chk8);
chk1.setVisible(false);
chk2.setVisible(false);
chk3.setVisible(false);
chk4.setVisible(false);
chk5.setVisible(false);
chk6.setVisible(false);
chk7.setVisible(false);
chk8.setVisible(false);
chk1.addItemListener(this);
chk2.addItemListener(this);
chk3.addItemListener(this);
chk4.addItemListener(this);
chk5.addItemListener(this);
chk6.addItemListener(this);
chk7.addItemListener(this);
chk8.addItemListener(this);
lbl7 = new JLabel("Cost: ");
lbl7.setBounds(40, 280, 70, 20);
pan2.add(lbl7);
txt4 = new JTextField(100);
txt4.setBounds(120, 280, 100, 20);
txt4.setEditable(false);
pan2.add(txt4);
lbl8 = new JLabel("When u want delivery?:");
lbl8.setBounds(40, 310, 200, 20);
pan2.add(lbl8);
cbo1 = new JComboBox(gstr);
cbo1.setBounds(250, 310, 180, 20);
pan2.add(cbo1);
cmd1 = new JButton("Order");
pan3.add(cmd1);
cmd1.addActionListener(this);
cmd2 = new JButton("Exit");
pan3.add(cmd2);
cmd2.addActionListener(this);
t.start();
this.setVisible(true);
}
public void run() {
for (; ; ) {
co.setText(new Date().toString());
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == cmd1)
{
if (txt1.getText().equals("") && txt2.getText().equals(""))
{
EntryException a=new EntryException(this, txt1);
}
else
{
boolean mbln1 = false;
int a = 0, d = 0;
boolean mbln2 = false;
String email = txt3.getText();
for (int minti1 = 0; minti1 < email.length(); minti1++) {
char mc = email.charAt(minti1);
if (mc == '@') {
mbln2 = true;
a = minti1;
}
if (mc == '.') {
mbln1 = true;
d = minti1;
}
}
if ( (! (mbln2 & mbln1)) || (a > d)) {
EntryException a1=new EntryException(this, txt3);
}
else
{
OrderWindow ow = new OrderWindow(txt1.getText(), gstr1, txt4.getText(),
cbo1.getSelectedItem().toString());
}
}
}
if (e.getSource() == cmd2)
{
t.stop();
System.exit(0);
}
}
public void itemStateChanged(ItemEvent e) {
if (e.getSource() == rd1) {
gstr1 = "Pizza";
chk1.setVisible(true);
chk2.setVisible(true);
chk3.setVisible(true);
chk4.setVisible(true);
chk5.setVisible(false);
chk6.setVisible(false);
chk7.setVisible(false);
chk8.setVisible(false);
}
if (e.getSource() == rd2) {
gstr1 = "Burger";
chk1.setVisible(false);
chk2.setVisible(false);
chk3.setVisible(false);
chk4.setVisible(false);
chk5.setVisible(true);
chk6.setVisible(true);
chk7.setVisible(true);
chk8.setVisible(true);
}
if (e.getSource() == chk1) {
lbl6.setIcon(new ImageIcon("images\\pizza_cap.jpg"));
txt4.setText("$10");
}
if (e.getSource() == chk2) {
lbl6.setIcon(new ImageIcon("images\\pizza_cheese.jpg"));
txt4.setText("$15");
}
if (e.getSource() == chk3) {
lbl6.setIcon(new ImageIcon("images\\pizza_oni.jpg"));
txt4.setText("$12");
}
if (e.getSource() == chk4) {
lbl6.setIcon(new ImageIcon("images\\pizza_toma.jpg"));
txt4.setText("$10");
}
if (e.getSource() == chk5) {
lbl6.setIcon(new ImageIcon("images\\kampungburger_burger.jpg"));
txt4.setText("$8");
}
if (e.getSource() == chk6) {
lbl6.setIcon(new ImageIcon("images\\loveburger_burger.jpg"));
txt4.setText("$14");
}
if (e.getSource() == chk7) {
lbl6.setIcon(new ImageIcon("images\\mcpepper_burger.jpg"));
txt4.setText("$10");
}
if (e.getSource() == chk8) {
lbl6.setIcon(new ImageIcon("images\\mctowkay_burger.jpg"));
txt4.setText("$18");
}
}
public void focusLost(FocusEvent e) {
try
{
}
catch (Exception e1) {}
}
public void focusGained(FocusEvent e) {
}
public static void main(String arg[]) {
new McDonald();
}
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
}
class EntryException
extends Exception
implements ActionListener
{
JFrame fra, fra1;
JTextField txt;
EntryException(JFrame fra, JTextField txt) {
super("");
this.fra = fra;
fra.setEnabled(false);
this.txt = txt;
fra1 = new JFrame("ERROR");
fra1.setBounds(200, 150, 200, 100);
fra1.getContentPane().setLayout(new FlowLayout());
JLabel lbl = new JLabel("Valid Data must be enter");
JButton cmd3 = new JButton("OK");
fra1.getContentPane().add(lbl);
fra1.getContentPane().add(cmd3);
cmd3.addActionListener(this);
fra1.setVisible(true);
}
public void actionPerformed(ActionEvent e1) {
fra1.dispose();
fra.setEnabled(true);
txt.requestFocus();
}
}
class OrderWindow
extends JFrame
implements ActionListener {
JButton cmd4;
OrderWindow(String mstrname, String mstritem, String mstrcost, String mstrday) {
setBounds(50, 150, 480, 100);
getContentPane().setLayout(new FlowLayout());
JLabel lbl11 = new JLabel("Thanks " + mstrname + ",");
JLabel lbl12 = new JLabel(" for buying " + mstritem + " of the cost " +
mstrcost + " . " + mstritem +
" will be delivered " + mstrday);
cmd4 = new JButton("OK");
cmd4.addActionListener(this);
getContentPane().add(lbl11);
getContentPane().add(lbl12);
getContentPane().add(cmd4);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -