0

I'm trying to make a simple GUI with radio buttons that has a list of fonts and when a button is selected is displays text with the font name and in the font style.

When I try to compile I get several errors about "cannot find symbol ... Font.PLAIN" Is there something wrong with the way I'm using the fonts?

Also in the FontListener Class, I need to pass in the button group, should i pass in the button group though a parameter in the constructor?

Lastly, does it seem like I'm using the isSelected(), getSelection().getActionCommand methods correctly? FontFrame Class

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

public class FontFrame extends JFrame {

    Font HelveticaFont = new Font("Helvetica", Font.PLAIN, 14);  
    Font TimesRomanFont = new Font("TimesRoman",Font.PLAIN, 14); 
    Font CourierFont = new Font("Courier", Font.PLAIN,14); 


    public FontFrame(String title){
        super(title);
    }

    public void init(){

    Container panel = this.getContentPane();
    panel.setLayout(new FlowLayout());
    JRadioButton Helvetica = new JRadioButton("Helvetica", true);
    JRadioButton TimesRoman = new JRadioButton("Times Roman", false);
    JRadioButton Courier = new JRadioButton("Courier", false);
    JTextField tf = new JTextField("Select One");
    panel.add(Helvetica);
    panel.add(TimesRoman);
    panel.add(Courier);
    panel.add(tf);

    ButtonGroup group = new ButtonGroup();
    group.add(Helvetica);
    group.add(TimesRoman);
    group.add(Courier);


    Helvetica.addItemListener(new FontListener(HelveticaFont, tf));
    TimesRoman.addItemListener(new FontListener(TimesRomanFont, tf));
    Courier.addItemListener(new FontListener(CourierFont, tf));

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
    this.setSize(300,200);
    this.setVisible(true);
    }
}

FontListener Class

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

public class FontListener implements ItemListener {

    Font font;
    JTextField tf;

     public FontListener(Font f, JTextField tf){
        this.font = f;
        this.tf = tf;
     }

     public void itemStateChanged(ItemEvent event){
         if(Helvetica.isSelected()) {
            tf.setFont(font);
            tf.setText(group.getActionCommand());
         }
         else if(TimesRoman.isSelected()) {
            tf.setFont(font);
            tf.setText(group.getActionCommand());
        }   
        else if(Courier.isSelected()) {
            tf.setFont(font);
            tf.setText(group.getActionCommand());
        }
     }
 }

Main Class

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

public class Font{

    public static void main(String[] args){

        FontFrame window = new FontFrame("Fonts");
        window.init();
    }
}
Edgar Smith
  • 163
  • 1
  • 10
  • Follow the Java Naming Conventions: variable names are always written in camelCase. – MC Emperor Feb 18 '19 at 16:09
  • 3
    You __really__ shouldn't call your own class `Font`, because `FontListener` will use that instead of `java.awt.Font`. It isn't even a font, so it also doesn't make to name it that way. – Tom Feb 18 '19 at 16:16
  • 1
    Possible duplicate of [Order of automatically imported packages and ambiguity](https://stackoverflow.com/questions/21477783/order-of-automatically-imported-packages-and-ambiguity) – Tom Feb 18 '19 at 16:19
  • Also read: https://stackoverflow.com/questions/147454/why-is-using-a-wild-card-with-a-java-import-statement-bad – Tom Feb 18 '19 at 16:19
  • @Tom Wouldn't the `import java.awt.Font;` explicit import solve the problem then? I'm guessing it does and OP is running old code due to other compile errors but I'd rather make sure before casting a close vote... – Aaron Feb 18 '19 at 16:26
  • @Aaron When OP explicitly imports that class, then yes it would work, like you can see in `FontFrame`, where `Font.PLAIN` is correctly recognised. – Tom Feb 18 '19 at 16:30
  • OP also has issues on lines like `if(Helvetica.isSelected())`, but this issue here is a bad variable scope. – Tom Feb 18 '19 at 16:33
  • @Tom the only references to `Font.PLAIN` are found in the `FontFrame` class though, so I'm guessing the `if(Helvetica.isSelected())` errors prevented him from compiling and he's running a version of his code where he didn't have the explicit `import java.awt.Font;` in the `FontFrame` class yet, which would explain the `cannot find symbol ... Font.PLAIN` error. Unless I'm missing something? Which piece of code do you think raises that error? – Aaron Feb 18 '19 at 16:52
  • Why would explicit importing make a difference? I didn't think about that because I thought awt.* would be the same as .font, what's the difference? – Edgar Smith Feb 18 '19 at 17:00
  • 1
    @Aaron I don't know what version OP actually tries to compile or if the error message he wrote there is really the one their code produces, because in the current form: it doesn't. The issues are coming from `FontListener` and do spread to `FontFrame` but only where that tries to call `FontListener` (because `FontFrame` passes `java.awt.Font`, but `FontListener` expects `Font`). – Tom Feb 18 '19 at 17:00
  • 1
    Edgar, please read the answer in the first link, it explains why it makes a difference. – Tom Feb 18 '19 at 17:01

0 Answers0