10.07.2015 Views

12 Factory GUI

12 Factory GUI

12 Factory GUI

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

import java.awt.*;import java.awt.event.*;public class <strong>Factory</strong>AWT implements Component<strong>Factory</strong> {}public Component createButton(String label, ActionListener listener){Button b = new Button(label);b.addActionListener(listener);return b;}public Component createLabel(String label){return new Label(label);}public Component createField(int width){return new AWTTextField(width);}public Frame createFrame(String title){Frame f = new Frame(title);f.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}});return f;}static {Current<strong>Factory</strong>.set<strong>Factory</strong>(new <strong>Factory</strong>AWT());}class AWTTextField extends TextField implements Component<strong>Factory</strong>.Field {AWTTextField(int width){super(width);}}


import java.awt.*;import java.awt.event.*;import javax.swing.*;public class <strong>Factory</strong>Swing implements Component<strong>Factory</strong> {}public Component createButton(String label, ActionListener listener){JButton b = new JButton (label);b.addActionListener(listener);return b;}public Component createLabel(String label){return new JLabel(label);}public Component createField(int width){return new SwingField(width);}public Frame createFrame(String title){return new SwingFrame(title);}static {Current<strong>Factory</strong>.set<strong>Factory</strong>(new <strong>Factory</strong>Swing());}class SwingField extends JTextField implements Component<strong>Factory</strong>.Field {public SwingField(int width){super(width);}}class SwingFrame extends JFrame {SwingFrame(String title){super(title);setDefaultCloseOperation(EXIT_ON_CLOSE);}}}public Component add(Component c){return getContentPane().add(c);}public Component add(String name, Component c){return getContentPane().add(name, c);}public void setLayout(LayoutManager m){if(isRootPaneCheckingEnabled())getContentPane().setLayout(m);elsesuper.setLayout(m);


import java.awt.*;import java.awt.event.*;public class <strong>GUI</strong>1 {}}public static void main(String[] args){Component<strong>Factory</strong> gui = new <strong>Factory</strong>AWT();Frame f = gui.createFrame("Calculator");final Component x = gui.createField(10);final Component y = gui.createField(10);final Component sum = gui.createField(10);Component b = gui.createButton("Compute",new ActionListener(){public void actionPerformed(ActionEvent e){int ix = Integer.parseInt(((Component<strong>Factory</strong>.Field)x).getText());int iy = Integer.parseInt(((Component<strong>Factory</strong>.Field)y).getText());((Component<strong>Factory</strong>.Field)sum).setText("" + (ix + iy));}});f.setLayout(new GridLayout(4, 2));f.add(gui.createLabel("x")); f.add(x);f.add(gui.createLabel("y")); f.add(y);f.add(gui.createLabel("sum")); f.add(sum);f.add(b);f.pack();f.setVisible(true);

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!