【问题标题】:Why does my program not change the font or size of the font when clicked in the JMenu?为什么我的程序在 JMenu 中单击时不会更改字体或字体大小?
【发布时间】:2014-06-11 10:32:04
【问题描述】:

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

这是我的程序的代码:

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

【问题讨论】:

    标签: java swing fonts jlabel jmenu


    【解决方案1】:

    您的部分问题是您在这里使用了一些神奇的想法,假设一个变量比它实际拥有的权力更大。当您像这样创建 Comic Sans 字体时:

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

    字体完成;它被创建。

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

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

    这对字体,comicSans,完全没有没有影响,因为它已经被创建,并且它的状态是固定的,并且改变了最初用于设置字体的变量对已创建的字体对象没有影响。你的 fsize 变量也有同样的问题。

    解决方案:不要这样做。从一开始就使用正确的字体名称创建新字体,或者在需要时创建新字体。

    对于大小,这可以通过获取字体和派生字体来完成:

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

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

    【讨论】:

    • 那么我该如何解决这个问题呢?
    • @DelleVelleD:同样,通过在需要更改大小时创建新字体。可以做到这一点的一种方法是获取字体并派生字体。例如,请参阅编辑以回答。
    • @DelleVelleD:我无法强调使用float 来表示字体大小而不是int 的重要性。
    • 谢谢,我希望这对以后的其他人有所帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-26
    • 1970-01-01
    • 2021-12-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多