我已经创建了一个基于触摸的交互式披萨屋的程序,其中我分别为pizza菜单(pizzabase.java)的mainmenu(pizza.java)菜单和选择浇头的菜单(pizzatoppings.java)创建了三个文件。 问题是,当我访问第一个文件(主菜单)中的按钮视图量时,我需要得到 basecost 变量和 toppingcost 变量的总和,它们是两个不同的文件pizzabase.java和pizzatoppings.java.how我可以将basecost和toppingcost压缩在两个不同的文件中并将它显示在第一个文件(pizza.java)中。可能有人建议一个合适的答案。
这是第一个文件pizza.java
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
import java.sql.*;
import javax.swing.*;
class p extends JFrame implements ActionListener{
JLabel l,l1;
JButton b1,b2,b3,b4,b5;
Font f1,f;
p(String q)
{
super(q);
Container cc=getContentPane();
cc.setLayout(null);
f1=new Font("Century",1,27);
l=new JLabel("PIZZA HOUSE");
cc.add(l);
l.setFont(f1);
l.setBounds(250,30,250,50);
f=new Font("Arial",1,17);
l1=new JLabel("MAIN MENU");
cc.add(l1);
l1.setFont(f);
l1.setBounds(300,70,100,50);
b1=new JButton("BASE");
cc.add(b1);
b1.setBounds(250,130,200,50);
b1.addActionListener(this);
b2=new JButton("TOPPINGS");
cc.add(b2);
b2.setBounds(250,190,200,50);
b2.addActionListener(this);
b3=new JButton("VIEW AMOUNT");
cc.add(b3);
b3.setBounds(250,250,200,50);
b3.addActionListener(this);
b4=new JButton("PAY BILL");
cc.add(b4);
b4.setBounds(250,310,200,50);
b4.addActionListener(this);
b5=new JButton("EXIT");
cc.add(b5);
b5.setBounds(250,370,200,50);
b5.addActionListener(this);
}
public void actionPerformed(ActionEvent t)
{
if(t.getSource()==b1)
{
p1 obj=new p1("Pizza Base");
obj.setSize(400,350);
obj.setVisible(true);
obj.setLocation(200,200);
this.setVisible(false);
}
if(t.getSource()==b2)
{
p2 obj=new p2("Pizza-Toppings");
obj.setSize(500,500);
obj.setVisible(true);
obj.setLocation(200,200);
this.setVisible(false);
}
if(t.getSource()==b3)
{}
if(t.getSource()==b4)
{
JOptionPane.showMessageDialog(this,"AMOUNT RECEIVED!!!!","THANK YOU",JOptionPane.INFORMATION_MESSAGE);
}
if(t.getSource()==b5)
{
this.setVisible(false);
}
}
}
class pizza
{
public static void main(String args[])
{
p obj=new p("Pizza-House");
obj.setSize(700,500);
obj.setVisible(true);
obj.setLocation(200,200);
}
}
这是第二个文件pizzabase.java
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
import java.sql.*;
import javax.swing.*;
import java.io.*;
class p1 extends JFrame implements ActionListener{
JLabel l;
JButton b1,b2;
JRadioButton R1,R2;
Font f1;
Connection con;
Statement stat;
String qry;
int Soft=30;
int Hard=40;
int baseCost=0;
p1(String q)
{
super(q);
Container cc=getContentPane();
cc.setLayout(null);
f1=new Font("Century",1,16);
l=new JLabel("SELECT BASE");
cc.add(l);
l.setFont(f1);
l.setBounds(130,30,150,50);
R1=new JRadioButton("SOFT--Rs.30");
cc.add(R1);
R1.setBounds(140,100,200,50);
R1.addActionListener(this);
R1.setSelected(false);
R2=new JRadioButton("HARD--Rs.40");
cc.add(R2);
R2.setBounds(140,150,200,50);
R2.addActionListener(this);
ButtonGroup group = new ButtonGroup();
group.add(R1);
group.add(R2);
b1=new JButton("CANCEL");
cc.add(b1);
b1.setBounds(120,200,150,30);
b1.addActionListener(this);
}
public void actionPerformed(ActionEvent t)
{
if(R1.isSelected())
{
baseCost+=Soft;
System.out.println("Total Amount=Rs."+baseCost);
p obj=new p("Pizza-House");
obj.setSize(700,500);
obj.setVisible(true);
obj.setLocation(200,200);
this.setVisible(false);
}
if(R2.isSelected())
{
baseCost+=Hard;
System.out.println("Total Amount=Rs."+baseCost);
p obj=new p("Pizza-House");
obj.setSize(700,500);
obj.setVisible(true);
obj.setLocation(200,200);
this.setVisible(false);
}
if(t.getSource()==b1)
{
p obj=new p("Pizza-House");
obj.setSize(700,500);
obj.setVisible(true);
obj.setLocation(200,200);
this.setVisible(false);
}
}
}
class pizzabase
{
public static void main(String args[])
{
p1 obj=new p1("Pizza Base");
obj.setSize(400,350);
obj.setVisible(true);
obj.setLocation(200,200);
obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
这是第三个文件pizzatoppings.java
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
import java.sql.*;
import javax.swing.*;
class p2 extends JFrame implements ActionListener
{
JLabel l,l1;
JButton b1,b2;
Font f,f1,f2,f3,f4,f5;
JCheckBox c1,c2,c3,c4;
int Tomato=10;
int Capsicum=15;
int Onion=20;
int Paneer=30;
p2(String q)
{
super(q);
Container cc=getContentPane();
cc.setLayout(null);
f1=new Font("Century",1,27);
l=new JLabel("SELECT TOPPINGS");
cc.add(l);
l.setFont(f1);
l.setBounds(110,30,350,50);
f=new Font("Arial",1,17);
l1=new JLabel("SUB MENU");
cc.add(l1);
l1.setFont(f);
l1.setBounds(190,70,100,50);
c1=new JCheckBox("Tomato-Rs.10");
cc.add(c1);
f2=new Font("Arial",1,15);
c1.setFont(f2);
c1.setBounds(170,120,300,50);
c1.addActionListener(this);
c2=new JCheckBox("Capsicum-Rs.15");
cc.add(c2);
f3=new Font("Arial",1,15);
c2.setFont(f3);
c2.setBounds(170,190,300,50);
c2.addActionListener(this);
c3=new JCheckBox("Onion-Rs.20");
cc.add(c3);
f4=new Font("Arial",1,15);
c3.setFont(f4);
c3.setBounds(170,260,300,50);
c3.addActionListener(this);
c4=new JCheckBox("Paneer-Rs.30");
cc.add(c4);
f5=new Font("Arial",1,15);
c4.setFont(f5);
c4.setBounds(170,330,300,50);
c4.addActionListener(this);
b2=new JButton("OK");
cc.add(b2);
b2.setBounds(100,400,100,50);
b2.addActionListener(this);
b1=new JButton("Cancel");
cc.add(b1);
b1.setBounds(300,400,100,50);
b1.addActionListener(this);
}
public void actionPerformed(ActionEvent t)
{
int toppingCost=0;
if (c1.isSelected())
toppingCost +=Tomato ;
if (c2.isSelected())
toppingCost += Capsicum;
if (c3.isSelected())
toppingCost += Onion;
if (c4.isSelected())
toppingCost += Paneer;
if(t.getSource()==b2)
{
System.out.println("Total Amount=Rs."+ toppingCost);
p obj=new p("Pizza-House");
obj.setSize(700,500);
obj.setVisible(true);
obj.setLocation(200,200);
this.setVisible(false);
}
if(t.getSource()==b1)
{
p obj=new p("Pizza-House");
obj.setSize(700,500);
obj.setVisible(true);
obj.setLocation(200,200);
this.setVisible(false);
}
}
}
class pizzatoppings{
public static void main(String args[])
{
p2 obj=new p2("Pizza-Toppings");
obj.setSize(500,500);
obj.setVisible(true);
obj.setLocation(200,200);
obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}