📄 fontdialog.java
字号:
package mypack;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
class FontDialog extends JDialog implements ActionListener ,ItemListener
{
/**
*
*/
private static final long serialVersionUID = 8579942140211456145L;
JComboBox listSize,listName; //下拉列表框
List listStyle;
JLabel labelStyle,labelSize,labelName,labelTest;
JButton bSure,bCancle;
int[] fontSize=new int[50]; //设置字体大小
int fontStyle[]={Font.PLAIN,Font.ITALIC,Font.BOLD,Font.BOLD+Font.ITALIC,Font.ROMAN_BASELINE,Font.TRUETYPE_FONT}; //设置字体样式
String styleName[]={"正常","斜体","加粗","加粗斜体","ROMAN_BASELINE","TRUETYPE_FONT"}; //添加字形列表
GraphicsEnvironment ge;
Font f;
FontDialog(JFrame f,String s,Boolean b)
{
super(f,s,b);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Container con=getContentPane();
con.setLayout(null);
setBounds(60,60,600,400);
labelTest=new JLabel("Welcome to use!");
labelTest.setForeground(Color.RED);
labelSize=new JLabel(" 字体大小");
labelStyle=new JLabel(" 字体风格");
labelName=new JLabel(" 字体名");
bSure=new JButton("确认");
bCancle=new JButton("取消");
listStyle=new List(6,false);
listStyle.setBackground(Color.GREEN);
listSize=new JComboBox();
listSize.setBackground(Color.GREEN);
listName=new JComboBox();
listName.setBackground(Color.GREEN);
//获取字体名
ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
String fontName[]=ge.getAvailableFontFamilyNames();
for(int i=0;i<fontName.length;i++)
{
listName.addItem(fontName[i]);
}
//获取字体型号
for(int i=0,j=20;i<50;i++,j++)
{
fontSize[i]=j;
listSize.addItem(fontSize[i]);
}
//获取字体风格
for(int i=0;i<5;i++)
{
listStyle.add(styleName[i]);
}
listName.addItemListener(this);
listSize.addItemListener(this);
listStyle.addItemListener(this);
bSure.addActionListener(this);
bCancle.addActionListener(this);
con.add(labelName);
con.add(listName);
con.add(labelStyle);
con.add(listStyle);
con.add(labelSize);
con.add(listSize);
con.add(labelTest);
con.add(bSure);
con.add(bCancle);
labelName.setBounds(50,25,100,25);
labelStyle.setBounds(250,25,100,25);
labelSize.setBounds(450,25,100,25);
listName.setBounds(40,75,100,25);
listStyle.setBounds(250,75,150,25);
listSize.setBounds(450,75,100,25);
labelTest.setBounds(200,125,200,100);
bSure.setBounds(50,270,100,25);
bCancle.setBounds(450,270,100,25);
con.setBackground(Color.YELLOW);
setResizable(false);
con.validate();
}
public void itemStateChanged(ItemEvent e) {
// TODO 自动生成方法存根
String name=(String)listName.getSelectedItem();
int fStyle=listStyle.getSelectedIndex();
int fSize=(Integer)listSize.getSelectedItem();
f=new Font(name,fontStyle[fStyle],fSize);
labelTest.setFont(f);
}
public void actionPerformed(ActionEvent e) {
// TODO 自动生成方法存根
if(e.getSource()==bSure)
{
getfont();
setVisible(false);
}
else if(e.getSource()==bCancle)
{
setVisible(false);
}
}
public Font getfont()
{
return f;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -