所以,我有3个标签。
当我按下PizzaPanel.java中的任何按钮时,它不会更新CheckoutPanel.java文件。我希望在checkoutpanel上更新小计,以便在按下每个按钮后保持运行总计。
我尝试过repaint()和revalidate()无济于事,我哪里错了?
import javax.swing.*;
public class pizzaMain
{
// sets up the frame and panels
public static void main (String[] args)
{
JFrame frame = new JFrame ("Pizza Dudes Pizza Palace");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
JTabbedPane tp = new JTabbedPane();
tp.addTab ("Welcome", new WelcomePanel()); // DONE
tp.addTab ("Pizza", new PizzaPanel()); //made, needs listeners
//tp.addTab ("Sides", new SidesPanel()); //made, needs listeners
//tp.addTab ("Drinks", new DrinksPanel()); //made, needs listeners
//tp.addTab ("Deserts", new DesertsPanel());
//tp.addTab("Tools", new ToolsPanel());
tp.addTab ("Checkout", new CheckoutPanel());
frame.getContentPane().add(tp);
frame.pack();
frame.setVisible(true);
}
}
import java.awt。; import javax.swing。;
public class WelcomePanel extends JPanel
{
JLabel l1, l2;
public WelcomePanel()
{
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
setBackground(Color.blue);
l1 = new JLabel("This is a interactive POS Prototype");
l2 = new JLabel(" SCPC CORP. ");
add(l1);
add(l2);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PizzaPanel extends JPanel
{
CheckoutPanel cp = new CheckoutPanel();
private JButton b1, b2, b3, b4, b5, b6;
public PizzaPanel()
{
setLayout(new GridLayout(3, 3));
setBackground(Color.blue);
ButtonListener listener = new ButtonListener();
b1 = new JButton("12' Cheese");
b1.addActionListener(listener);
b2 = new JButton("14' Cheese");
b2.addActionListener(listener);
b3 = new JButton("12' Meat Lover");
b3.addActionListener(listener);
b4 = new JButton("14' Meat Lover");
b4.addActionListener(listener);
b5 = new JButton("12' Supreme");
b5.addActionListener(listener);
b6 = new JButton("14' Supreme");
b6.addActionListener(listener);
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
add(b6);
}
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
if(event.getSource() == b1) // cheese 12
{
cp.setSubtotal(9.99); //works - sends the amnt over to the CheckoutPanel
cp.updateLabels(); //doesnt work
revalidate(); //doesnt work
repaint(); //doesnt work
}
else if(event.getSource() == b2) //cheese 14
{
//Set a qnty option later
cp.setSubtotal(12.99);
cp.updateLabels();
revalidate(); //doesnt work
repaint(); //doesnt work
}
else if(event.getSource() == b3) // meat 12
{
//Set a qnty option later
cp.setSubtotal(11.99);
cp.updateLabels();
revalidate(); //doesnt work
repaint(); //doesnt work
}
else if(event.getSource() == b4) // meat 14
{
//Set a qnty option later
cp.setSubtotal(15.99);
cp.updateLabels();
revalidate(); //doesnt work
repaint(); //doesnt work
}
else if(event.getSource() == b5) //supreme 12
{
//Set a qnty option later
cp.setSubtotal(11.99);
cp.updateLabels();
revalidate(); //doesnt work
repaint(); //doesnt work
}
else if(event.getSource() == b6)//supreme 14
{
//Set a qnty option later
cp.setSubtotal(14.99);
cp.updateLabels();
revalidate(); //doesnt work
repaint(); //doesnt work
}
}
}
}
// getQnty // setQnty需要
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.DecimalFormat;
import java.util.Scanner;
public class CheckoutPanel extends JPanel
{
DecimalFormat formatter = new DecimalFormat("#0.00");
Scanner keyboard = new Scanner(System.in);
double Tax = 0.10; //10% tax rate
double Subtotal;
double Total = (Tax * Subtotal);
JLabel l1, l2, l3;
//double Discount;
public CheckoutPanel()
{
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
setBackground(Color.red);
l1 = new JLabel("Subtotal: $" + formatter.format(Subtotal) + " ");
l2 = new JLabel("Tax: $" + formatter.format(Tax) + " ");
l3 = new JLabel("Order Total: $" + formatter.format(Total) + " ");
add(l1);
add(l2);
add(l3);
}
public void updateLabels()
{
l1.setText("Subtotal: $" + formatter.format(Subtotal) + " ");
l2.setText("Tax: $" + formatter.format(Tax) + " ");
l3.setText("Order Total: $" + formatter.format(Total) + " ");
revalidate();
repaint();
}
public void setTax(double taxRate)
{
Tax = taxRate;
}
public double getTax()
{
return Tax;
}
public void setTotal(double Amount)
{
Total += Amount;
}
public double getTotal()
{
return Total;
}
public void setSubtotal(double Amount)
{
Subtotal += Amount;
l1.setText("Subtotal: $" + formatter.format(Subtotal) + " ");
l2.setText("Tax: $" + formatter.format(Tax) + " ");
l3.setText("Order Total: $" + formatter.format(Total) + " ");
System.out.println(Subtotal); //testing
revalidate();
repaint();
}
public double getSubtotal()
{
return Subtotal;
}