/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package view; /** * * @author cirstea */ import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Observable; import java.util.Observer; import javax.swing.*; import library.Converter; import model.Computer; public class HexView implements Observer { private JFrame frame = new JFrame("Calculator"); private JPanel[] panels = new JPanel[6]; private JTextField textField = new JTextField(); private JButton[] numberButtons = new JButton[10]; private JButton[] hexaButtons = new JButton[6]; private JButton subtractButton = new JButton("-"); private JButton addButton = new JButton("+"); private JButton equateButton = new JButton(" = "); private final Computer model; // the operand displayed in the TextField // it changes as soon as one types a digit private String operand = ""; public HexView(Computer model) { this.model = model; model.addObserver(this); buildFrame(); } public void buildFrame() { frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel contentPane = (JPanel) frame.getContentPane(); // initialize panels for (int i = 0; i < panels.length; i++) { panels[i] = new JPanel(); } // initialize button 0-9 for (int i = 0; i < numberButtons.length; i++) { numberButtons[i] = new JButton(" " + i + " "); numberButtons[i].addActionListener(new DigitListener(i + "")); } for (int i = 0; i < hexaButtons.length; i++) { hexaButtons[i] = new JButton(" " + new Character((char) (i + 65)) + " "); hexaButtons[i].addActionListener(new DigitListener(new Character((char) (i + 65)).toString())); } // default layout = BorderLayout.CENTER textField.setColumns(20); textField.setText("0"); textField.setHorizontalAlignment(JTextField.RIGHT); panels[0].add(textField); // layout = FlowLayout.LEFT panels[1].setLayout(new FlowLayout(FlowLayout.LEFT)); panels[1].add(numberButtons[7]); panels[1].add(numberButtons[8]); panels[1].add(numberButtons[9]); panels[1].add(addButton); addButton.addActionListener(new OpListener(Converter.PLUS)); // layout = FlowLayout.LEFT panels[2].setLayout(new FlowLayout(FlowLayout.LEFT)); panels[2].add(numberButtons[4]); panels[2].add(numberButtons[5]); panels[2].add(numberButtons[6]); panels[2].add(subtractButton); subtractButton.addActionListener(new OpListener(Converter.MINUS)); // layout = FlowLayout.LEFT panels[3].setLayout(new FlowLayout(FlowLayout.LEFT)); panels[3].add(numberButtons[1]); panels[3].add(numberButtons[2]); panels[3].add(numberButtons[3]); panels[3].add(equateButton); equateButton.addActionListener(new OpListener(Converter.EQUAL)); // layout = FlowLayout.LEFT panels[4].setLayout(new FlowLayout(FlowLayout.LEFT)); panels[4].add(numberButtons[0]); panels[4].add(hexaButtons[0]); panels[4].add(hexaButtons[1]); panels[4].add(hexaButtons[2]); // layout = FlowLayout.LEFT panels[5].setLayout(new FlowLayout(FlowLayout.LEFT)); panels[5].add(hexaButtons[3]); panels[5].add(hexaButtons[4]); panels[5].add(hexaButtons[5]); contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS)); for (JPanel jPanel : panels) { contentPane.add(jPanel); } frame.pack(); frame.setVisible(true); } @Override public void update(Observable o, Object o1) { operand = model.getOperand() + ""; operand = Integer.toHexString(Integer.parseInt(operand)); textField.setText(operand); } class OpListener implements ActionListener { private int op; public OpListener(int op) { this.op = op; } @Override public void actionPerformed(ActionEvent e) { model.setOperation(op); operand = ""; } } class DigitListener implements ActionListener { private String digit; public DigitListener(String digit) { this.digit = digit; } @Override public void actionPerformed(ActionEvent e) { operand = operand + digit; model.setOperand(Integer.parseInt(operand, 16)); } } }