📄 fontdialog.java
字号:
package javadesign;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
/****************************************************************
*
* 类FontDialog用来设置字体
*
********************************************************/
public class FontDialog extends JDialog implements ActionListener {
private String font[];
private JLabel fontLabel, fontTypeLabel, fontSizeLabel, fontShowLabel;
private JList fontList, fontTypeList, fontSizeList;
private JTextField fontField, fontTypeField, fontSizeField,fontShowField;
private JScrollPane fontScroll, fontTypeScroll, fontSizeScroll;
public static JButton sureButton, cancelButton;
private JPanel panel = new JPanel();
public static Font myfont;
private Container container = getContentPane();
public static boolean sure;
public FontDialog(Frame frame,String title, boolean modal){
super(frame,title,modal);
container.setLayout(null);
setLocation(Note.xp + 50, Note.yp + 150);
setResizable(false);
setSize(420, 300);
fontLabel = new JLabel("字体(F):");
fontField = new JTextField(Note.font_name);
GraphicsEnvironment g = GraphicsEnvironment.getLocalGraphicsEnvironment();
font = g.getAvailableFontFamilyNames();//获得系统支持的所有字体列表
fontList = new JList(font);
fontList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);// 设为单选模式
fontScroll = new JScrollPane(fontList);
///////////////////////////////////////////////////////////////////////
fontTypeLabel = new JLabel("字形(Y):");
String[] data = { "常规", "斜体", "粗体", "粗斜体" };
fontTypeList = new JList(data);
if (Note.font_type == 0)
fontTypeField = new JTextField("常规");
else if (Note.font_type == 1)
fontTypeField = new JTextField("粗体");
else if (Note.font_type == 2)
fontTypeField = new JTextField("斜体");
else fontTypeField = new JTextField("粗斜体");
fontTypeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);// 设为单选模式
fontTypeScroll = new JScrollPane(fontTypeList);
///////////////////////////////////////////////////////////////
fontSizeLabel = new JLabel("大小(S):");
String size[] = new String[100];
for (int i = 10; i <= 100; i++) {
size[i - 10] = String.valueOf(i);
}
fontSizeList = new JList(size);
fontSizeField = new JTextField(""+Note.font_size);
fontSizeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);// 设为单选模式
fontSizeScroll = new JScrollPane(fontSizeList);
/////////////////////////////////////////////////////////////////
sureButton = new JButton("确定");
cancelButton = new JButton("取消");
fontShowLabel = new JLabel("示例");
fontShowField = new JTextField("字体AaBbYy");
Border inside = BorderFactory.createBevelBorder(BevelBorder.LOWERED);
fontShowField.setBorder(BorderFactory.createTitledBorder(inside));
fontShowField.setEditable(false);
fontShowField.setHorizontalAlignment(fontShowField.CENTER);
fontShowField.setFont(myfont);
this.getRootPane().setDefaultButton(sureButton);//默认的回车按键
fontLabel.setBounds(10, 5, 162, 20);
fontField.setBounds(10, 25, 162, 20);
fontScroll.setBounds(10, 45, 162, 140);
fontTypeLabel.setBounds(177, 5, 100, 20);
fontTypeField.setBounds(177, 25, 100, 20);
fontTypeScroll.setBounds(177, 45, 100, 140);
fontSizeLabel.setBounds(282, 5, 50, 20);
fontSizeField.setBounds(282, 25, 50, 20);
fontSizeScroll.setBounds(282, 45, 50, 140);
sureButton.setBounds(337, 25, 60, 20);
cancelButton.setBounds(337, 50, 60, 20);
fontShowLabel.setBounds(177, 195, 100, 20);
fontShowField.setBounds(177, 215, 200, 45);
/////////////////////////////////////////////////////////////
fontField.setEditable(false);
fontTypeField.setEditable(false);
fontSizeField.setEditable(false);
fontField.setBackground(Color.WHITE);
fontTypeField.setBackground(Color.WHITE);
fontSizeField.setBackground(Color.WHITE);
container.add(fontLabel);
container.add(fontField);
container.add(fontScroll);
container.add(fontTypeLabel);
container.add(fontTypeField);
container.add(fontTypeScroll);
container.add(fontSizeLabel);
container.add(fontSizeField);
container.add(fontSizeScroll);
container.add(sureButton);
container.add(cancelButton);
container.add(fontShowLabel);
container.add(fontShowField);
////////////////////////////////////////////////////////////////
MouseListener mouseListener = new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
mouseClick(e);
}
};
fontList.addMouseListener(mouseListener);
fontTypeList.addMouseListener(mouseListener);
fontSizeList.addMouseListener(mouseListener);
sureButton.addActionListener(this);
cancelButton.addActionListener(this);
this.setVisible(true);
}
public void mouseClick(MouseEvent e) {
if (e.getSource() == fontList) {
Note.font_name = fontList.getSelectedValue().toString();
fontField.setText(Note.font_name);
} else if (e.getSource() == fontTypeList)
fontTypeField.setText(fontTypeList.getSelectedValue() .toString());
else if (e.getSource() == fontSizeList) {
fontSizeField.setText(fontSizeList.getSelectedValue().toString());
Note.font_size= Integer.parseInt(fontSizeField.getText());
}
show_Font();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == sureButton){
sure=true;
setVisible(false);
}
else if (e.getSource() == cancelButton){
sure=false;
setVisible(false);
}
}
public void show_Font() {
if (fontTypeField.getText().equals("常规"))
Note.font_type = 0;
else if (fontTypeField.getText().equals("斜体"))
Note.font_type = 2;
else if (fontTypeField.getText().equals("粗体"))
Note.font_type = 1;
else if (fontTypeField.getText().equals("粗斜体"))
Note.font_type = 3;
myfont = new Font(Note.font_name, Note.font_type, Note.font_size);
fontShowField.setFont(myfont);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -