Java字体选择对话框
2013-01-17 20:14
453 查看
/**
* 字体选择对话框。
*
* @author lucky star
*
*/
public class FontDialog extends JDialog {
private final JPanel contentPanel = new JPanel();
private JComboBox fontNameBox = null;
private JComboBox fontStyleBox = null;
private JComboBox fontSizeBox = null;
private JTextArea txtrHereIs = null;
private String fontName;
private String fontStyle;
private String fontSize;
private int fontSty;
private int fontSiz;
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
FontDialog dialog = new FontDialog();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the dialog.
*/
public FontDialog() {
setTitle("\u5B57\u4F53");
setBounds(100, 100, 483, 234);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(null);
{
JLabel lblf = new JLabel("\u5B57\u4F53(F):");
lblf.setBounds(0, 10, 54, 15);
contentPanel.add(lblf);
}
{
JLabel lbly = new JLabel("\u5B57\u5F62(Y):");
lbly.setBounds(182, 10, 54, 15);
contentPanel.add(lbly);
}
{
JLabel lbls = new JLabel("\u5927\u5C0F(S):");
lbls.setBounds(315, 10, 54, 15);
contentPanel.add(lbls);
}
{
JLabel label = new JLabel("\u663E\u793A\u6548\u679C:");
label.setBounds(126, 82, 64, 15);
contentPanel.add(label);
}
Panel panel = new Panel();
panel.setBounds(196, 40, 228, 113);
contentPanel.add(panel);
panel.setLayout(null);
{
txtrHereIs = new JTextArea();
txtrHereIs.setBounds(39, 38, 177, 44);
txtrHereIs
.setText("\u8FD9\u91CC\u663E\u793A\u9884\u89C8\r\nHere is the preview");
panel.add(txtrHereIs);
}
{
fontNameBox = new JComboBox();
fontNameBox.setBounds(49, 7, 123, 21);
contentPanel.add(fontNameBox);
fontNameBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent itemevent) {
fontName = (String) itemevent.getItem();
System.out.println(fontName);
// change preview
Font f = new Font(fontName, fontSty, fontSiz);
txtrHereIs.setFont(f);
}
});
}
{
fontStyleBox = new JComboBox();
fontStyleBox.setBounds(232, 7, 73, 21);
contentPanel.add(fontStyleBox);
fontStyleBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent itemevent) {
fontStyle = (String) itemevent.getItem();
fontSty = getFontStyleByCnName(fontStyle);
// change preview
Font f = new Font(fontName, fontSty, fontSiz);
txtrHereIs.setFont(f);
}
});
}
{
fontSizeBox = new JComboBox();
fontSizeBox.setBounds(379, 7, 78, 21);
contentPanel.add(fontSizeBox);
fontSizeBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent itemevent) {
fontSize = (String) itemevent.getItem();
fontSiz = Integer.parseInt(fontSize);
// change preview
Font f = new Font(fontName, fontSty, fontSiz);
txtrHereIs.setFont(f);
}
});
}
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
{
JButton okButton = new JButton("\u786E\u5B9A");
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionevent) {
int fontSty = getFontStyleByCnName(fontStyle);
int fontSiz = Integer.parseInt(fontSize);
JOptionPane.showMessageDialog(FontDialog.this,
"你选择的字体名称:" + fontName + ",字体样式:" + fontStyle
+ ",字体大小:" + fontSiz, "提示",
JOptionPane.CLOSED_OPTION);
}
});
okButton.setActionCommand("OK");
buttonPane.add(okButton);
getRootPane().setDefaultButton(okButton);
}
{
JButton cancelButton = new JButton("\u53D6\u6D88");
cancelButton.setActionCommand("Cancel");
buttonPane.add(cancelButton);
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionevent) {
FontDialog.this.dispose();
}
});
}
}
// 初始化字体名称
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
String[] fontNames = ge.getAvailableFontFamilyNames();
fontNameBox.setModel(new DefaultComboBoxModel(fontNames));
// 初始化字体样式
String[] fontStyles = { "常规", "斜体", "粗体", "粗斜体" };
fontStyleBox.setModel(new DefaultComboBoxModel(fontStyles));
// 初始化字体大小
String[] fontSizes = { "8", "9", "10", "11", "12", "14", "16", "18",
"20", "22", "24", "26", "28", "36", "48", "72" };
fontSizeBox.setModel(new DefaultComboBoxModel(fontSizes));
System.out.println("finish.");
fontSizeBox.setSelectedIndex(4);
fontStyle = (String) fontStyleBox.getSelectedItem();
fontSize = (String) fontSizeBox.getSelectedItem();
fontSty = getFontStyleByCnName(fontStyle);
fontSiz = Integer.parseInt(fontSize);
}
public static int getFontStyleByCnName(String fontStyle) {
if (fontStyle.equals("常规")) {
return Font.PLAIN;
}
if (fontStyle.equals("斜体")) {
return Font.ITALIC;
}
if (fontStyle.equals("粗体")) {
return Font.BOLD;
}
if (fontStyle.equals("粗斜体")) {
return Font.BOLD + Font.ITALIC;
}
return -1;
}
}
* 字体选择对话框。
*
* @author lucky star
*
*/
public class FontDialog extends JDialog {
private final JPanel contentPanel = new JPanel();
private JComboBox fontNameBox = null;
private JComboBox fontStyleBox = null;
private JComboBox fontSizeBox = null;
private JTextArea txtrHereIs = null;
private String fontName;
private String fontStyle;
private String fontSize;
private int fontSty;
private int fontSiz;
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
FontDialog dialog = new FontDialog();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the dialog.
*/
public FontDialog() {
setTitle("\u5B57\u4F53");
setBounds(100, 100, 483, 234);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(null);
{
JLabel lblf = new JLabel("\u5B57\u4F53(F):");
lblf.setBounds(0, 10, 54, 15);
contentPanel.add(lblf);
}
{
JLabel lbly = new JLabel("\u5B57\u5F62(Y):");
lbly.setBounds(182, 10, 54, 15);
contentPanel.add(lbly);
}
{
JLabel lbls = new JLabel("\u5927\u5C0F(S):");
lbls.setBounds(315, 10, 54, 15);
contentPanel.add(lbls);
}
{
JLabel label = new JLabel("\u663E\u793A\u6548\u679C:");
label.setBounds(126, 82, 64, 15);
contentPanel.add(label);
}
Panel panel = new Panel();
panel.setBounds(196, 40, 228, 113);
contentPanel.add(panel);
panel.setLayout(null);
{
txtrHereIs = new JTextArea();
txtrHereIs.setBounds(39, 38, 177, 44);
txtrHereIs
.setText("\u8FD9\u91CC\u663E\u793A\u9884\u89C8\r\nHere is the preview");
panel.add(txtrHereIs);
}
{
fontNameBox = new JComboBox();
fontNameBox.setBounds(49, 7, 123, 21);
contentPanel.add(fontNameBox);
fontNameBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent itemevent) {
fontName = (String) itemevent.getItem();
System.out.println(fontName);
// change preview
Font f = new Font(fontName, fontSty, fontSiz);
txtrHereIs.setFont(f);
}
});
}
{
fontStyleBox = new JComboBox();
fontStyleBox.setBounds(232, 7, 73, 21);
contentPanel.add(fontStyleBox);
fontStyleBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent itemevent) {
fontStyle = (String) itemevent.getItem();
fontSty = getFontStyleByCnName(fontStyle);
// change preview
Font f = new Font(fontName, fontSty, fontSiz);
txtrHereIs.setFont(f);
}
});
}
{
fontSizeBox = new JComboBox();
fontSizeBox.setBounds(379, 7, 78, 21);
contentPanel.add(fontSizeBox);
fontSizeBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent itemevent) {
fontSize = (String) itemevent.getItem();
fontSiz = Integer.parseInt(fontSize);
// change preview
Font f = new Font(fontName, fontSty, fontSiz);
txtrHereIs.setFont(f);
}
});
}
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
{
JButton okButton = new JButton("\u786E\u5B9A");
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionevent) {
int fontSty = getFontStyleByCnName(fontStyle);
int fontSiz = Integer.parseInt(fontSize);
JOptionPane.showMessageDialog(FontDialog.this,
"你选择的字体名称:" + fontName + ",字体样式:" + fontStyle
+ ",字体大小:" + fontSiz, "提示",
JOptionPane.CLOSED_OPTION);
}
});
okButton.setActionCommand("OK");
buttonPane.add(okButton);
getRootPane().setDefaultButton(okButton);
}
{
JButton cancelButton = new JButton("\u53D6\u6D88");
cancelButton.setActionCommand("Cancel");
buttonPane.add(cancelButton);
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionevent) {
FontDialog.this.dispose();
}
});
}
}
// 初始化字体名称
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
String[] fontNames = ge.getAvailableFontFamilyNames();
fontNameBox.setModel(new DefaultComboBoxModel(fontNames));
// 初始化字体样式
String[] fontStyles = { "常规", "斜体", "粗体", "粗斜体" };
fontStyleBox.setModel(new DefaultComboBoxModel(fontStyles));
// 初始化字体大小
String[] fontSizes = { "8", "9", "10", "11", "12", "14", "16", "18",
"20", "22", "24", "26", "28", "36", "48", "72" };
fontSizeBox.setModel(new DefaultComboBoxModel(fontSizes));
System.out.println("finish.");
fontSizeBox.setSelectedIndex(4);
fontStyle = (String) fontStyleBox.getSelectedItem();
fontSize = (String) fontSizeBox.getSelectedItem();
fontSty = getFontStyleByCnName(fontStyle);
fontSiz = Integer.parseInt(fontSize);
}
public static int getFontStyleByCnName(String fontStyle) {
if (fontStyle.equals("常规")) {
return Font.PLAIN;
}
if (fontStyle.equals("斜体")) {
return Font.ITALIC;
}
if (fontStyle.equals("粗体")) {
return Font.BOLD;
}
if (fontStyle.equals("粗斜体")) {
return Font.BOLD + Font.ITALIC;
}
return -1;
}
}
相关文章推荐
- Java 使用对话框选择文件并输出到控制台
- Java-记事本程序、文件选择对话框(JFileChooser)
- MFC如何制作字体选择对话框
- 应用颜色选择和字体选择对话框
- 设置文件选择对话框大小及纵向大小不可变--JAVA-SWING-JFileChooser-setMinimumSize
- 孙鑫 第十课之二 字体选择对话框编程(笔记+转载)
- GTK+ 消息对话框 属性对话框 字体选择对话框 颜色选择对话框 的使用例子
- java字体设置对话框
- 设置Java Swing的文件选择对话框只选择目录的方法
- \t\t孙鑫 第十课之二 字体选择对话框编程(笔记+转载)
- java对话框选择图片,并显示到lable上
- java字体对话框
- 如何使用字体选择对话框
- Java--第13周实验--任务2--编写一个FontFamily类,该类对象获取当前机器可用的全部字体名称,编写一个对话框FontDialog,该对话框是模式对话框,采用BorderLayout布局
- Java文件选择对话框(文件选择器JFileChooser)的使用:以一个文件加密器为例
- Java 实训 使用弹出对话框设置主窗体中的文本字体
- 目录选择对话框、文件打开或保存对话框、字体对话框和颜色对话框
- 在窗体上放置一个标签、一个按钮、一个fontDialog控件。标签内容改为“烟台大学”。实现功能:1)程序运行时,单击打开字体对话框按钮,可选择字体,并以所选字体作为标签字体。
- PyQt5学习教程15:字体选择对话框的使用
- [Python][小知识][NO.4] wxPython 字体选择对话框(O.O 不知道放到那里就放到这个分类的)