为什么我的程序在JMenu中点击时不会改变字体的字体或大小?

时间:2014-04-25 15:50:30

标签: java swing fonts jlabel jmenu

当我尝试运行该程序时,它运行正常。问题是单击按钮时它不会改变任何内容。我认为问题是它没有正确改变字体。即使我试图改变尺寸,它也什么也没做,但我不知道为什么会这样。感谢您的帮助!

以下是我的程序的代码:

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 个答案:

答案 0 :(得分:3)

你的部分问题是你在这里使用了一些神奇的想法,假设一个变量的功率大于它实际拥有的功率。当您创建Comic Sans字体时,如下所示:

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

字体已完成;它被创造了。

稍后如果你改变fTheme,就像这样:

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

这对字体,comicSans,根本没有效果没有影响,因为它已经被创建,其状态是固定的,并且更改了最初用于设置字体的变量将对已创建的字体对象没有影响。您的fsize变量存在同样的问题。

解决方案:不要这样做。从get go创建具有正确Font名称的新字体,或者在需要时创建新Font。

对于大小,这可以通过get font和derive font来完成:

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

因此,您可以使用正确类型的字体创建Font变量,每个字体使用自己正确的字体名称,然后通过交换正确创建的字体来更改字体,然后使用上面显示的代码更改其大小。 / p>