📄 fontdialog.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//import javax.swing.event.*;
public class FontDialog{
public static void main(String[] args){
FontDialogFrame frame=new FontDialogFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class FontDialogFrame extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
public FontDialogFrame(){
setTitle("Pxg-GBLTest");
setSize(DEFAULT_W,DEFAULT_H);
GridBagLayout layout=new GridBagLayout();
setLayout(layout);
ActionListener listener=new FontAction();
JLabel faceLabel=new JLabel("Face:");
face=new JComboBox(new String[]{
"Serif","华文琥珀","华文彩云","隶书"
});
face.addActionListener(listener);
JLabel sizeLabel=new JLabel("Size:");
size=new JComboBox(new String[]{
"8","18","28","38"
});
size.addActionListener(listener);
bold=new JCheckBox("Bold");
bold.setBackground(new Color(255,0,0));
bold.addActionListener(listener);
italic=new JCheckBox("Italic");
italic.addActionListener(listener);
sample=new JTextArea();
sample.setText("测试GBL布局");
sample.setEditable(true);
sample.setLineWrap(true);
sample.setBorder(BorderFactory.createEtchedBorder());
add(faceLabel,new GBC(0,0).setAnchor(GBC.EAST));
add(face,new GBC(1,0).setFill(GBC.HORIZONTAL).setWeight(100,0).setInsets(1));
add(sizeLabel,new GBC(0,1).setAnchor(GBC.EAST));
add(size,new GBC(1,1).setFill(GBC.HORIZONTAL).setWeight(100,0).setInsets(1));
add(bold,new GBC(0,2,2,1).setAnchor(GBC.CENTER).setWeight(100,100));
add(italic,new GBC(0,3,2,1).setAnchor(GBC.CENTER).setWeight(100,100));
add(sample,new GBC(2,0,1,4).setFill(GBC.BOTH).setWeight(100,100));
}
public static final int DEFAULT_W=600;
public static final int DEFAULT_H=400;
private JComboBox face;
private JComboBox size;
private JCheckBox bold;
private JCheckBox italic;
private JTextArea sample;
private class FontAction implements ActionListener{
public void actionPerformed(ActionEvent event){
String fontFace=(String)face.getSelectedItem();
int fontStyle=(bold.isSelected()?Font.ITALIC:0)
+(italic.isSelected()?Font.ITALIC:0);
int fontSize=Integer.parseInt((String)size.getSelectedItem());
Font font=new Font(fontFace,fontStyle,fontSize);
sample.setFont(font);
sample.repaint();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -