如何只显示点击哪个按钮的数据?

时间:2016-04-20 17:31:34

标签: java arrays swing jbutton actionlistener

我正在从事面包店销售系统项目。 我将订单页面和付款页面分成两个框架。 我想只在点击按钮时才显示订单框架中付款框架中按钮背后的数据。

有办法吗?

以下是我的订购页面代码:

import java.text.DecimalFormat; 
class Bun extends javax.swing.JFrame {
 String decimal = "#.##";
 DecimalFormat df = new DecimalFormat(decimal);
 public final String [] bname = new String [] {"Tuna Sandwich","Garlic Bread","Redbean Bun","Sausage Bun","Chicken Burger","Ham Sandwich","Egg Mayo Bun","Chocolate Bread","Potato Bun"};
 public final double [] bprice = new double [] {1.20, 2, 1.50, 2, 3, 2, 2, 1.50, 1.20};
 public final double [] bsub = new double [] {0,0,0,0,0,0,0,0,0};
 public double bsubtotal=0;

public Bun() {
    initComponents();
}

private void BtnTunaSandwichActionPerformed(java.awt.event.ActionEvent evt) {                                                
    BtnTunaSandwich.setText(bname[1]+" - RM "+bprice[1]);
}                                               

private void BtnGarlicBreadActionPerformed(java.awt.event.ActionEvent evt) {                                               
    BtnGarlicBread.setText(bname[2]+" - RM "+bprice[2]);
}                                              

private void BtnRedbeanBunActionPerformed(java.awt.event.ActionEvent evt) {                                              
    BtnRedbeanBun.setText(bname[3]+" - RM "+bprice[3]);
}                                             

private void BtnSausageBunActionPerformed(java.awt.event.ActionEvent evt) {                                              
    BtnSausageBun.setText(bname[4]+" - RM "+bprice[4]);
}                                             

private void BtnChickenBurgerActionPerformed(java.awt.event.ActionEvent evt) {                                                 
    BtnChickenBurger.setText(bname[5]+" - RM "+bprice[5]);
}                                                

private void BtnHamSandwichActionPerformed(java.awt.event.ActionEvent evt) {                                               
    BtnHamSandwich.setText(bname[6]+" - RM "+bprice[6]);
}                                              

private void BtnEggMayoBunActionPerformed(java.awt.event.ActionEvent evt) {                                              
    BtnEggMayoBun.setText(bname[7]+" - RM "+bprice[7]);
}                                             

private void BtnChocolateBreadActionPerformed(java.awt.event.ActionEvent evt) {                                                  
    BtnChocolateBread.setText(bname[8]+" - RM "+bprice[8]);
}                                                 

private void BtnPotatoBunActionPerformed(java.awt.event.ActionEvent evt) {                                             
    BtnPotatoBun.setText(bname[9]+" - RM "+bprice[9]);
}                                            

private void numTunaSandwichActionPerformed(java.awt.event.ActionEvent evt) {                                                
    bsub[1] = Integer.parseInt(numTunaSandwich.getText())*bprice[1];
}                                               

private void numGarlicBreadActionPerformed(java.awt.event.ActionEvent evt) {                                               
    bsub[2] = Integer.parseInt(numGarlicBread.getText())*bprice[2];
}                                              

private void numRedbeanBunActionPerformed(java.awt.event.ActionEvent evt) {                                              
    bsub[3] = Integer.parseInt(numRedbeanBun.getText())*bprice[3];
}                                             

private void numSausageBunActionPerformed(java.awt.event.ActionEvent evt) {                                              
    bsub[4] = Integer.parseInt(numSausageBun.getText())*bprice[4];
}                                             

private void numChickenBurgerActionPerformed(java.awt.event.ActionEvent evt) {                                                 
    bsub[5] = Integer.parseInt(numChickenBurger.getText())*bprice[5];
}                                                

private void numHamSandwichActionPerformed(java.awt.event.ActionEvent evt) {                                               
    bsub[6] = Integer.parseInt(numHamSandwich.getText())*bprice[6];
}                                              

private void numEggMayoBunActionPerformed(java.awt.event.ActionEvent evt) {                                              
    bsub[7] = Integer.parseInt(numEggMayoBun.getText())*bprice[7];
}                                             

private void numChocolateBreadActionPerformed(java.awt.event.ActionEvent evt) {                                                  
    bsub[8] = Integer.parseInt(numChocolateBread.getText())*bprice[8];
}                                                 

private void numPotatoBunActionPerformed(java.awt.event.ActionEvent evt) {                                             
    bsub[9] = Integer.parseInt(numPotatoBun.getText())*bprice[9];
}                                            

public void Calculatetotalb(){
    for(double i : bsub){
        bsubtotal += i;
    }
}

我还没有在付款框架中写任何内容。 但这是我的订单框架如下:

Order Frame

这是我的付款框架如下:

Payment frame

我想将数据显示到该Jlist中。 请帮助,非常感谢。

1 个答案:

答案 0 :(得分:2)

将数据从一个帧传输到另一个帧只不过是在对象之间交换数据。你需要做的第一件事是实现一个 PaymentFrame内部的方法,它将接受数据并更改列表

class PaymentFrame{

public void setData(String list){
//get the list from the method and update the jlist
jlist.add(list);
}

}

,下一步是在你的案例Orderframe

中从另一个类调用此方法
class orderFrame{
paymentframe pay; 
public void sendData(String data){
if(pay==null){
pay = new paymentframe();
 }    
pay.setdata(data);
pay.setvisible(true);
}

}

这里有一个例子,如果你已经在项目中打开了一个PaymentFrame,你应该从订单框中访问同一个对象,做你曾经问过的问题。