📄 restaurant.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class Restaurant extends JFrame implements ActionListener {
JMenuBar menuBar;
JMenu options, calcu;
JMenuItem rest, quit, starters, mains, desserts;
JPanel Starters = new JPanel(new GridLayout(4,2,2,2));
JPanel Mains = new JPanel(new GridLayout(4,2,2,2));
JPanel Desserts = new JPanel(new GridLayout(4,2,2,2));
JPanel total = new JPanel(new GridLayout(3,1,2,2));
JPanel cost = new JPanel(new FlowLayout(FlowLayout.LEFT));
JLabel A[],B[],C[],D;
JComboBox a[],b[],c[];
JTextField text4;
Border blackline = BorderFactory.createLineBorder(Color.black);
double aprice[],bprice[],cprice[];
static double cost1=0, cost2=0, cost3=0, cost4=0;
public Restaurant() {
menuBar = new JMenuBar();
setJMenuBar(menuBar);
options = new JMenu("options");
menuBar.add(options);
rest = new JMenuItem("Rest"); rest.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, InputEvent.CTRL_MASK));
rest.addActionListener(this);
options.add(rest); options.addSeparator();
quit = new JMenuItem("Quit");
quit.addActionListener(this); quit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, InputEvent.CTRL_MASK));
options.add(quit);
calcu = new JMenu("calculation cost");
menuBar.add(calcu);
starters = new JMenuItem("Starters");
starters.addActionListener(this); starters.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK));
calcu.add(starters);
mains = new JMenuItem("Mains");
mains.addActionListener(this); mains.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_M, InputEvent.CTRL_MASK));
calcu.add(mains);
desserts = new JMenuItem("Desserts");
desserts.addActionListener(this); desserts.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D, InputEvent.CTRL_MASK));
calcu.add(desserts);
String[] num = {"0","1","2","3","4","5","6"};
Border title1 = BorderFactory.createTitledBorder(blackline, "Starters");
A = new JLabel[5];
A[0] = new JLabel("Chickens Lettuce Wraps - 7.95");
A[1] = new JLabel("Bruschetta - 6.95");
A[2] = new JLabel("Chilli Cheese Fries - 6.25");
A[3] = new JLabel("Total Starter Cost:"); A[4] = new JLabel("£0.00");
a = new JComboBox[3];
for (int l=0; l<3; l++) {
a[l] = new JComboBox (num);
a[l].addActionListener(this);
}
addToJPanel(Starters, A, a);
Starters.setBorder(title1);
Border title2 = BorderFactory.createTitledBorder(blackline, "Mains");
B = new JLabel[5];
B[0] = new JLabel("Gardenburger(v) - 7.45");
B[1] = new JLabel("Angel Hair Pasta - 9.45");
B[2] = new JLabel("Pot Roast Dinner - 12.95");
B[3] = new JLabel("Total Main Course Cost:"); B[4] = new JLabel("£0.00");
b = new JComboBox [3];
for (int k=0; k<3; k++) {
b[k] = new JComboBox (num);
b[k].addActionListener(this);
}
addToJPanel(Mains, B, b);
Mains.setBorder(title2);
Border title3 = BorderFactory.createTitledBorder(blackline, "Desserts");
C = new JLabel[5];
C[0] = new JLabel("BJ's Famous Pizookie - 4.75");
C[1] = new JLabel("Cookies and Cream, Party Platter - 14.95");
C[2] = new JLabel("BJ's Brownie - 3.75");
C[3] = new JLabel("Total Dessert Cost:"); C[4] = new JLabel("£0.00");
c = new JComboBox[3];
for (int m=0; m<3; m++) {
c[m] = new JComboBox(num);
c[m].addActionListener(this);
}
addToJPanel(Desserts, C, c);
Desserts.setBorder(title3);
total.add(Starters);
total.add(Mains);
total.add(Desserts);
D = new JLabel("Total Cost:");
cost.add(D);
text4 = new JTextField("£0.00");
text4.setEditable(false);
cost.add(text4);
setTitle("BJ's Brewhouse");
setSize(450,500);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(total, BorderLayout.CENTER);
this.getContentPane().add(cost, BorderLayout.SOUTH);
setVisible(true);
}
static void addToJPanel (JPanel panel, JLabel[] label, JComboBox[] box) {
for(int i=0; i<8; i++) {
if (i%2==0) {
panel.add(label[i/2]);
} else if(i==7) {
panel.add(label[(i+1)/2]);
} else {
box[(i-1)/2].setSelectedIndex(0);
panel.add(box[(i-1)/2]);
}
}
}
static void calculate (JComboBox[] box, double price[], double cost, JLabel[] label, JTextField text4) {
int times =0; for (int i=0; i<3; i++) { times+=box[i].getSelectedIndex();
cost=cost+(box[i].getSelectedIndex())*price[i];
} if(times>6) { for(int n=0;n<3;n++) {
box[n].setSelectedIndex(0);
}
label[4].setText("£0.00"); text4.setText("£0.00"); } else { cost4+=cost;
label[4].setText("£"+cost);
text4.setText("£"+cost4); }
}
public void actionPerformed(java.awt.event.ActionEvent e) {
if(e.getActionCommand()=="Rest") {
for(int n=0;n<3;n++) {
a[n].setSelectedIndex(0);
b[n].setSelectedIndex(0);
c[n].setSelectedIndex(0);
}
cost4=0;
A[4].setText("£0.00");
B[4].setText("£0.00");
C[4].setText("£0.00");
text4.setText("£0.00");
} else if(e.getActionCommand()=="Quit") {
System.exit(0);
} else if(e.getActionCommand()=="Starters") {
double[] aprice ={7.95,6.95,6.25};
calculate(a, aprice, cost1, A, text4);
} else if(e.getActionCommand()=="Mains") {
double[] bprice = {7.45,9.45,12.95};
calculate(b, bprice, cost2, B, text4);
} else if(e.getActionCommand()=="Desserts"){
double[] cprice = {4.75,14.95,3.75};
calculate(c,cprice, cost3, C, text4);
}
}
public static void main(String args[]) {
JFrame meal = new Restaurant();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -