OK, so yesterday, part of what you did involved working on a small program that I have introduced. Today, before you work further on your game programs, I am going to demonstrate to you some added functionality of this other program, using a component we're already familiar with -- the JComboBox -- and one we haven't worked with yet, the JSlider.
We are going to use these two components to chnage the font style and size of the text typed into the JTextAreas. Here we go!
A) first off, you need to add this code block to your "declarations", I would do it just below the line that runs "JPanel alpha,omega, setWordz;"
//use to fill JComboBox
String[] typeStyle ={"Impact ","Arial", "Comic Sans MS"};
JComboBox daFonts;
String whichFont= typeStyle[0]; //start with first font in Array
JSlider daSizes;
int fontBigness=25; //start at font size 25
//use to provide info about JSlider for font sizes and JComboBox font styles
JLabel whatDaFont, wowSoBig;
As you can see, there are actually FOUR components we will be using eventually, but two of them are JLabels. These JLabels will just have some text in them to describe the function of the component they sit next to
Also, we will use a String to describe which font is going to be used by picking it from a String Array full of font names. Additionally, there is an int to desribe how big the font size is
B) NEXT, you need to add the following to your constructor:
//add in Panel with JSlider and JComboBox here
setWordz = new JPanel();
//new JPanelsetWordz.setBounds(250,380,300,50);
setWordz.setLayout(new GridLayout(2,2));
daSizes = new JSlider(); //new JSlider
daSizes.setValue(fontBigness); //start at default font size
daSizes.setSize(150,25);
setWordz = new JPanel();
//new JPanelsetWordz.setBounds(250,380,300,50);
setWordz.setLayout(new GridLayout(2,2));
daSizes = new JSlider(); //new JSlider
daSizes.setValue(fontBigness); //start at default font size
daSizes.setSize(150,25);
setWordz.add(daSizes); //add slider to panel
wowSoBig = new JLabel("Slider Sets Font Size",SwingConstants.CENTER);
wowSoBig = new JLabel("Slider Sets Font Size",SwingConstants.CENTER);
wowSoBig.setSize(150,25);
setWordz.add(wowSoBig); //add JLabel for slider to panel
As you can see, this sets up a JPanel that has a gridlayout with four sections. Today, you will just be filling two.
The first thing we put in the Panel was a "JSlider". It's a little control that can be slid back and forth. By default, it starts at a value of 50 out of 100, but that can be set to something else. In this case, we set it to the value of an integer that starts at 25
The other thing was a JLabel. This just gives you some info about what the JSlider will do. You will notice that I've shown the way to center the text in the JLabel
C) OK, if you compile and run the program now, you can move the slider thingy around, but it does nothing. Here's what we do to rememdy that:
D) First off, you have to add a new kind of Listener way up at the top of the program, after the part that says "implement ActionListener". make it read like so:
implement ActionListener, ChangeListener
The next obvious step is to add a ChangeListener to your slider, like so:
daSizes.addChangeListener(this);//add changelistener to slider
E) Once you have a ChangeListener on the JSlider, you gotta tell it to DO something once you have its attention. So, just below the (still empty) actionPerformed method block, add in this new method block:
public void stateChanged(ChangeEvent ce)
{
fontBigness = daSizes.getValue();
System.out.println(fontBigness);
ChangeDemWordz();//call method to change the font size & face
}
All this method does is to to change the integer "fontBigness" to the value of the JSlider as you move it around...you can see it change in the system console. BUT, it doesn't actually change the size of the font in the JTextAreas, which is why you call another method, a "homegrown" method called :
public void ChangeDemWordz()
{
{
clear.setFont(new Font(whichFont, 0, fontBigness));
enc.setFont(new Font(whichFont, 0, fontBigness));
}//end method ChangeDemWordz()
OK, so re-compile and run the program. You should be able to change the font size of text in this program at will.
Once you're done with that, proceed to working on your game programs
Cheers,
Mr. L

No comments:
Post a Comment