0

I have to change the size and font of the string on every mouse click in java AWT.

Firstly I'm getting all the available fonts in the system into an array but it is not working. What is the reason behind that, and where am I wrong?

In below code the font size increases with every mouse click but font does not changes!

import java.awt.*;
import java.awt.event.*;
import java.awt.Font;
class JavaFont extends Frame implements MouseListener {
    int size=20;
    Label l1;
    Font  font;
    int i=0;
    int j=0;
    String fonts[] = 
 GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();

    JavaFont()
    {
        System.out.println(fonts.length);
        setSize(500,500);
        setVisible(true);
        font=new Font(fonts[i],Font.BOLD,size);
        l1=new Label("ABC",Label.CENTER);
        l1.setFont(font);
        add(l1,BorderLayout.NORTH);
        addMouseListener(this);
    }
    public void mousePressed(MouseEvent me)
    {

    }
    public void mouseClicked(MouseEvent me)
    {
        if(i<fonts.length)
        {
        i++;
        size=size+2;
        l1.setText("ABC12");
        font=new Font(fonts[i],Font.BOLD,size);
        l1.setFont(font);
        }
        else
        {
            i=0;
        }

    }
    public void mouseEntered(MouseEvent me)
    {

    }
    public void mouseExited(MouseEvent me)
    {

    }
    public void mouseReleased(MouseEvent me)
    {

    }
    public static void main(String args[])
    {
        new JavaFont();
    }
}

But when I use font in this way it works

font=new Font("TimesRoman",Font.BOLD,size);

It works for only "TimesRoman" so why all systemfonts are not working.

halfer
  • 19,824
  • 17
  • 99
  • 186
Rohit Suthar
  • 967
  • 9
  • 22
  • It works if the label is a (Swing based) `JLabel`. Which doesn't directly answer your question, but raises a new one.. Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT components in favor of Swing. – Andrew Thompson Apr 17 '18 at 15:48
  • Ya i know AWT is not used but it is a program in my college practicals – Rohit Suthar Apr 17 '18 at 17:00
  • Because of this i use AWT – Rohit Suthar Apr 17 '18 at 17:01
  • *"my college"* Well that's tough. My best advice if they won't update from a technology that was superseded over 16 years ago is to change colleges. Most of the people who might be able to help on this could not be bothered even looking into it. – Andrew Thompson Apr 18 '18 at 02:12
  • @AndrewThompson Dear, most platforms still using AWT and old java 1.3 version. We are currently using it for a big client. we need answer not excuse. – Akhilesh Dhar Dubey Jul 02 '19 at 12:32
  • @AkhileshDharDubey *"We are currently using it for a big client."* My condolences. – Andrew Thompson Jul 02 '19 at 12:43

1 Answers1

1

Your question tagged with Swing, so use Swing components.

Label and Frame are AWT components.

For Swing you should be using JLabel and JFrame and the Font will change as expected.

It works for only "TimesRoman" so why all systemfonts are not working pls help!

I guess AWT only supports certain fonts. Keep clicking. I noticed it works for "monospaced" as well.

camickr
  • 321,443
  • 19
  • 166
  • 288