constructor for view class
play

Constructor for View Class 7 January 2019 OSU CSE 1 Tasks To Be - PowerPoint PPT Presentation

Constructor for View Class 7 January 2019 OSU CSE 1 Tasks To Be Performed A simple constructor for the view class has four main jobs: 1. Create the JFrame being extended 2. Set up the GUI widgets to be used and lay out these widgets


  1. Constructor for View Class 7 January 2019 OSU CSE 1

  2. Tasks To Be Performed • A simple constructor for the view class has four main jobs: 1. Create the JFrame being extended 2. Set up the GUI widgets to be used and “lay out” these widgets in the main application window 3. 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

  3. 1: Create the JFrame super ("Simple GUI Demo"); 7 January 2019 OSU CSE 3

  4. External (GUI) Effect this 7 January 2019 OSU CSE 4

  5. External (GUI) Effect this Nothing illustrated in these slides actually becomes visible to the user until later! 7 January 2019 OSU CSE 5

  6. 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

  7. 2: Set Up GUI Widgets (Text)... this .inputText = new JTextArea("", LINES_IN_TEXT_AREAS, LINE_LENGTHS_IN_TEXT_AREAS); ... JScrollPane inputTextScrollPane = 5 lines, each of length 20 new JScrollPane( this .inputText); 7 January 2019 OSU CSE 7

  8. External (GUI) Effect this .inputText inputTextScrollPane 7 January 2019 OSU CSE 8

  9. External (GUI) Effect this .inputText The scrollbars in inputTextScrollPane JScrollPane s actually arise only when needed; not needed yet, but illustrated here. 7 January 2019 OSU CSE 9

  10. 2: Set Up GUI Widgets (Buttons)... this .copyButton = new JButton("Copy Input"); 7 January 2019 OSU CSE 10

  11. External (GUI) Effect this .copyButton 7 January 2019 OSU CSE 11

  12. 2: ... and Lay Out GUI Widgets JPanel 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

  13. 2: ... and Lay Out GUI Widgets JPanel buttonPanel = 1 row, 2 columns 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

  14. External (GUI) Effect buttonPanel 7 January 2019 OSU CSE 14

  15. 2: ... and Lay Out GUI Widgets this .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

  16. 2: ... and Lay Out GUI Widgets this .setLayout( 3 rows, 1 column 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

  17. 2: ... and Lay Out GUI Widgets this .setLayout( Remember: the two buttons new GridLayout( are in this panel. ROWS_IN_THIS_GRID, COLUMNS_IN_THIS_GRID)); ... this .add(inputTextScrollPane); this .add(buttonPanel); this .add(outputTextScrollPane); 7 January 2019 OSU CSE 17

  18. External (GUI) Effect this 7 January 2019 OSU CSE 18

  19. External (GUI) Effect this Remember: none of this is visible to the user yet. 7 January 2019 OSU CSE 19

  20. 3: Set Up the Observers this .resetButton.addActionListener( this ); this .copyButton.addActionListener( this ); 7 January 2019 OSU CSE 20

  21. Internal (non-GUI) Effect 7 January 2019 OSU CSE 21

  22. 4: Start the Main Window this .pack(); this .setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); this .setVisible( true ); 7 January 2019 OSU CSE 22

  23. External (GUI) Effect: Now Visible this 7 January 2019 OSU CSE 23

  24. External (GUI) Effect: Now Visible this The only code you wrote that executes now is the callback method for the two buttons: this .actionPerformed . 7 January 2019 OSU CSE 24

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend