📄 fontdialog.java
字号:
package boya;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
class FontDialog extends JDialog implements ID,ActionListener
,ListSelectionListener{
private DrawFrame window;
private Font font;
private int style;
private int size;
private JButton ok,cancel;
private JList fontList;
private JLabel fontDisplay;
private JComboBox chooseSize;
public FontDialog(DrawFrame window){
super(window,"字体选择",true);
this.window=window;
font=window.getFont();
style=font.getStyle();
size=font.getSize();
JPanel buttonPane=new JPanel();
buttonPane.add(ok=createButton("确认"));
buttonPane.add(cancel=createButton("取消"));
getContentPane().add(buttonPane,BorderLayout.SOUTH);
JPanel dataPane=new JPanel();
dataPane.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(Color.black),
BorderFactory.createEmptyBorder(5,5,5,5)));
GridBagLayout gbLayout=new GridBagLayout();
dataPane.setLayout(gbLayout);
GridBagConstraints constraints=new GridBagConstraints();
JLabel label=new JLabel("选择字体");
constraints.fill=GridBagConstraints.HORIZONTAL;
constraints.gridwidth=GridBagConstraints.REMAINDER;
gbLayout.setConstraints(label,constraints);
dataPane.add(label);
GraphicsEnvironment e=GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fonts=e.getAvailableFontFamilyNames();
fontList=new JList(fonts);
fontList.setValueIsAdjusting(true);
fontList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
fontList.setSelectedValue(font.getFamily(),true);
fontList.addListSelectionListener(this);
JScrollPane chooseFont=new JScrollPane(fontList);
chooseFont.setMinimumSize(new Dimension(300,100));
JPanel display=new JPanel();
fontDisplay=new JLabel("字体样例:x X y Y z Z");
fontDisplay.setPreferredSize(new Dimension(300,100));
display.add(fontDisplay);
JSplitPane splitPane=new JSplitPane(JSplitPane.VERTICAL_SPLIT,true,chooseFont,display);
gbLayout.setConstraints(splitPane,constraints);
dataPane.add(splitPane);
JPanel sizePane=new JPanel();
label=new JLabel("选择字体大小");
sizePane.add(label);
String[] sizeList={"8","10","12","14","16","18","20","22","24"};
chooseSize=new JComboBox(sizeList);
chooseSize.setSelectedItem(Integer.toString(size));
chooseSize.addActionListener(this);
sizePane.add(chooseSize);
gbLayout.setConstraints(sizePane,constraints);
dataPane.add(sizePane);
JRadioButton bold=new JRadioButton("加粗",(style&Font.BOLD)>0);
JRadioButton italic=new JRadioButton("斜体",(style&Font.ITALIC)>0);
bold.addItemListener(new StyleListener(Font.BOLD));
italic.addItemListener(new StyleListener(Font.ITALIC));
JPanel stylePane=new JPanel();
stylePane.add(bold);
stylePane.add(italic);
gbLayout.setConstraints(stylePane,constraints);
dataPane.add(stylePane);
getContentPane().add(dataPane,BorderLayout.CENTER);
pack();
setVisible(false);
}
JButton createButton(String label){
JButton button=new JButton(label);
button.setPreferredSize(new Dimension(80,20));
button.addActionListener(this);
return button;
}
public void actionPerformed(ActionEvent e){
Object source=e.getSource();
if(source==ok){
window.setFont(font);
setVisible(false);
}
else if(source==cancel)
setVisible(false);
else if(source==chooseSize){
size=Integer.parseInt((String)chooseSize.getSelectedItem());
font=font.deriveFont((float)size);
fontDisplay.setFont(font);
fontDisplay.repaint();
}
}
public void valueChanged(ListSelectionEvent e){
if(!e.getValueIsAdjusting()){
font=new Font((String)fontList.getSelectedValue(),style,size);
fontDisplay.setFont(font);
fontDisplay.repaint();
}
}
class StyleListener implements ItemListener{
private int astyle;
public StyleListener(int style){
this.astyle=style;
}
public void itemStateChanged(ItemEvent e){
if(e.getStateChange()==ItemEvent.SELECTED)
style|=astyle;
else style&=~astyle;
font=font.deriveFont(style);
fontDisplay.setFont(font);
fontDisplay.repaint();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -