为什么我的程序在JMenu中单击时不能更改字体的字体或大小? [英] Why does my program not change the font or size of the font when clicked in the JMenu?

查看:206
本文介绍了为什么我的程序在JMenu中单击时不能更改字体的字体或大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试运行该程序时,它运行良好。问题是,当按钮被点击时它不会改变任何东西。我认为问题在于它没有正确地更改字体。它什么都不做,甚至当我试图改变尺寸,但我不知道这是为什么。感谢您的帮助!



这里是我的程序代码:

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

public class JMenuExample5 extends JFrame implements ActionListener {
Container con;
JMenuBar menuBar;
JMenu文件;
JMenu颜色;
JMenu字体;
JMenu对齐;
JMenu themeFont;
JMenu大小;
JMenuItem退出;
ButtonGroup组;
JRadioButtonMenuItem黑色;
JRadioButtonMenuItem绿色;
JRadioButtonMenuItem red;
JRadioButtonMenuItem blue;
JMenuItem arial;
JMenuItem timesNewRoman;
JMenuItem wingdings;
JMenuItem模板;
JMenuItem comicSans;
JMenuItem pt12;
JMenuItem pt18;
JMenuItem pt24;
JMenuItem pt36;
字符串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标签;

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 \\\\\\\\\\\\\\\\\\\\\\\\\\\\')) ;
JCheckBoxMenuItem justified = new JCheckBoxMenuItem(Justified,new ImageIcon(C:\\ Users \\\\\\\\\\\\\\\\\\\\\\\')) ;
JCheckBoxMenuItem left = new JCheckBoxMenuItem(Left,new ImageIcon(C:\\Users\\\\\\\\\\\\\\\\\\')) ;

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(绿色);
group.add(red);
group.add(blue);
colors.add(黑色);
colors.addSeparator();
colors.add(绿色);
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(快速的棕色狐狸跳过懒惰的狗);
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;


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


$ / code $ / pre

解决方案

你的问题是你在这里使用一些神奇的想法,假设一个变量比实际有更多的权力。当你创建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; //这必须是一个浮点数
Font newFont = label.getFont()。deriveFont(newSize);
label.setFont(newFont);所以你可以用正确类型的字体创建你的字体变量,每个字体都使用自己正确的字体名称,然后通过交换正确创建的字体来更改字体,然后使用上面显示的代码更改其大小。


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

解决方案

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.

这篇关于为什么我的程序在JMenu中单击时不能更改字体的字体或大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆