Author Topic: setFont in java not working  (Read 5214 times)

0 Members and 1 Guest are viewing this topic.

Offline Snake X

  • Ancient Veteran
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 810
  • Rating: +33/-8
    • View Profile
setFont in java not working
« on: May 22, 2011, 03:14:22 pm »
This is my final project in my class. However, I wanted to create a JLabel and apply a bold font and make it 42 pt and make it red. so:

Code: [Select]
JLabel win = new JLabel("YOU WIN!");
win.setFont(new Font("Serif", Font.BOLD, 42));
win.setForeground(Color.red);
however this does not seem to work for unknown reasons. I say unknown because these are the errors:

Code: [Select]

--------------------Configuration: <Default>--------------------
E:\adv. comp sci\slot machine\slotmachine.java:21: <identifier> expected
win.setFont(new Font("Serif", Font.BOLD, 42));
          ^
E:\adv. comp sci\slot machine\slotmachine.java:21: illegal start of type
win.setFont(new Font("Serif", Font.BOLD, 42));
           ^
E:\adv. comp sci\slot machine\slotmachine.java:21: ')' expected
win.setFont(new Font("Serif", Font.BOLD, 42));
              ^
E:\adv. comp sci\slot machine\slotmachine.java:21: ';' expected
win.setFont(new Font("Serif", Font.BOLD, 42));
                   ^
E:\adv. comp sci\slot machine\slotmachine.java:21: illegal start of type
win.setFont(new Font("Serif", Font.BOLD, 42));
                    ^
E:\adv. comp sci\slot machine\slotmachine.java:21: <identifier> expected
win.setFont(new Font("Serif", Font.BOLD, 42));
                           ^
E:\adv. comp sci\slot machine\slotmachine.java:21: ';' expected
win.setFont(new Font("Serif", Font.BOLD, 42));
                            ^
E:\adv. comp sci\slot machine\slotmachine.java:21: illegal start of type
win.setFont(new Font("Serif", Font.BOLD, 42));
                                 ^
E:\adv. comp sci\slot machine\slotmachine.java:21: <identifier> expected
win.setFont(new Font("Serif", Font.BOLD, 42));
                                       ^
E:\adv. comp sci\slot machine\slotmachine.java:21: illegal start of type
win.setFont(new Font("Serif", Font.BOLD, 42));
                                          ^
E:\adv. comp sci\slot machine\slotmachine.java:21: <identifier> expected
win.setFont(new Font("Serif", Font.BOLD, 42));
                                           ^
E:\adv. comp sci\slot machine\slotmachine.java:22: <identifier> expected
win.setForeground(Color.red);
                ^
E:\adv. comp sci\slot machine\slotmachine.java:22: <identifier> expected
win.setForeground(Color.red);
                          ^
13 errors

Process completed.

if you want to see the full applet then here it is:

Code: [Select]
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
import java.util.*;


public class slotmachine extends JApplet implements ActionListener {

Random r = new Random();
JButton spin = new JButton("Spin!");
JLabel slot11,slot12,slot13,slot21,slot22,slot23,slot31,slot32,slot33;
JLabel win = new JLabel("YOU WIN!");
win.setFont(new Font("Serif", Font.BOLD, 42));
win.setForeground(Color.red);
JLabel lose = new JLabel("YOU LOSE!");
Container contentArea;
Graphics g;

     public void init() {
contentArea = getContentPane();
contentArea.setBackground(Color.cyan);
FlowLayout manager = new FlowLayout();
contentArea.setLayout(manager);
spin.addActionListener(this);
spin.setEnabled(true);
contentArea.add(spin);
setContentPane(contentArea);
    }
public void actionPerformed(ActionEvent event) {
Image limon = getImage(getCodeBase(),"limon.jpg");
ImageIcon pica = new ImageIcon(limon);

Image grape = getImage(getCodeBase(),"grape.jpg");
ImageIcon picb = new ImageIcon(grape);

Image johnnydepp = getImage(getCodeBase(),"johnnydepp.jpg");
ImageIcon picc = new ImageIcon(johnnydepp);

if(event.getSource()==spin) {
int randomnumber1 = r.nextInt(3)+1;
int randomnumber2 = r.nextInt(3)+1;
int randomnumber3 = r.nextInt(3)+1;

slot11 = new JLabel(pica);
slot12 = new JLabel(picb);
slot13 = new JLabel(picc);
slot21 = new JLabel(pica);
slot22 = new JLabel(picb);
slot23 = new JLabel(picc);
slot31 = new JLabel(pica);
slot32 = new JLabel(picb);
slot33 = new JLabel(picc);
contentArea.removeAll();
setContentPane(contentArea);

if (randomnumber1 == 1) {

contentArea.add(slot11);
contentArea.add(spin);

setContentPane(contentArea);
}

if (randomnumber1 == 2) {

contentArea.add(slot12);
contentArea.add(spin);

setContentPane(contentArea);
}
if (randomnumber1 == 3) {

contentArea.add(slot13);
contentArea.add(spin);

setContentPane(contentArea);
}

if (randomnumber2 == 1) {

contentArea.add(slot21);
contentArea.add(spin);

setContentPane(contentArea);
}
if (randomnumber2 == 2) {

contentArea.add(slot22);
contentArea.add(spin);

setContentPane(contentArea);
}
if (randomnumber2 == 3) {

contentArea.add(slot23);
contentArea.add(spin);

setContentPane(contentArea);
}

if (randomnumber3 == 1) {

contentArea.add(slot31);
contentArea.add(spin);

setContentPane(contentArea);
}
if (randomnumber3 == 2) {

contentArea.add(slot32);
contentArea.add(spin);

setContentPane(contentArea);
}
if (randomnumber3 == 3) {

contentArea.add(slot33);
contentArea.add(spin);

setContentPane(contentArea);
}
if ((randomnumber1 == 1 && randomnumber2 == 1 && randomnumber3 == 1) ||
(randomnumber1 == 2 && randomnumber2 == 2 && randomnumber3 == 2) ||
(randomnumber1 == 3 && randomnumber2 == 3 && randomnumber3 == 3)) {
contentArea.add(win);
setContentPane(contentArea);
}
else {
contentArea.add(lose);
setContentPane(contentArea);
}
}
}
}
EDIT:
It works when I take win.setFont(new Font("Serif", Font.BOLD, 42)); and win.setForeground(Color.red); and place it before the line contentArea.add(win);

This is strange.. How come it works there but not up near the top?  ???
« Last Edit: May 22, 2011, 03:17:52 pm by Snake X »
Loved this place, still the best producers of power metal, and sparked my dreams of coding.

Offline ZippyDee

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 729
  • Rating: +83/-8
  • Why not zoidberg?
    • View Profile
Re: setFont in java not working
« Reply #1 on: May 22, 2011, 03:36:36 pm »
Ah, there's the issue...Outside of methods you can only DEFINE and INITIALIZE variables, but you can't manipulate them. so you can define JLabel win, but you can't manipulate it with setFont, because it's not in a method, it's only in the class body.
There's something about Tuesday...


Pushpins 'n' stuff...