📄 fontsetting.java
字号:
{
setScrollValueBaseGreenText();
}
private void setScrollValueBaseGreenText() throws NumberFormatException
{
//如果没有输入,则设置为0
try
{
//取得输入的值
int g = Integer.parseInt(gValueText.getText().trim());
//如果设置大于255,则将其设置为255
if (g > 255)
{
gValue.setValue(255);
}
//如果没有输入任何数或是小于0的数,则设置为0
else if (g <= 0)
{
gValue.setValue(0);
}
else
{
gValue.setValue(Integer.parseInt(gValueText.getText()));
}
}
catch (Exception ev)
{
//如果输入的数格式不对,则做任何处理
}
}
});
gValueText.setText("0");
bValueText.setBounds(new Rectangle(264, 94, 49, 23));
bValueText.addKeyListener(new KeyAdapter()
{
public void keyReleased(KeyEvent e)
{
//如果没有输入,则设置为0
setScrollValueBaseBlueText();
}
private void setScrollValueBaseBlueText() throws NumberFormatException
{
try
{
//取得输入的值
int b = Integer.parseInt(bValueText.getText().trim());
//如果设置大于255,则将其设置为255
if (b > 255)
{
bValue.setValue(255);
}
//如果没有输入任何数或是小于0的数,则设置为0
else if (b <= 0)
{
bValue.setValue(0);
}
else
{
bValue.setValue(Integer.parseInt(bValueText.getText()));
}
}
catch (Exception ev)
{
//如果输入的数格式不对,则做任何处理
}
}
});
bValueText.setText("93");
jScrollPane1.setBounds(new Rectangle(7, 137, 168, 84));
jPanel4.setLayout(borderLayout2);
sampleFont.setDoubleBuffered(true);
sampleFont.setEditable(false);
sampleFont.setText("应用");
sampleFont.setBackground(new Color(71, 171, 255)); //初始化
sampleFont.setForeground(new Color(0, 0, 93));
sampleFont.setFont(new Font("", Font.PLAIN, 14));
jCheckBox1.setText("粗体");
jCheckBox1.setBounds(new Rectangle(16, 262, 54, 26));
jCheckBox1.addChangeListener(new ChangeListener()
{
public void stateChanged(ChangeEvent e)
{
setSampleFont();
}
});
jCheckBox2.setBounds(new Rectangle(73, 262, 54, 26));
jCheckBox2.addChangeListener(new ChangeListener()
{
public void stateChanged(ChangeEvent e)
{
setSampleFont();
}
});
jCheckBox2.setText("斜体");
fontTypeList.setOpaque(true);
fontTypeList.addListSelectionListener(new ListSelectionListener()
{
public void valueChanged(ListSelectionEvent e)
{
setSampleFont();
}
});
backgroundSetSign.setBounds(new Rectangle(181, 194, 64, 26));
//选择设置sampleFont的背景颜色
backgroundSetSign.addChangeListener(new ChangeListener()
{
public void stateChanged(ChangeEvent e)
{
setSampleFont();
}
});
foregroundSetSign.setSelected(true);
foregroundSetSign.setText("字体");
foregroundSetSign.setBounds(new Rectangle(179, 140, 64, 26));
//选择设置sampleFont的字体颜色
foregroundSetSign.addChangeListener(new ChangeListener()
{
public void stateChanged(ChangeEvent e)
{
setSampleFont();
}
});
this.getContentPane().add(jPanel1);
jPanel2.add(jButton3);
jPanel2.add(jButton1);
jPanel2.add(jButton2);
this.getContentPane().add(jCheckBox2, null);
this.getContentPane().add(jCheckBox1, null);
this.getContentPane().add(jLabel1, null);
this.getContentPane().add(sizeValueText, null);
this.getContentPane().add(sizeValue, null);
this.getContentPane().add(jPanel3, null);
jPanel1.add(fontTypeList, java.awt.BorderLayout.CENTER);
jPanel3.add(rValue, null);
jPanel3.add(rValueText, null);
jPanel3.add(bValue, null);
jPanel3.add(gValue, null);
jPanel3.add(gValueText, null);
jPanel3.add(bValueText, null);
jPanel3.add(jLabel4, null);
jPanel3.add(jLabel3, null);
jPanel3.add(jLabel2, null);
jPanel3.add(jScrollPane1, null);
jPanel3.add(backgroundSetSign, null);
jPanel3.add(foregroundSetSign, null);
jPanel3.add(jButton4, null);
this.getContentPane().add(jPanel2);
jScrollPane1.getViewport().add(jPanel4, null);
jPanel4.add(sampleFont, BorderLayout.CENTER);
buttonGroup1.add(foregroundSetSign);
buttonGroup1.add(backgroundSetSign);
}
private void centerDialog() throws HeadlessException
{
// 使窗口居中显示
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = this.getSize();
if (frameSize.height > screenSize.height)
{
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width)
{
frameSize.width = screenSize.width;
}
this.setLocation( (screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
}
//******************************************************************************
//输入B的值处理
void bValueText_keyReleased(KeyEvent e)
{
//如果没有输入,则设置为0
try
{
//取得输入的值
int b = Integer.parseInt(rValueText.getText().trim());
//如果设置大于255,则将其设置为255
if (b > 255)
{
bValue.setValue(255);
}
//如果没有输入任何数或是小于0的数,则设置为0
else if (b <= 0)
{
bValue.setValue(0);
}
else
{
bValue.setValue(Integer.parseInt(bValueText.getText()));
}
}
catch (Exception ev)
{
//如果输入的数格式不对,则做任何处理
}
}
//*****************************************************************************
//将样本的字体设置成当前所有设置
void setSampleFont()
{
//设置示例文字的颜色
int r = rValue.getValue();
int g = gValue.getValue();
int b = bValue.getValue();
int fontSize = sizeValue.getValue();
//获取粗体或斜体设置
int x = boldAndItalicSet();
sampleFont.setFont(new Font("宋体", x, fontSize));
if (foregroundSetSign.isSelected())
{
sampleFont.setForeground(new Color(r, g, b));
}
else if (backgroundSetSign.isSelected())
{
sampleFont.setBackground(new Color(r, g, b));
}
}
//将目标字体设置成当前的所有设置项
void setContentFont()
{
String fontType; // = fontTypeList.getSelectedValue().toString().trim();
//获取粗体或斜体设置
int x = boldAndItalicSet();
//if (fontType == "默认" || fontType == null)
{
fontType = "Dialog";
}
MainFrame.contentText.setForeground(sampleFont.getForeground());
MainFrame.contentText.setBackground(sampleFont.getBackground());
MainFrame.contentText.setFont(sampleFont.getFont());
}
//******************************************************************************
void gValue_stateChanged(ChangeEvent e)
{
gValueText.setText(gValue.getValue() + "");
//根据当前的改变处理所有待设置字体
setSampleFont();
}
void bValue_stateChanged(ChangeEvent e)
{
bValueText.setText(bValue.getValue() + "");
//根据当前的改变处理所有待设置字体
setSampleFont();
}
//******************************************************************************
//粗体斜体设置
int boldAndItalicSet()
{
//既不是粗体,也不是斜体
if (!jCheckBox1.isSelected() && !jCheckBox2.isSelected())
{
f = Font.PLAIN;
}
//是粗体,不是斜体
else if (jCheckBox1.isSelected() && !jCheckBox2.isSelected())
{
f = Font.BOLD;
}
//不是粗体,是斜体
else if (!jCheckBox1.isSelected() && jCheckBox2.isSelected())
{
f = Font.ITALIC;
}
//是粗体,也是斜体
else if (jCheckBox1.isSelected() && jCheckBox2.isSelected())
{
f = Font.BOLD + Font.ITALIC;
}
//返回所选择的粗斜体设置值
return f;
}
}
//******************************************************************************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -