1 / 21

Java Swing GUIs and Arrays Tutorial

Learn how to create GUIs using Java's Swing library, work with arrays (including 2D arrays), and respond to user input. Includes information on JFrame, JLabel, JTextField, JTextArea, JButton, JPanel, and layout managers.

jandria
Télécharger la présentation

Java Swing GUIs and Arrays Tutorial

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. CSSE221: Software Dev. Honors Day 8 • Announcements • HW3 due now, solutions will be posted. • Questions on Arrays? • Max # of elements in an array: • Integer.MAX_INT, but your machine probably doesn’t have enough space to hold this much data! And the JVM will certainly not claim all your memory even if you did. • Newer machines (with 64-bit addressing) will have more… but I don’t know how Java is responding (perhaps Java 7 will have long int array indices) • Today is Constitution Day. • Speaker in Kahn Room in Union, 10th hour. • No baby yet…

  2. Capsule Deliverables: sec 1 • Please email the quiz, key, and summary to me by 7:30 am on the day you are presenting. • Send .doc files • (If you get them to me by 3pm on a weekday before you present, I’ll still print them) • Lessons from week 1: • Nice research and summarization! • Proofread your stuff. Everyone else will see it. • 1-page quizzes are good (or staple) • Summaries don’t have to be verbose, just dense with info.

  3. This week: Fifteen assignment • Monday: • Fifteen specification • GUIs using Java’s Swing library • Intro to UML as a design tool • Start Fifteen • Tuesday: • EventListeners: responding to user input • Time to work on project • Thursday: • Anonymous classes (capsule) • Function objects and comparators (capsule)

  4. “Fifteen” Arrays (especially 2D) Creating GUIs using Swing Responding to mouse clicks

  5. Fifteem Teams Repos is csse221-200810-<usr1>-<usr2>. If solo, use your personal repos. • beltonj1-fishmad • bennetrn-leveyjr • bennindp-speyerea • crockeea-morrisps • devorejd-priestjs • lundgrpb-skilessa • johnsoad-smithny • sullivja-wentztj • mcginnda Discuss how to pair-program

  6. The Java Swing Library • JFrames, JTextBoxes, JButtons, JScrollPanes… what’s available? • What components will I need… • …now, for Fifteen? • …later, for a term project? • Browse the Visual Index to the Swing Components in Sun’s Java Tutorial.

  7. JFrame • A simple window that contains the other GUI components • Background Color • getContentPane().setBackground(Color); • Make sure to set visible • setVisible(true); • Setting the size • setSize(width, height);

  8. Benefits of Extending JFrame or JComponents like JPanel • When extending the JFrame class we can still instantiate it as JFrame. • “Controlling” the JFrame is easier to comprehend when we do not need the calling object stated every time. i.e. setSize(w,h); instead of jFrameObject.setSize(w,h);

  9. JLabel • Displays text to user • User cannot edit text • Construction: JLabel message = new JLabel(String text); Methods: • String text = message.getText(); • message.setText(String text);

  10. JTextField • Lets user enter or modify a single line of text • Construction: • JTextField text = new JTextField(int WidthInCharacters); • Getting the Text: • String input = text.getText(); • Setting text in a Text Field: • text.setText(String output);

  11. JTextArea • Lets user enter or modify multiple lines of text. • Declaration: private JTextArea text; • Constructors: • text = new JTextArea(int rows, int columns); • text = new JTextArea(String output); • text = new JTextArea(String output, int rows, int columns); • Useful methods: • String input = text.getText(); • text.setText(String output); • setEditable(Boolean editable); • boolean editable = text.isEditable();

  12. JButton • Used to let user execute action • Construction: JButton button = new JButton(String text); • button.setBackground(Color color);

  13. JPanel • Simple container that groups objects • Meant to subdivide • Creation Syntax: • JPanel panelName = new JPanel(); • Useful attributes and methods • .setLayout(new Layout()); • .add(component); • .setBackground(Color color);

  14. Layout Managers • Frames are organized by layout managers. • Organizes the locations of the components within the frame • Not only frames use layout managers, but Panels are also organized by layout managers. • Allows for panels within panels.

  15. Layout: Flow Layout • Places the components from the left side to the right side. • Wraps around the right side. • .setLayout(new FlowLayout()); • No special add syntax.

  16. Layout: Border Layout • North, South, East, West, Center • setLayout(new BorderLayout()); • .add(component, BorderLayout.NORTH);

  17. Layout: Grid Layout • Similar to flow in the left to right manner, but the grid that you define limits the size of any one component • You can leave out a definite definition of how many rows or columns are in the grid. • setLayout(new GridLayout(r,c)); • Adds in components from left to right, top to bottom. Cannot specify which block.

  18. PaintComponent • Used with JPanels, and all JComponents, to paint the components on the screen. • Called automatically, no need to invoke it. • Use repaint(); to force the paintComponent() method to execute. • Start with super.paintComponent(g); where g is your Graphics object publicvoid paintComponent(Graphics g) { super.paintComponent(g); setBackgroundColor(Color.red); g.drawOval(10,40,20,20); }

  19. Alternative designs • I want a component to appear. Question: • Should I paint it? Add a new component? • Answer: It depends… • On how much control you want over its appearance • On how you want it to respond to events • It’s usually a tradeoff. Press me!

  20. Listeners • If you want to respond to events, you need to implement a Listener interface. • We’ll look at these in detail tomorrow

  21. Demo together

More Related