📄 myfontdlg.java
字号:
package notepad;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class MyFontDlg extends JDialog implements ListSelectionListener, ActionListener {
//private JFrame frame;
private JLabel font_label, type_label, size_label;
private JTextField font_field, type_field, size_field;
private JList font_list, type_list, size_list;
private JPanel label_panel, field_panel, list_panel;
private JPanel button_panel, all;
private JScrollPane font_scroll, size_scroll;
private JButton okay, cancle;
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
private final String[] font_name = ge.getAvailableFontFamilyNames();
//private final String[] font_name = new String[6];
private final String[] font_type = {"常规", "粗体", "斜体", "粗斜体"};
private final String[] font_size = {"5", "8", "9", "11", "16", "20", "22"};
//the properity to other class
private Font font;
public MyFontDlg (Frame owner, String title, boolean modal) {
super(owner, title, modal);
//frame = new JFrame();
font = null;
/*for (int i=0; i<6; ++i) {
font_name[i] = sys_font_name[i+20];
}*/
font_label = new JLabel("字体");
type_label = new JLabel("字形");
size_label = new JLabel("大小");
font_field = new JTextField(10);
type_field = new JTextField(6);
size_field = new JTextField(4);
font_list = new JList(font_name);
type_list = new JList(font_type);
size_list = new JList(font_size);
label_panel = new JPanel();
field_panel = new JPanel();
list_panel = new JPanel();
button_panel = new JPanel();
all = new JPanel();
font_scroll = new JScrollPane(font_list);
size_scroll = new JScrollPane(size_list);
okay = new JButton("确定");
cancle = new JButton("取消");
label_panel.setLayout(new GridLayout(1,3));
field_panel.setLayout(new FlowLayout());
list_panel.setLayout(new GridLayout(1,3));
button_panel.setLayout(new FlowLayout());
/*
label_panel.setLayout(new FlowLayout());
field_panel.setLayout(new FlowLayout());
list_panel.setLayout(new FlowLayout());
button_panel.setLayout(new FlowLayout());
*/
font_list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
type_list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
size_list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
font_list.setVisibleRowCount(6);
type_list.setVisibleRowCount(6);
size_list.setVisibleRowCount(6);
font_list.setAutoscrolls(true);
type_list.setAutoscrolls(true);
size_list.setAutoscrolls(true);
font_list.addListSelectionListener(this);
type_list.addListSelectionListener(this);
size_list.addListSelectionListener(this);
okay.addActionListener(this);
cancle.addActionListener(this);
label_panel.add(font_label);
label_panel.add(type_label);
label_panel.add(size_label);
field_panel.add(font_field);
field_panel.add(type_field);
field_panel.add(size_field);
/*
list_panel.add(font_list);
list_panel.add(type_list);
list_panel.add(size_list);*/
list_panel.add(font_scroll);
list_panel.add(type_list);
list_panel.add(size_scroll);
button_panel.add(okay);
button_panel.add(cancle);
all.setLayout(new BorderLayout());
all.add(label_panel, BorderLayout.NORTH);
all.add(field_panel, BorderLayout.CENTER);
all.add(list_panel, BorderLayout.SOUTH);
this.setLayout(new BorderLayout());
this.add(all, BorderLayout.NORTH);
this.add(button_panel, BorderLayout.CENTER);
this.setVisible(true);
this.setSize(360,260);
this.setLocation(100, 100);
//this.setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void valueChanged(ListSelectionEvent lse) {
if (font_list == lse.getSource() ) {
font_field.setText(font_list.getSelectedValue().toString());
}
if (type_list == lse.getSource() ) {
type_field.setText(type_list.getSelectedValue().toString());
}
if (size_list == lse.getSource() ) {
size_field.setText(size_list.getSelectedValue().toString());
}
}
public void actionPerformed(ActionEvent ae) {
if (okay == ae.getSource() ) {
int style = 0;
if ("常规".equals(type_field.getText())) {
style = Font.PLAIN;
}
if ("粗体".equals(type_field.getText())) {
style = Font.BOLD;
}
if ("斜体".equals(type_field.getText())) {
style = Font.ITALIC;
}
if ("粗斜体".equals(type_field.getText())) {
style = Font.ITALIC | Font.BOLD;
}
//System.out.println(style);
this.font = new Font(font_field.getText(), style,
Integer.parseInt(size_field.getText()));
exitFontDlg();
}
if (cancle == ae.getSource() ) {
exitFontDlg();
}
}
public Font getFont() {
return this.font;
}
public void setFontDlgSize(int x, int y) {
this.setSize(x, y);
}
private void exitFontDlg() {
this.setVisible(false);
}
public static void main(String[] args) {
//new MyFontDlg();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -