分享 本人实现的记事本中的字体对话框
import java.awt.*;import java.awt.event.*;
import javax.swing.*;
import java.util.List;
public class FontDialog extends JDialog
{
private static final long serialVersionUID = -6289497815381851003L;
private JButton button_ok;
private JButton button_cancel;
private Font font;
private JList list_font;
private JList list_style;
private JList list_size;
private JLabel label_font;
private JLabel label_style;
private JLabel label_size;
private JLabel label_demo;
private JLabel label_code;
private JTextField text_font;
private JTextField text_style;
private JTextField text_size;
private JTextField text_demo = new JTextField("ABCDEF");
private JComboBox choice_code;
private String[] fontItems = GraphicsEnvironment
.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
private String[] typeItems =
{
"常规", "斜体", "粗体", "粗斜体"
};
private String[] sizeItems =
{
"8", "9", "10", "11", "12", "14", "18", "24", "30", "36", "40",
"48", "60", "72"
};
// 这是对话框model,List类型,0 是字体,1 是风格,2 是大小;
private List model;
public List getModel()
{
return model;
}
public FontDialog(JFrame frame, boolean modal, List model)
{
this.model = model;
init();
setTitle("字体");
setModal(modal);
setBounds(400, 200, 435, 320);
setResizable(false);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
private void init()
{
label_font = new JLabel("字体:");
label_style = new JLabel("字形:");
label_size = new JLabel("大小:");
label_demo = new JLabel("示例:");
label_code = new JLabel("字符集:");
setLayout(null);
button_ok = new JButton("确定");
button_ok.setBounds(350, 45, 65, 20);
button_ok.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
model.clear();
model.add(text_font.getText());
model.add(list_style.getSelectedIndex());
model.add(text_size.getText());
setVisible(false);
}
});
add(button_ok);
button_cancel = new JButton("取消");
button_cancel.setBounds(350, 70, 65, 20);
button_cancel.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
}
});
add(button_cancel);
label_font.setBounds(13, 27, 145, 20);
add(label_font);
text_font = new JTextField();
text_font.setBounds(13, 47, 145, 19);
add(text_font);
list_font = new JList(fontItems);
list_font.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
text_font.setText(list_font.getSelectedValue().toString());
font = new Font(text_font.getText(), getFontStyle(text_style
.getText()), Integer.parseInt(text_size.getText()
.trim()));
text_demo.setFont(font);
}
});
list_font.setBounds(13, 65, 145, 87);
JScrollPane font_scrollPane = new JScrollPane(list_font);
font_scrollPane.setBounds(13, 65, 145, 87);
add(font_scrollPane);
label_style.setBounds(168, 27, 110, 20);
add(label_style);
text_style = new JTextField();
text_style.setBounds(168, 47, 110, 19);
add(text_style);
list_style = new JList(typeItems);
list_style.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
text_style.setText(list_style.getSelectedValue().toString());
font = new Font(text_font.getText(), getFontStyle(text_style
.getText()), Integer.parseInt(text_size.getText()
.trim()));
text_demo.setFont(font);
}
});
list_style.setBounds(168, 65, 110, 87);
JScrollPane style_scrollPane = new JScrollPane(list_style);
style_scrollPane.setBounds(168, 65, 110, 87);
add(style_scrollPane);
label_size.setBounds(287, 27, 52, 20);
add(label_size);
text_size = new JTextField();
text_size.setBounds(287, 47, 52, 19);
add(text_size);
list_size = new JList(sizeItems);
list_size.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
text_size.setText(list_size.getSelectedValue().toString());
font = new Font(text_font.getText(), getFontStyle(text_style
.getText()), Integer.parseInt(text_size.getText()
.trim()));
text_demo.setFont(font);
}
});
list_size.setBounds(287, 65, 52, 87);
JScrollPane size_scrollPane = new JScrollPane(list_size);
size_scrollPane.setBounds(287, 65, 52, 87);
add(size_scrollPane);
label_demo.setBounds(170, 165, 50, 20);
add(label_demo);
text_demo.setFont(font);
text_demo.setEditable(false);
text_demo.setBounds(180, 187, 150, 35);
add(text_demo);
label_code.setBounds(168, 235, 50, 20);
add(label_code);
choice_code = new JComboBox();
choice_code.addItem("CHINESE_GB2312");
choice_code.addItem("西方");
choice_code.setBounds(168, 260, 172, 20);
add(choice_code);
getRootPane().setDefaultButton(button_ok);
String font_name = (String) model.get(0);
int font_style = (Integer) model.get(1);
String font_size = (String) model.get(2);
list_font.setSelectedValue(font_name, true);
text_font.setText(font_name);
list_style.setSelectedIndex(font_style);
text_style.setText(list_style.getSelectedValue().toString());
list_size.setSelectedValue(font_size, true);
text_size.setText(font_size);
}
private int getFontStyle(String style)
{
if (style.equals("斜体"))
return Font.ITALIC;
else
if (style.equals("粗体"))
return Font.BOLD;
else
if (style.equals("粗斜体"))
return Font.ITALIC | Font.BOLD;
else
return Font.PLAIN;
}
}
有点长,但是实在不知道字符集怎么实现 有知道的给我指教指教