Why doesn't my program change font or font size when I click on JMenu?

When I try to run the program, it works fine. The problem is that nothing changes when the button is pressed. I think the problem is that it changes fonts incorrectly. It doesn't do anything even when I try to resize, but I don't know why it is. Thanks for the help!

Here is my program code:

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

public class JMenuExample5 extends JFrame implements ActionListener{
Container con;
JMenuBar menuBar;
JMenu file;
JMenu colors;
JMenu font;
JMenu alignment;
JMenu themeFont;
JMenu size;
JMenuItem exit;
ButtonGroup group;
JRadioButtonMenuItem black;
JRadioButtonMenuItem green;
JRadioButtonMenuItem red;
JRadioButtonMenuItem blue;
JMenuItem arial;
JMenuItem timesNewRoman;
JMenuItem wingdings;
JMenuItem stencil;
JMenuItem comicSans;
JMenuItem pt12;
JMenuItem pt18;
JMenuItem pt24;
JMenuItem pt36;
String fTheme = "";
int fsize = 12;
Font fontArial = new Font(fTheme, Font.BOLD, fsize);
Font fontTimes = new Font(fTheme, Font.BOLD, fsize);
Font fontWingdings = new Font(fTheme, Font.BOLD, fsize);
Font fontStencil = new Font(fTheme, Font.BOLD, fsize);
Font fontComic= new Font(fTheme, Font. BOLD, fsize);
JLabel label;

public JMenuExample5(){
  super("JMenu Demonstration");
  setSize(901, 201); 
  con = getContentPane();
  con.setLayout(new FlowLayout(FlowLayout.CENTER, 40, 40)); 
  setVisible(true); 
  con.setBackground(Color.LIGHT_GRAY);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 


  menuBar = new JMenuBar();
  group = new ButtonGroup();
  file = new JMenu("File");
  colors = new JMenu("Colors");
  font = new JMenu("Font");
  alignment = new JMenu("Alignment");
  themeFont = new JMenu("Font Theme");
  size = new JMenu("Size");
  exit = new JMenuItem("Exit");
  black = new JRadioButtonMenuItem("Black");
  green = new JRadioButtonMenuItem("Green");
  red = new JRadioButtonMenuItem("Red");
  blue = new JRadioButtonMenuItem("Blue");
  arial = new JMenuItem("Arial");
  timesNewRoman = new JMenuItem("Times New Roman");
  wingdings = new JMenuItem("Wing Dings");
  stencil = new JMenuItem("Stencil");
  comicSans = new JMenuItem("Comic Sans");
  pt12 = new JMenuItem("12pt");
  pt18 = new JMenuItem("18pt");
  pt24 = new JMenuItem("24pt");
  pt36 = new JMenuItem("36pt");
  JCheckBoxMenuItem right = new JCheckBoxMenuItem("Right", new ImageIcon("C:\\Users\\delle_000\\Desktop\\AlignmentImages\\right.png"));
  JCheckBoxMenuItem center = new JCheckBoxMenuItem("Center", new ImageIcon("C:\\Users\\delle_000\\Desktop\\AlignmentImages\\center.jpeg"));
  JCheckBoxMenuItem justified = new JCheckBoxMenuItem("Justified", new ImageIcon("C:\\Users\\delle_000\\Desktop\\AlignmentImages\\justified.jpeg"));
  JCheckBoxMenuItem left = new JCheckBoxMenuItem("Left", new ImageIcon("C:\\Users\\delle_000\\Desktop\\AlignmentImages\\left.jpeg"));

  file.setMnemonic('F');
  exit.setMnemonic('E');
  colors.setMnemonic('C');
  black.setMnemonic('V');
  green.setMnemonic('B');
  red.setMnemonic('N');
  blue.setMnemonic('M');
  font.setMnemonic(',');
  themeFont.setMnemonic('T');
  arial.setMnemonic('G');
  timesNewRoman.setMnemonic('H');
  wingdings.setMnemonic('J');
  stencil.setMnemonic('K');
  comicSans.setMnemonic('L');
  size.setMnemonic('S');
  pt12.setMnemonic('U');
  pt18.setMnemonic('I');
  pt24.setMnemonic('O');
  pt36.setMnemonic('P');
  alignment.setMnemonic('A');

  setJMenuBar(menuBar);
  menuBar.add(file);
  menuBar.add(colors);
  menuBar.add(font);
  menuBar.add(alignment);
  font.add(themeFont);
  font.add(size);
  themeFont.add(arial);
  themeFont.add(timesNewRoman);
  themeFont.add(wingdings);
  themeFont.add(stencil);
  themeFont.add(comicSans);
  size.add(pt12);
  size.add(pt18);
  size.add(pt24);
  size.add(pt36);
  group.add(black);
  group.add(green);
  group.add(red);
  group.add(blue);
  colors.add(black);
  colors.addSeparator();
  colors.add(green);
  colors.addSeparator();
  colors.add(red);
  colors.addSeparator();
  colors.add(blue);
  file.add(exit);
  file.addSeparator();
  font.insertSeparator(1);
  alignment.add(right);
  alignment.addSeparator();
  alignment.add(center);
  alignment.addSeparator();
  alignment.add(justified);
  alignment.addSeparator();
  alignment.add(left);
  pt12.addActionListener(this);
  pt18.addActionListener(this);
  pt24.addActionListener(this);
  pt36.addActionListener(this);
  arial.addActionListener(this);
  timesNewRoman.addActionListener(this);
  wingdings.addActionListener(this);
  stencil.addActionListener(this);
  comicSans.addActionListener(this);
  black.addActionListener(this);
  green.addActionListener(this);
  red.addActionListener(this);
  blue.addActionListener(this);
  label = new JLabel("The quick brown fox jumps over the lazy dog.");
  con.add(label);
}

public void actionPerformed(ActionEvent e){
  Object source = e.getSource();
  if(source == black){
    label.setForeground(Color.BLACK);
  } else if(source == green){
    label.setForeground(Color.GREEN);
  } else if(source == red){
    label.setForeground(Color.RED);
  } else if(source == blue){
    label.setForeground(Color.BLUE);
  }

  if(source == arial){
    fTheme = "Arial";
    label.setFont(fontArial);
  } else if(source == timesNewRoman){
    fTheme = "Times New Roman";
    label.setFont(fontTimes);
  } else if(source == wingdings){
    fTheme = "Wing Dings";
    label.setFont(fontWingdings);
  } else if(source == stencil){
    fTheme = "Stencil";
    label.setFont(fontStencil);
  } else if(source == comicSans){
    fTheme = "Comic Sans MS";
    label.setFont(fontComic);
  }

  if(source == pt12){
    fsize = 12;
  } else if(source == pt18){
    fsize = 18;
  } else if(source == pt24){
    fsize = 24;
  } else if(source == pt36){
    fsize = 36;
  }
}

public static void main(String[]args){
  JMenuExample5 myFrame = new JMenuExample5(); 
  myFrame.setSize(900, 200); 
}
}

      

+1


source to share


1 answer


Part of your problem is that you are using magical thinking here, assuming that the variable has more cardinality than it actually does. When you create a Comic Sans font like this:

Font fontComic = new Font(fTheme, Font.BOLD, fsize);

      

The font is done; it is created.

Later, if you change fTheme like so:

  } else if (source == comicSans) {
     fTheme = "Comic Sans MS";
     label.setFont(fontComic);
  }

      



It will have no effect on the font, comicSans, none at all, since it has already been created, and its state has been fixed, and changing the variable that was originally used to set the font will not affect the already created font object. You have the same problem with your fsize variable.

Solution: don't do this. Either create your new font with the correct font name, or create a new font if needed.

For size, this can be done with font get and font output:

float newSize = 36f; // this has to be a float
Font newFont = label.getFont().deriveFont(newSize);
label.setFont(newFont);

      

This way, you can create your font variables with fonts of the correct type, each using its own correct font name, and then changing the font by substitution in the correctly generated font and then resizing it using the code shown above.

+3


source







All Articles