Last active
          January 10, 2016 09:10 
        
      - 
      
- 
        Save AnnaBoro/ea8b2294999e9419d24b to your computer and use it in GitHub Desktop. 
    StoreUI + ActionListener
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | package lesson5_8.shop; | |
| import javax.swing.*; | |
| import java.awt.*; | |
| import java.awt.event.ActionEvent; | |
| import java.awt.event.ActionListener; | |
| import java.text.NumberFormat; | |
| import java.util.List; | |
| public class StoreUI { | |
| private Store2 store; | |
| private ButtonGroup group = new ButtonGroup(); | |
| public StoreUI(Store2 store) { | |
| this.store = store; | |
| JFrame frame = new JFrame("STORE"); | |
| frame.setMinimumSize(new Dimension(800, 600)); | |
| frame.setLocation(300, 200); | |
| frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
| frame.getContentPane().add(createSellingPanel()); | |
| frame.pack(); | |
| frame.setVisible(true); | |
| } | |
| private JPanel createSellingPanel() { | |
| JPanel panel = new JPanel(); | |
| panel.setBackground(Color.CYAN); | |
| GridBagLayout gbl = new GridBagLayout(); | |
| panel.setLayout(gbl); | |
| GridBagConstraints c = new GridBagConstraints(); | |
| c.anchor = GridBagConstraints.NORTHWEST; | |
| c.fill = GridBagConstraints.NONE; | |
| c.gridheight = 1; | |
| c.gridwidth = 1; | |
| c.gridx = GridBagConstraints.RELATIVE; | |
| c.gridy = GridBagConstraints.RELATIVE; | |
| c.insets = new Insets(10, 0, 0, 0); | |
| c.ipadx = 0; | |
| c.ipady = 0; | |
| c.weightx = 0.0; | |
| c.weighty = 0.0; | |
| JLabel jName = new JLabel("Enter name: "); | |
| final JTextField jTField = new JTextField(30); | |
| gbl.setConstraints(jName, c); | |
| c.gridwidth = GridBagConstraints.REMAINDER; | |
| gbl.setConstraints(jTField, c); | |
| panel.add(jName); | |
| panel.add(jTField); | |
| List<Bird> birds = store.getBirds(); | |
| JLabel jBirds = new JLabel("Birds"); | |
| JPanel jPBirds = new JPanel(); | |
| jPBirds.setBorder(BorderFactory.createLineBorder(Color.BLUE)); | |
| jPBirds.setLayout(new GridLayout(birds.size(), 0)); | |
| for (Bird b : birds) { | |
| JRadioButton birdButton = new JRadioButton(b.getName() + " " + b.getPrice()); | |
| birdButton.setActionCommand(b.getName()); | |
| jPBirds.add(birdButton); | |
| group.add(birdButton); | |
| } | |
| c.gridwidth = 1; | |
| gbl.setConstraints(jBirds, c); | |
| c.gridwidth = GridBagConstraints.REMAINDER; | |
| gbl.setConstraints(jPBirds, c); | |
| panel.add(jBirds); | |
| panel.add(jPBirds); | |
| JLabel jNumber = new JLabel("Enter quantity: "); | |
| NumberFormat nf = NumberFormat.getInstance(); | |
| final JFormattedTextField jTField2 = new JFormattedTextField(nf); | |
| jTField2.setValue(1); | |
| c.gridwidth = 1; | |
| gbl.setConstraints(jNumber, c); | |
| c.gridwidth = GridBagConstraints.REMAINDER; | |
| gbl.setConstraints(jTField2, c); | |
| panel.add(jNumber); | |
| panel.add(jTField2); | |
| final JButton buyButton = new JButton("BUY"); | |
| c.ipadx = 100; | |
| c.anchor = GridBagConstraints.CENTER; | |
| gbl.setConstraints(buyButton, c); | |
| buyButton.addActionListener(new ActionListener() { | |
| @Override | |
| public void actionPerformed(ActionEvent e) { | |
| Buyer b = new Buyer(); | |
| b.setName(jTField.getText()); | |
| String birdSTR = group.getSelection().getActionCommand(); | |
| int num = Integer.parseInt(jTField2.getText()); | |
| store.sell(birdSTR, num, b); | |
| } | |
| }); | |
| panel.add(buyButton); | |
| return panel; | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment