2017-05-03 43 views
0

我正在學習如何創建GUI並需要幫助理解actionListeners。一切都編碼,但出於某種原因,我的計算按鈕實際上並沒有打開與主窗口的結果的新窗口。該計劃是一家三明治店,可讓您製作三明治並選擇飲料。當用戶完成後,他們點擊計算,並打開一個新的窗口,其中包含小計,稅金和最終總計以及另一個退出按鈕。這是迄今爲止我所有的代碼。我很感激任何人都可以提供的指導。Action Listener用結果打開新窗口

三明治店

/** 
* Creating a menu for a sandwich shop. There are 2 choices for bread, 4 
choices for fixings, and 4 drink selections. The total will be calculated in 
a new 
* window using the actionListener. This new window class is 
CalculateListener. 
* @author Chad Hoye 
* @version 1.0 
*/ 

public class SandShop2 extends JFrame { 
/** 
* 
*/ 
private static final long serialVersionUID = 1L; 
private JPanel contentPane; 
private final ButtonGroup buttonGroup_Bread = new ButtonGroup(); 
private final ButtonGroup buttonGroup_Drink = new ButtonGroup(); 

/** 
* Launch the application. 
*/ 
public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       SandShop2 frame = new SandShop2(); 
       frame.setVisible(true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

/** 
* Create the frame. 
*/ 
public SandShop2() { 
    String breadOP; 
    String drinkOP = null; 
    boolean hamOP = false, turkeyOP = false, cheeseOP = false, mayoOP = false; 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setBounds(100, 100, 240, 240); 
    contentPane = new JPanel(); 
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
    setContentPane(contentPane); 
    contentPane.setLayout(new BorderLayout(0, 0)); 

    JPanel bread = new JPanel(); 
    bread.setBorder(new LineBorder(new Color(0, 0, 0))); 
    contentPane.add(bread, BorderLayout.WEST); 
    bread.setLayout(new GridLayout(2, 1, 0, 0)); 

    JRadioButton rdbtnWhite = new JRadioButton("White"); 
    buttonGroup_Bread.add(rdbtnWhite); 
    rdbtnWhite.setSelected(true); 
    bread.add(rdbtnWhite); 

    JRadioButton rdbtnWheat = new JRadioButton("Wheat"); 
    buttonGroup_Bread.add(rdbtnWheat); 
    bread.add(rdbtnWheat); 

    if(rdbtnWhite.isSelected()){ 
     breadOP = "white"; 
    }else{ 
     breadOP = "wheat"; 
    } 

    JPanel fixings = new JPanel(); 
    fixings.setBorder(new LineBorder(new Color(0, 0, 0))); 
    contentPane.add(fixings, BorderLayout.CENTER); 
    fixings.setLayout(new GridLayout(4, 1, 0, 0)); 

    JCheckBox chckbxHam = new JCheckBox("Ham"); 
    fixings.add(chckbxHam); 

    JCheckBox chckbxTurkey = new JCheckBox("Turkey"); 
    fixings.add(chckbxTurkey); 

    JCheckBox chckbxCheese = new JCheckBox("Cheese"); 
    fixings.add(chckbxCheese); 

    JCheckBox chckbxMayo = new JCheckBox("Mayo"); 
    fixings.add(chckbxMayo); 

    if(chckbxHam.isSelected()){ 
     hamOP = true; 
    } 
    if(chckbxTurkey.isSelected()){ 
     turkeyOP = true; 
    } 
    if(chckbxCheese.isSelected()){ 
     cheeseOP = true; 
    } 
    if(chckbxMayo.isSelected()){ 
     mayoOP = true; 
    } 

    JPanel drinks = new JPanel(); 
    drinks.setBorder(new LineBorder(new Color(0, 0, 0))); 
    contentPane.add(drinks, BorderLayout.EAST); 
    drinks.setLayout(new GridLayout(4, 1, 0, 0)); 

    JRadioButton rdbtnSoda = new JRadioButton("Soda"); 
    buttonGroup_Drink.add(rdbtnSoda); 
    drinks.add(rdbtnSoda); 

    JRadioButton rdbtnBeer = new JRadioButton("Beer"); 
    buttonGroup_Drink.add(rdbtnBeer); 
    drinks.add(rdbtnBeer); 

    JRadioButton rdbtnTea = new JRadioButton("Tea"); 
    buttonGroup_Drink.add(rdbtnTea); 
    drinks.add(rdbtnTea); 

    JRadioButton rdbtnWater = new JRadioButton("Water"); 
    buttonGroup_Drink.add(rdbtnWater); 
    drinks.add(rdbtnWater); 

    if(rdbtnSoda.isSelected()){ 
     drinkOP = "Soda"; 
    }else if(rdbtnBeer.isSelected()){ 
     drinkOP = "Beer"; 
    }else if(rdbtnTea.isSelected()){ 
     drinkOP = "Tea"; 
    }else { 
     drinkOP = "Water"; 
    } 

    JPanel Cal_Exit = new JPanel(); 
    Cal_Exit.setBorder(new LineBorder(new Color(0, 0, 0))); 
    contentPane.add(Cal_Exit, BorderLayout.SOUTH); 

    JButton btnCalculate = new JButton("Calculate"); 

    btnCalculate.addActionListener(new CalculateListener(breadOP, drinkOP, hamOP, turkeyOP, cheeseOP, mayoOP)); 
    Cal_Exit.add(btnCalculate); 

    JButton btnExit = new JButton("Exit"); 
    btnExit.addActionListener(new ExitListener()); 
    Cal_Exit.add(btnExit); 

    JLabel lblChadsSandwhichShop = new JLabel("Chad's Sandwhich Shop"); 
    lblChadsSandwhichShop.setHorizontalAlignment(SwingConstants.CENTER); 
    contentPane.add(lblChadsSandwhichShop, BorderLayout.NORTH); 
    } 
} 

計算監聽器類

public class CalculateListener extends JFrame implements ActionListener{ 

/** 
* 
*/ 
private static final long serialVersionUID = 1L; 
private JPanel contentPane; 
private double subtotal; 
private double whiteBread, wheatBread; 
private double ham, turkey, cheese, mayo; 
private double soda, beer, tea, water; 

public CalculateListener(String bread, String drink, boolean hamOP, boolean turkeyOP, boolean cheeseOP, boolean mayoOP){ 
    subtotal = 0; 
    whiteBread = 1.00; 
    wheatBread = 1.25; 
    ham = 0.75; 
    turkey = 0.65; 
    cheese = 0.50; 
    mayo = 0.25; 
    soda = 0.65; 
    beer = 3.75; 
    tea = 2.25; 
    water = 0.0; 

    if(bread.equals("white")){ 
     subtotal += whiteBread; 
    }else{ 
     subtotal += wheatBread; 
    } 

    if(drink.equals("Soda")){ 
     subtotal += soda; 
    }else if(drink.equals("Beer")){ 
     subtotal += beer; 
    }else if(drink.equals("Tea")){ 
     subtotal += tea; 
    }else if(drink.equals("Water")){ 
     subtotal += water; 
    } 

    if(hamOP) 
     subtotal += ham; 
    if(turkeyOP) 
     subtotal += turkey; 
    if(cheeseOP) 
     subtotal += cheese; 
    if(mayoOP) 
     subtotal += mayo; 

} 

public void actionPerformed(ActionEvent e){ 


    setTitle("Your Bill"); 
    setResizable(false); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setBounds(100, 100, 300, 300); 
    contentPane = new JPanel(); 
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
    setContentPane(contentPane); 
    contentPane.setLayout(new BorderLayout(0, 0)); 

    JPanel panel = new JPanel(); 
    contentPane.add(panel, BorderLayout.CENTER); 
    panel.setLayout(new GridLayout(3, 1, 0, 0)); 

    JLabel lblSubtotal = new JLabel(" Subtotal: $" + subtotal); 
    panel.add(lblSubtotal); 

    JLabel lblTax = new JLabel("Tax: $" + (subtotal * 0.07)); 
    panel.add(lblTax); 

    JLabel lblTotal = new JLabel("Total: $" + ((subtotal * 0.07) + subtotal)); 
    panel.add(lblTotal); 

    JButton btnOk = new JButton("OK"); 
    contentPane.add(btnOk, BorderLayout.SOUTH); 
    btnOk.addActionListener(new ExitListener()); 


} 
} 

退出監聽器類

public class ExitListener implements ActionListener { 

/* (non-Javadoc) 
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) 
*/ 
@Override 
public void actionPerformed(ActionEvent e) { 
    // TODO Auto-generated method stub 
    System.exit(0); 
} 

} 

回答

1

ÿ OU只是忘了一行: setVisible(true);

你加它在CalculateListener類的actionPerformed(...)方法的底部。

+0

非常感謝你,我不能相信這件事很簡單。我一直在調試這個東西幾個小時,試圖弄清楚爲什麼它不起作用。謝謝 –

+0

我很高興能幫上忙!我有很多像這樣的愚蠢錯誤:) – Adrian

0

看來你正在使用主應用程序中的不同幀以及CalculateListener,因此需要使用偵聽器來計算值並將其設置在可以更新UI的主類上。嘗試檢查MVC模式,並嘗試在變量名稱上使用Java命名標準,如不帶_,並以小寫字母開頭。