1 / 17

Announcements/Reminders

Announcements/Reminders. Next week: No Lectures No Labs Recitation on Friday (review for Final Exam) Project 8 due Thursday April 28 Final Exam Tuesday, May 3: 1:00 - 3:00 PM (Phys 112) Cumulative (weighted towards recent material)

sharis
Télécharger la présentation

Announcements/Reminders

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Announcements/Reminders • Next week: • No Lectures • No Labs • Recitation on Friday (review for Final Exam) • Project 8 due Thursday April 28 • Final Exam • Tuesday, May 3: 1:00 - 3:00 PM (Phys 112) • Cumulative (weighted towards recent material) • 40 MC (2 points each), 6 programming (20 points each) = 200 points total

  2. Chapter 13 Applets and HTML HTML Applets

  3. Gotcha: Using an Old Web Browser • Updates to browsers are usually much later than updates to core Java language. • Web browsers don't use the same Java interpreter that runs Java applications. • Test your applets in different browsers and in different versions of browsers. • Can't expect everyone to have most recent browser. • Might need to use older Applet class if you want your applet to run in older browsers.

  4. The Older Applet Class • Older versions of web browsers can't run applets that use JApplet and other Swing classes. • Using the old Applet class instead of JApplet will allow your applet to run in more browsers. • To convert a JApplet program to an Applet program: • Replace: import javax.swing.*; with import java.applet.*; • Replace Swing classes that start with J (JButton, JPanel, etc.) with AWT classes that don't start with J (Button, Panel, etc.) • Remove references to the content pane of the applet.

  5. Applets and Security • Applets are someone else's program running on your computer. • Security issues: • viruses • reading confidential information • corrupting operating system • Security features of applets to help avoid these problems: • Applets cannot run any programs on your computer. • Applets cannot read or write to files on your computer (unless the applet originated on your computer).

  6. Chapter 14 More Swing Menus Making GUIs Pretty (and More Functional) Box Containers and Box Layout Managers More on Events and Listeners Another Look at the Swing Class Hierarchy

  7. AbstractButton JMenuItem JButton JMenu Menus • Three Swing classes used to put a menu in a program: • JMenuBar • JMenu • JMenuItem • Menu items behave in the same way as buttons

  8. A GUI witha Menu JMenu memoMenu = new JMenu("Memos"); JMenuItem m; m = new JMenuItem("Save Memo 1"); m.addActionListener(this); memoMenu.add(m); m = new JMenuItem("Save Memo 2"); m.addActionListener(this); memoMenu.add(m); . . . JMenuBar mBar = new JMenuBar(); mBar.add(memoMenu); setJMenuBar(mBar); Create a menu Create a menu item A menu item uses an action listener the same way a button does.

  9. A GUI witha Menu JMenu memoMenu = new JMenu("Memos"); JMenuItem m; m = new JMenuItem("Save Memo 1"); m.addActionListener(this); memoMenu.add(m); m = new JMenuItem("Save Memo 2"); m.addActionListener(this); memoMenu.add(m); . . . JMenuBar mBar = new JMenuBar(); mBar.add(memoMenu); setJMenuBar(mBar); Each menu item is added to the menu. The menu is added to the menu bar. One way to add a menu bar to a JFrame

  10. AbstractButton JMenuItem JButton JMenu Nested Menus • JMenu is a descendant of JMenuItem • Every JMenu object is also a JMenuItem • A JMenu can be a menu item in another menu • This allows nested menus • Clicking on a nested menu shows the items in the nested menu and allows them to be selected.

  11. The JScrollPane Class for Scroll Bars • A view port is used when not all information can be displayed on screen at once. • Scroll bars move a view port around to show different parts of the information. • JScrollPane is a class that can provide a view port with scroll bars. • An example using JScrollPane with a JTextArea called theText and a JPanel called textPanel: JScrollPane scrolledText = new JScrollPane(theText); textPanel.add(scrolledText);

  12. JScrollPane Example JPanel textPanel = new Jpanel(); theText = new JTextArea(10, 40); JScrollPane scrolledText = new JScrollPane(theText); scrolledText.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); scrolledText.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); textPanel.add(scrolledText); contentPane.add(textPanel);

  13. The CardLayout Manager • Allows a set of views (components) to choose among • Only one view is visible at a time. • Can go through views in order or jump to any view. • Often the components added to a CardLayout will be panels. • Each component added to a CardLayout has a string associated with it that works like a name: deckPanel.add("start", startCardPanel); • The string can be used later to display that component (or view): dealer.show(deckPanel, "start"); • Need reference to layout manager to change views, so do not use anonymous object: deckPanel.setLayout(new CardLayout()); legal but useless

  14. CardLayoutDemo Only one of these three panels will be visible at a time. deckPanel = new JPanel(); dealer = new CardLayout(); deckPanel.setLayout(dealer); ... deckPanel.add("start", startCardPanel); ... deckPanel.add("green", greenCardPanel); ... deckPanel.add("red", redCardPanel); ... dealer.show(deckPanel, "red"); ... dealer.next(deckPanel); will show redCardPanel if redCardPanel is currently displayed, will show startCardPanel

  15. Changing Components • A program can add or remove components after a GUI has been displayed, but that is beyond the scope of the book. • Making components visible or not visible gives a similar effect. • The setVisible method is used in the VisibleDemo program to make only one of the red and green labels visible at a time. (code on next slide)

  16. Changing Components The actionPerformed method from the VisibleDemo program public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals(“Red”)) { colorPanel.setBackground(Color.red); stopLabel.setVisible(false); goLabel.setVisible(true); validate(); } . . . } There is similar code for when the Green button is pressed, which turns the background green and hides the go label. Visibility changes won’t occur until the validate method is called.

  17. Summary • You can add icons to JButtons, JLabels, and JMenuItems. • A JMenuBar can be added to a JFrame with the method setJMenuBar or with the usual add method. • Both buttons and menu items fire action events and so should have an ActionListener registered with them. • You can use the class JScrollPane to add scroll bars to a text area. • You can define a window listener class by having it implement the WindowListener interface. • If you want a close-button to do something other than close the window, you must use SetDefaultCloseOperation. • If you change the visibility of a component you should use the validate method to update the GUI.

More Related