Java ChangeListener不';不要在JText区域中更改字体
我正在使用Java(ver1.42)awt制作聊天程序 我使用粗体和斜体Java ChangeListener不';不要在JText区域中更改字体,java,swing,jtextarea,jcheckbox,changelistener,Java,Swing,Jtextarea,Jcheckbox,Changelistener,我正在使用Java(ver1.42)awt制作聊天程序 我使用粗体和斜体JCheckBox来更改JTextArea中的字体 下面是两个复选框的侦听器 class CheckBoxListener implements ChangeListener { public void stateChanged(ChangeEvent ce) { String fontName = inputTextArea.getFont().getFontName();
JCheckBox
来更改JTextArea
中的字体
下面是两个复选框的侦听器
class CheckBoxListener implements ChangeListener {
public void stateChanged(ChangeEvent ce) {
String fontName = inputTextArea.getFont().getFontName();
int fontSize = inputTextArea.getFont().getSize();
int fontStyle = 0;
if(boldCheckBox.isSelected())
fontStyle += Font.BOLD;
if(italicCheckBox.isSelected())
fontStyle += Font.ITALIC;
inputTextArea.setFont(new Font(fontName, fontStyle, fontSize));
}
}
}
一切正常
如果我选中“boldCheckBox”,则InputExtArea
中的字体将变为粗体。如果我选中“italicCheckBox”,则
InputExtArea
中的字体将变为斜体。和
如果我取消选中“italicCheckBox”,字体将变为正常形式 然而 字体永远不会回来,即使我取消选中“boldCheckBox”
你能找出哪里不对劲吗?首先,你必须使用按位的
操作符将粗体和斜体放在同一字体中,而不是+
操作符
也可能是系统在切换到粗体字体后,正在使用包含粗体属性的相关字体。例如,在某些操作系统中,有“Arial”和“ArialBD”。由于您是根据旧字体的名称创建新字体,而不是使用deriveFont
,因此它可能保持为“ArialBD”
所以试试这个:
class CheckBoxListener implements ChangeListener {
public void stateChanged(ChangeEvent ce) {
int fontStyle = Font.PLAIN;
if(boldCheckBox.isSelected())
fontStyle |= Font.BOLD;
if(italicCheckBox.isSelected())
fontStyle |= Font.ITALIC;
inputTextArea.setFont(inputTextArea.getFont().deriveFont(fontStyle));
}
}
}
首先,确实必须使用按位的
|
运算符,才能在同一字体中同时使用粗体和斜体,而不是+
运算符
也可能是系统在切换到粗体字体后,正在使用包含粗体属性的相关字体。例如,在某些操作系统中,有“Arial”和“ArialBD”。由于您是根据旧字体的名称创建新字体,而不是使用deriveFont
,因此它可能保持为“ArialBD”
所以试试这个:
class CheckBoxListener implements ChangeListener {
public void stateChanged(ChangeEvent ce) {
int fontStyle = Font.PLAIN;
if(boldCheckBox.isSelected())
fontStyle |= Font.BOLD;
if(italicCheckBox.isSelected())
fontStyle |= Font.ITALIC;
inputTextArea.setFont(inputTextArea.getFont().deriveFont(fontStyle));
}
}
}
首先,确实必须使用按位的
|
运算符,才能在同一字体中同时使用粗体和斜体,而不是+
运算符
也可能是系统在切换到粗体字体后,正在使用包含粗体属性的相关字体。例如,在某些操作系统中,有“Arial”和“ArialBD”。由于您是根据旧字体的名称创建新字体,而不是使用deriveFont
,因此它可能保持为“ArialBD”
所以试试这个:
class CheckBoxListener implements ChangeListener {
public void stateChanged(ChangeEvent ce) {
int fontStyle = Font.PLAIN;
if(boldCheckBox.isSelected())
fontStyle |= Font.BOLD;
if(italicCheckBox.isSelected())
fontStyle |= Font.ITALIC;
inputTextArea.setFont(inputTextArea.getFont().deriveFont(fontStyle));
}
}
}
首先,确实必须使用按位的
|
运算符,才能在同一字体中同时使用粗体和斜体,而不是+
运算符
也可能是系统在切换到粗体字体后,正在使用包含粗体属性的相关字体。例如,在某些操作系统中,有“Arial”和“ArialBD”。由于您是根据旧字体的名称创建新字体,而不是使用deriveFont
,因此它可能保持为“ArialBD”
所以试试这个:
class CheckBoxListener implements ChangeListener {
public void stateChanged(ChangeEvent ce) {
int fontStyle = Font.PLAIN;
if(boldCheckBox.isSelected())
fontStyle |= Font.BOLD;
if(italicCheckBox.isSelected())
fontStyle |= Font.ITALIC;
inputTextArea.setFont(inputTextArea.getFont().deriveFont(fontStyle));
}
}
}
问题在于每次在侦听器中初始化
fontName
。只需将fontName
初始化代码移到侦听器外部一次
请尝试以下代码:
final String fontName = inputTextArea.getFont().getFontName();
final int fontSize = inputTextArea.getFont().getSize();
class CheckBoxListener implements ChangeListener {
public void stateChanged(ChangeEvent ce) {
int fontStyle = 0;
if (boldCheckBox.isSelected() && italicCheckBox.isSelected())
fontStyle = Font.BOLD | Font.ITALIC;
else if (boldCheckBox.isSelected())
fontStyle = Font.BOLD;
else if (italicCheckBox.isSelected())
fontStyle = Font.ITALIC;
else
fontStyle = Font.PLAIN;
inputTextArea.setFont(new Font(fontName, fontStyle, fontSize));
}
}
问题在于每次在侦听器中初始化
fontName
。只需将fontName
初始化代码移到侦听器外部一次
请尝试以下代码:
final String fontName = inputTextArea.getFont().getFontName();
final int fontSize = inputTextArea.getFont().getSize();
class CheckBoxListener implements ChangeListener {
public void stateChanged(ChangeEvent ce) {
int fontStyle = 0;
if (boldCheckBox.isSelected() && italicCheckBox.isSelected())
fontStyle = Font.BOLD | Font.ITALIC;
else if (boldCheckBox.isSelected())
fontStyle = Font.BOLD;
else if (italicCheckBox.isSelected())
fontStyle = Font.ITALIC;
else
fontStyle = Font.PLAIN;
inputTextArea.setFont(new Font(fontName, fontStyle, fontSize));
}
}
问题在于每次在侦听器中初始化
fontName
。只需将fontName
初始化代码移到侦听器外部一次
请尝试以下代码:
final String fontName = inputTextArea.getFont().getFontName();
final int fontSize = inputTextArea.getFont().getSize();
class CheckBoxListener implements ChangeListener {
public void stateChanged(ChangeEvent ce) {
int fontStyle = 0;
if (boldCheckBox.isSelected() && italicCheckBox.isSelected())
fontStyle = Font.BOLD | Font.ITALIC;
else if (boldCheckBox.isSelected())
fontStyle = Font.BOLD;
else if (italicCheckBox.isSelected())
fontStyle = Font.ITALIC;
else
fontStyle = Font.PLAIN;
inputTextArea.setFont(new Font(fontName, fontStyle, fontSize));
}
}
问题在于每次在侦听器中初始化
fontName
。只需将fontName
初始化代码移到侦听器外部一次
请尝试以下代码:
final String fontName = inputTextArea.getFont().getFontName();
final int fontSize = inputTextArea.getFont().getSize();
class CheckBoxListener implements ChangeListener {
public void stateChanged(ChangeEvent ce) {
int fontStyle = 0;
if (boldCheckBox.isSelected() && italicCheckBox.isSelected())
fontStyle = Font.BOLD | Font.ITALIC;
else if (boldCheckBox.isSelected())
fontStyle = Font.BOLD;
else if (italicCheckBox.isSelected())
fontStyle = Font.ITALIC;
else
fontStyle = Font.PLAIN;
inputTextArea.setFont(new Font(fontName, fontStyle, fontSize));
}
}
谢谢你们,但我找到了答案 原因是他的名字 如果我把它变成大胆的风格 FontName从“dialog”更改为“dialog.bold” 所以,即使我删除了粗体样式,字体仍然是粗体样式,因为FontName是“dialog.Bold” 这是我的答案
class CheckBoxListener implements ChangeListener {
public void stateChanged(ChangeEvent ce) {
int fontSize = inputTextArea.getFont().getSize();
int fontStyle = 0;
if(boldCheckBox.isSelected())
fontStyle += Font.BOLD;
if(italicCheckBox.isSelected())
fontStyle += Font.ITALIC;
inputTextArea.setFont(new Font("dialog", fontStyle, fontSize));
}
}
谢谢你们,但我找到了答案 原因是他的名字 如果我把它变成大胆的风格 FontName从“dialog”更改为“dialog.bold” 所以,即使我删除了粗体样式,字体仍然是粗体样式,因为FontName是“dialog.Bold” 这是我的答案
class CheckBoxListener implements ChangeListener {
public void stateChanged(ChangeEvent ce) {
int fontSize = inputTextArea.getFont().getSize();
int fontStyle = 0;
if(boldCheckBox.isSelected())
fontStyle += Font.BOLD;
if(italicCheckBox.isSelected())
fontStyle += Font.ITALIC;
inputTextArea.setFont(new Font("dialog", fontStyle, fontSize));
}
}
谢谢你们,但我找到了答案 原因是他的名字 如果我把它变成大胆的风格 FontName从“dialog”更改为“dialog.bold” 所以,即使我删除了粗体样式,字体仍然是粗体样式,因为FontName是“dialog.Bold” 这是我的答案
class CheckBoxListener implements ChangeListener {
public void stateChanged(ChangeEvent ce) {
int fontSize = inputTextArea.getFont().getSize();
int fontStyle = 0;
if(boldCheckBox.isSelected())
fontStyle += Font.BOLD;
if(italicCheckBox.isSelected())
fontStyle += Font.ITALIC;
inputTextArea.setFont(new Font("dialog", fontStyle, fontSize));
}
}
谢谢你们,但我找到了答案 原因是他的名字 如果我把它变成大胆的风格 FontName从“dialog”更改为“dialog.bold” 所以,即使我删除了粗体样式,字体仍然是粗体样式,因为FontName是“dialog.Bold” 这是我的答案
class CheckBoxListener implements ChangeListener {
public void stateChanged(ChangeEvent ce) {
int fontSize = inputTextArea.getFont().getSize();
int fontStyle = 0;
if(boldCheckBox.isSelected())
fontStyle += Font.BOLD;
if(italicCheckBox.isSelected())
fontStyle += Font.ITALIC;
inputTextArea.setFont(new Font("dialog", fontStyle, fontSize));
}
}
考虑提供一个说明你的问题的方法。这将减少混乱和更好的响应。你也应该考虑使用<代码>字体>派生字体< /> >,但那只是我……从内存中,我认为它应该是代码> ftTythys=字体。Bruts<代码>,而不是<代码> += …<代码> +/COD>对我来说也很好。“我用java”嗯…“(1.42节)“等等,……什么?这是2004年吗?我认为这是十年后,考虑提供一个演示你的问题。这将减少混乱和更好的响应。你也应该考虑使用<代码>字体>派生字体< /> >,但那只是我……从内存中,我认为它应该是代码> ftTythys=字体。Bruts<代码>,而不是<代码> += …<代码> +/COD>对我来说也很好。“我用java”嗯…“(1.42节)“等等,……什么?这是2004年吗?我认为这是十年后,考虑提供一个演示你的问题。这将减少混乱和更好的响应。你也应该考虑使用<代码>字体>派生字体< /> >,但那只是我……从内存中,我认为它应该<代码> fOntType =字体。