Gregory Desrosiers Montreal

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Friday, 31 May 2013

JAVA Program: Displaying Unlimited Strings on a GUI Program

Posted on 21:00 by Unknown
Hello, everyone.

Here's an experimental program I had to do for one of my Data Structures assignments I have recently completed.

When you run the program, you type in the string, without seeing what it looks like, then press Enter to display it on the green canvas. It's not really a fun program, but this is to demonstrate the power of what's called action listeners. They are vitally important for GUI applications like this one.

Documentation is provided for your convenience. And you can copy and modify this code at your convenience, using a text editor or an IDE.

Enjoy!


/*
 *     Data Structures and Object-Oriented Programming - Assignment 7
 *     Question 16.10 of Introduction to Java Programming (by Daniel Liang)
 *
 *     Enter and Display a String
 *
 *     This is an example on how to use a keyboard listener. You type in a sequence
 *     of characters, then when you press enter, what you type in is displayed on
 *     the screen.
 *
 *     It's not really fancy, but this is to show off what a keyboard listener is
 *     about. You can resize the window to make it wider or narrower, but only one
 *     line of text is displayed and is always at the top of the panel, right below
 *     the frame's top side.
 *
 *     Programmed by Gregory Desrosiers
 *
 *     Start Date: March 24, 2013
 *     End Date: March 24, 2013
 *
 *     File Name: Chapter16Exercise10.java
 *     Teacher: Amin Ranj Bar
 *
 */

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Chapter16Exercise10 extends JPanel
{
    private static final long serialVersionUID = -7300674970023098222L;
  
    // Text Entered
    private StringBuilder text = new StringBuilder("");
  
    // Font Family
    private Font font = new Font("Tahoma", Font.PLAIN, 12);

    public static void main(String[] args)
    {
        JFrame frame = new JFrame();
      
        Chapter16Exercise10 panel = new Chapter16Exercise10();
        panel.setFocusable(true);
        frame.add(panel);
      
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 100);
        frame.setVisible(true);
    }
  
    public Chapter16Exercise10()
    {
        setBackground(new Color(0, 128, 0));
        setForeground(Color.WHITE);
        addKeyListener(new KeyAdapter() {
          
            public void keyPressed(KeyEvent e)
            {
                if (e.getKeyCode() == KeyEvent.VK_ENTER)
                    repaint();
                else
                    text.append(e.getKeyChar());
              
            }
        });
    }
  
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
      
        g.setFont(font);
        g.drawString(text.toString(), 0, 15);
        text.delete(0, text.length());
    }
}
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Posted in | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • The Quebec Ministerial Exam of College English (English Exit Exam)
    Hello, everyone. How you doing? I'm good. There is some unfortunate news for me to deliver, though I am planning to try to work myself a...
  • String Letter Counter (JAVA Program)
    Hello, guys. This is another JAVA Program I want to share with you guys. It's a counter for a specific letter in an entered string strai...
  • The Asperger Computer: A Self-Published Guide on Asperger's Syndrome and Multimedia
    The Asperger Computer - A Self-Published Guide on Asperger's Syndrome and Multimedia by Gregory Desrosiers How is Asperger’s syndrome li...
  • The Asperger Computer (e-book) - Sample Paragraph
    This is the sample paragraph for my e-book adaptation of a speech I've delivered at my college, Champlain College Saint-Lambert, on May ...
  • The Asperger Computer - Table des matières et une sample
    Excusez-moi si il ya beaucoup d'erreurs avec l'écriture , j'ai dû utiliser un traducteur pour obtenir ce parce que je su...
  • JAVA Program: Number Loop (Experimental Program of the Timer class)
    Hello there. Here's another program I want to present to you: it's a self-running experiment that tests the Timer class provided in ...
  • Fundraising Project (To Autism Society Canada)
    Hi, everyone! How are you people doing? I am feeling tired and stuff, but there are a couple of things I want to bring up with you. Tomorrow...
  • Arcade Games to check out
    Hello, everyone. How you people doing? I'm okay, though I am feeling mightily frustrated and lazy to try to work on a lot of things befo...
  • JAVA Program: Command-based String Remover in Text Files
    Hello, everyone. Let me share with you the code on one of the programs I did back in March 2013, while doing my Data Structures class. This ...
  • JAVA Program: Military Time to Real Time
    Hello, people. How you doing? I'm really busy with my college homework, but I am working so hard to pass my courses and continue in my s...

Blog Archive

  • ▼  2013 (38)
    • ►  August (2)
    • ►  July (9)
    • ►  June (12)
    • ▼  May (12)
      • JAVA Program: Number Loop (Experimental Program of...
      • JAVA Program: Displaying Unlimited Strings on a GU...
      • JAVA Program: Command-based String Remover in Text...
      • Arcade Games to check out
      • JAVA Program: Random 0s and 1s Table with Refresh ...
      • JAVA Program: Object-Oriented Programming Class, t...
      • Announcement on "The Asperger Computer"
      • JAVA Program : Hello World!
      • Introducing myself (original blog on Autism Suppor...
      • Demonstration of JAVA Classes
      • Non-fiction e-book, The Asperger Computer, reveals...
      • The Asperger Computer - Facebook Edition
    • ►  April (2)
    • ►  January (1)
  • ►  2012 (24)
    • ►  December (13)
    • ►  November (3)
    • ►  October (5)
    • ►  September (1)
    • ►  August (2)
Powered by Blogger.

About Me

Unknown
View my complete profile