constructor for view classweb.cse.ohio-state.edu/software/2221/web-sw1/...• a simple constructor...

24
Constructor for View Class 7 January 2019 OSU CSE 1

Upload: others

Post on 17-Jul-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Constructor for View Classweb.cse.ohio-state.edu/software/2221/web-sw1/...• A simple constructor for the . view. class has four main jobs: 1. Create the JFramebeing extended 2. Set

Constructor for View Class

7 January 2019 OSU CSE 1

Page 2: Constructor for View Classweb.cse.ohio-state.edu/software/2221/web-sw1/...• A simple constructor for the . view. class has four main jobs: 1. Create the JFramebeing extended 2. Set

Tasks To Be Performed• A simple constructor for the view class has four

main jobs:1. Create the JFrame being extended2. Set up the GUI widgets to be used and “lay out”

these widgets in the main application window3. Set up the observers by registering (in our

examples) the object being constructed, i.e., this, with each of the GUI widgets that might have events of interest to the application

4. Start the main application window

7 January 2019 OSU CSE 2

Page 3: Constructor for View Classweb.cse.ohio-state.edu/software/2221/web-sw1/...• A simple constructor for the . view. class has four main jobs: 1. Create the JFramebeing extended 2. Set

1: Create the JFramesuper("Simple GUI Demo");

7 January 2019 OSU CSE 3

Page 4: Constructor for View Classweb.cse.ohio-state.edu/software/2221/web-sw1/...• A simple constructor for the . view. class has four main jobs: 1. Create the JFramebeing extended 2. Set

External (GUI) Effectthis

7 January 2019 OSU CSE 4

Page 5: Constructor for View Classweb.cse.ohio-state.edu/software/2221/web-sw1/...• A simple constructor for the . view. class has four main jobs: 1. Create the JFramebeing extended 2. Set

External (GUI) Effectthis

7 January 2019 OSU CSE 5

Nothing illustrated in these slides actually becomes visible to the user until later!

Page 6: Constructor for View Classweb.cse.ohio-state.edu/software/2221/web-sw1/...• A simple constructor for the . view. class has four main jobs: 1. Create the JFramebeing extended 2. Set

2: Set Up GUI Widgets (Text)...this.inputText = new JTextArea("",

LINES_IN_TEXT_AREAS,

LINE_LENGTHS_IN_TEXT_AREAS);

...

JScrollPane inputTextScrollPane =

new JScrollPane(this.inputText);

7 January 2019 OSU CSE 6

Page 7: Constructor for View Classweb.cse.ohio-state.edu/software/2221/web-sw1/...• A simple constructor for the . view. class has four main jobs: 1. Create the JFramebeing extended 2. Set

2: Set Up GUI Widgets (Text)...this.inputText = new JTextArea("",

LINES_IN_TEXT_AREAS,

LINE_LENGTHS_IN_TEXT_AREAS);

...

JScrollPane inputTextScrollPane =

new JScrollPane(this.inputText);

7 January 2019 OSU CSE 7

5 lines,each of length 20

Page 8: Constructor for View Classweb.cse.ohio-state.edu/software/2221/web-sw1/...• A simple constructor for the . view. class has four main jobs: 1. Create the JFramebeing extended 2. Set

External (GUI) Effectthis.inputText

7 January 2019 OSU CSE 8

inputTextScrollPane

Page 9: Constructor for View Classweb.cse.ohio-state.edu/software/2221/web-sw1/...• A simple constructor for the . view. class has four main jobs: 1. Create the JFramebeing extended 2. Set

External (GUI) Effectthis.inputText

7 January 2019 OSU CSE 9

inputTextScrollPaneThe scrollbars in JScrollPanes

actually arise only when needed; not needed yet, but illustrated here.

Page 10: Constructor for View Classweb.cse.ohio-state.edu/software/2221/web-sw1/...• A simple constructor for the . view. class has four main jobs: 1. Create the JFramebeing extended 2. Set

2: Set Up GUI Widgets (Buttons)...this.copyButton =

new JButton("Copy Input");

7 January 2019 OSU CSE 10

Page 11: Constructor for View Classweb.cse.ohio-state.edu/software/2221/web-sw1/...• A simple constructor for the . view. class has four main jobs: 1. Create the JFramebeing extended 2. Set

External (GUI) Effect

this.copyButton

7 January 2019 OSU CSE 11

Page 12: Constructor for View Classweb.cse.ohio-state.edu/software/2221/web-sw1/...• A simple constructor for the . view. class has four main jobs: 1. Create the JFramebeing extended 2. Set

2: ... and Lay Out GUI WidgetsJPanel buttonPanel =

new JPanel(

new GridLayout(

ROWS_IN_BUTTON_PANEL_GRID,

COLUMNS_IN_BUTTON_PANEL_GRID));

...

buttonPanel.add(this.resetButton);buttonPanel.add(this.copyButton);

7 January 2019 OSU CSE 12

Page 13: Constructor for View Classweb.cse.ohio-state.edu/software/2221/web-sw1/...• A simple constructor for the . view. class has four main jobs: 1. Create the JFramebeing extended 2. Set

2: ... and Lay Out GUI WidgetsJPanel buttonPanel =

new JPanel(

new GridLayout(

ROWS_IN_BUTTON_PANEL_GRID,

COLUMNS_IN_BUTTON_PANEL_GRID));

...

buttonPanel.add(this.resetButton);buttonPanel.add(this.copyButton);

7 January 2019 OSU CSE 13

1 row,2 columns

Page 14: Constructor for View Classweb.cse.ohio-state.edu/software/2221/web-sw1/...• A simple constructor for the . view. class has four main jobs: 1. Create the JFramebeing extended 2. Set

External (GUI) Effect

buttonPanel

7 January 2019 OSU CSE 14

Page 15: Constructor for View Classweb.cse.ohio-state.edu/software/2221/web-sw1/...• A simple constructor for the . view. class has four main jobs: 1. Create the JFramebeing extended 2. Set

2: ... and Lay Out GUI Widgetsthis.setLayout(new GridLayout(

ROWS_IN_THIS_GRID,

COLUMNS_IN_THIS_GRID));

...

this.add(inputTextScrollPane);this.add(buttonPanel);this.add(outputTextScrollPane);

7 January 2019 OSU CSE 15

Page 16: Constructor for View Classweb.cse.ohio-state.edu/software/2221/web-sw1/...• A simple constructor for the . view. class has four main jobs: 1. Create the JFramebeing extended 2. Set

2: ... and Lay Out GUI Widgetsthis.setLayout(new GridLayout(

ROWS_IN_THIS_GRID,

COLUMNS_IN_THIS_GRID));

...

this.add(inputTextScrollPane);this.add(buttonPanel);this.add(outputTextScrollPane);

7 January 2019 OSU CSE 16

3 rows,1 column

Page 17: Constructor for View Classweb.cse.ohio-state.edu/software/2221/web-sw1/...• A simple constructor for the . view. class has four main jobs: 1. Create the JFramebeing extended 2. Set

2: ... and Lay Out GUI Widgetsthis.setLayout(new GridLayout(

ROWS_IN_THIS_GRID,

COLUMNS_IN_THIS_GRID));

...

this.add(inputTextScrollPane);this.add(buttonPanel);this.add(outputTextScrollPane);

7 January 2019 OSU CSE 17

Remember:the two buttons

are in this panel.

Page 18: Constructor for View Classweb.cse.ohio-state.edu/software/2221/web-sw1/...• A simple constructor for the . view. class has four main jobs: 1. Create the JFramebeing extended 2. Set

External (GUI) Effectthis

7 January 2019 OSU CSE 18

Page 19: Constructor for View Classweb.cse.ohio-state.edu/software/2221/web-sw1/...• A simple constructor for the . view. class has four main jobs: 1. Create the JFramebeing extended 2. Set

External (GUI) Effectthis

7 January 2019 OSU CSE 19

Remember:none of this is visible to the

user yet.

Page 20: Constructor for View Classweb.cse.ohio-state.edu/software/2221/web-sw1/...• A simple constructor for the . view. class has four main jobs: 1. Create the JFramebeing extended 2. Set

3: Set Up the Observersthis.resetButton.addActionListener(this);this.copyButton.addActionListener(this);

7 January 2019 OSU CSE 20

Page 21: Constructor for View Classweb.cse.ohio-state.edu/software/2221/web-sw1/...• A simple constructor for the . view. class has four main jobs: 1. Create the JFramebeing extended 2. Set

Internal (non-GUI) Effect

7 January 2019 OSU CSE 21

Page 22: Constructor for View Classweb.cse.ohio-state.edu/software/2221/web-sw1/...• A simple constructor for the . view. class has four main jobs: 1. Create the JFramebeing extended 2. Set

4: Start the Main Windowthis.pack();this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setVisible(true);

7 January 2019 OSU CSE 22

Page 23: Constructor for View Classweb.cse.ohio-state.edu/software/2221/web-sw1/...• A simple constructor for the . view. class has four main jobs: 1. Create the JFramebeing extended 2. Set

External (GUI) Effect: Now Visiblethis

7 January 2019 OSU CSE 23

Page 24: Constructor for View Classweb.cse.ohio-state.edu/software/2221/web-sw1/...• A simple constructor for the . view. class has four main jobs: 1. Create the JFramebeing extended 2. Set

External (GUI) Effect: Now Visiblethis

7 January 2019 OSU CSE 24

The only code you wrote that executes now is the callback method for the two buttons:this.actionPerformed.