Friday, April 24, 2009

Tuesday May 12th: JN25, part one

There is a program I am going to have you build over the next few days which has some interesting features which I must demonstrate to you while you work on your game programs. Here we go....


There is a file called "JN25" which you will need to download from a folder found here:

http://www.box.net/shared/aha47pf4fi



When you compile and run it, you will find a very simple program which actually doesn't have much in it just yet. There are only two simple things we are going to do with this program:


A) add two JTextAreas inside two JScrollPanes which have vertical scrollbars, just like those we put the JEditorPanes inside a few weeks back


B) add a small correction to a slight problem you will no doubt notice almost immediately


For part A, we need to construct two JTextAreas that are next to each other. Here's how the first one gets built:


in the programs constructor, just below where it states that the JFrame's container has a "null" layout, enter in this code block:


clear = new JTextArea("", 20,20);
clear.setBounds(10,10,400,100);
clearScroll = new JScrollPane(clear);
clearScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

clearScroll.setBounds(10,10,400,100);
Ultra.add(clearScroll);



This adds a new JTextArea, which unlike a TextField, has multiple rows as well as many columns: how many has been determined by the 20,20 ints above


The JTextArea is added inside a scrollpane, because if you end with lots of text in it, you might need to scroll up and down to read it all


The ScrollPane is then added to the container, and since that container's layout is set to null, you need to setBounds on the ScrollPane to determine where it gets placed.


Once you have that set up, all you need to do to put the other JTextArea/scrollpane onto the program is to copy and paste the code block from above, paste it right below that first code block in your program, then change the neame of the JTextArea from "clear" to "enc" and the name of the JScrollPane from "clearScroll" to "encScroll".


And of course, you have to change WHERE the second textarea/scroller is placed. Hint: if you set the first int valuein "setBounds" to something bigger than "10", say "450", that should do it



OK, one last thing: when you type in either of these JTextAreas, you will notice that they don't "word wrap" Even though the JScollPane has a vertical scrollbar. !!!!#!@##!~!!!!!!!


fear not though: you can control that by adding these three lines just below where you setbounds for the first JTextArea:

clear.setEditable(true);
clear.setLineWrap(true);
clear.setWrapStyleWord(true);


Make sure to do it for the other JTextArea as well. When you're done, your groups may proceed onwards with your game projects



Cheers,
Mr. L

No comments:

Post a Comment