Frage

When I try to run the program, it runs fine. The problem is that it does not change anything when the button is clicked. I think the problem is that it is not properly changing the Fonts. It does nothing even when I try to change the sizes but I do not know why that is. Thank you for helping!

Here is the code to my program:

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); 
}
}
War es hilfreich?

Lösung

Part of your problem is that you're using some magical thinking here, assuming that a variable has more power than it actually has. When you create your Comic Sans font like so:

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);
  }

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

Solution: don't do this. Either create your new font with the correct Font name from the get go, or create the new Font when needed.

For the size, this can be done via get font and derive font:

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

So you could create your Font variables with Fonts of the correct type, each using its own correct Font name, and then change the font by swapping in the properly created font, and then changing its size using the code shown above.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top