2017-05-03 2 views
0

GUI 작성 방법을 배우고 있으며, actionListeners를 이해하는 데 도움이 필요합니다. 모든 것은 코드화되어 있습니다. 그러나 어떤 이유로 인해 Calculate Button은 실제로 메인 윈도우의 결과와 함께 새로운 윈도우를 열지 않습니다. 이 프로그램은 샌드위치 가게로 샌드위치를 ​​만들고 음료를 선택할 수 있습니다. 사용자가 완료되면 계산을 클릭하고 다른 종료 버튼과 함께 소계, 세금 및 최종 합계가 표시된 새 창을 여는 것으로 가정합니다. 지금까지 제가 가지고있는 모든 코드가 있습니다. 누구든지 제공 할 수있는 지침을 주시면 감사하겠습니다.동작 수신기 결과가 새 창이 열림

샌드위치 가게

/** 
* 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

Y setVisible(true);

당신은 CalculateListener 클래스의 actionPerformed(...) 방법의 하단에 추가 : OU는 단지 하나의 라인을 잊어 버렸습니다.

+0

정말 고마워요, 그게 그렇게 단순한 것이 었습니다. 나는 왜 그것이 작동하지 않는지 알아 내려고 노력하면서이 문제를 디버깅 해왔다. 감사합니다. –

+0

도와 드릴 수있어서 기쁩니다! 나는 이것도 바보 같은 실수를 많이 겪었습니다 :) – Adrian

0

당신이 값을 계산하고 UI를 업데이트 할 수 있습니다 메인 클래스를 설정하기 위해 수신기를 사용할 필요가 당신이 당신의 주요 응용 프로그램과 CalculateListener 다른 프레임으로 작업하는 것 같다. MVC 패턴을 확인하고 변수 이름에 _이없고 소문자로 시작하는 것과 같은 Java 명명 표준을 사용해보십시오.