📄 fontchooser.java~151~
字号:
package chat;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.*;
public class FontChooser extends JFrame {
public static int OK_OPTION = 0;
public static int CANCEL_OPTION = 1;
private int OPTION;
JPanel fullPanel;
JPanel topPanel;
JPanel centerPanel;
JPanel undersidePanel;
JPanel fontPanel;
JPanel colorPanel;
JPanel viewPanel;
JPanel fontFamilyPanel;
JPanel fontStylePanel;
JPanel fontSizePanel;
JList fontList;
JList sizeList;
JList styleList;
JScrollPane fontFamilyScroll;
JScrollPane fontSizeScroll;
JScrollPane styleScroll;
JScrollPane sampleTextScroll;
JButton okButton;
JButton cancelButton;
JTextArea sampleText;
Font font;
String[] styles;
String[] sizes;
static String fontFamily="宋体";
static int fontStyle=Font.PLAIN;
static int fontSize=12;
Container con;
public FontChooser(){
super("字体属性对话框");
font=new Font("新宋体",Font.PLAIN,12);
sizes=new String[]{"8","10","12","14","16","18","20","22","24","30","34"};
styles=new String[]{"常规","粗体","斜体","粗斜体"};
okButton=new JButton("确定");
cancelButton=new JButton("取消");
fullPanel=new JPanel();
topPanel=new JPanel();
centerPanel=new JPanel();
undersidePanel=new JPanel();
fontPanel=new JPanel();
colorPanel=new JPanel();
viewPanel=new JPanel();
fontFamilyPanel=new JPanel();
fontStylePanel=new JPanel();
fontSizePanel=new JPanel();
fontList = new JList(GraphicsEnvironment.getLocalGraphicsEnvironment().
getAvailableFontFamilyNames())
{
public Dimension getPreferredScrollableViewportSize()
{
return new Dimension(160, 140);
}
};
fontList.setFont(font);
fontFamilyScroll=new JScrollPane(fontList);
//fontFamilyPanel.setPreferredSize(new Dimension(170,150));
fontFamilyPanel.add(fontFamilyScroll);
sizeList=new JList(sizes){
public Dimension getPreferredScrollableViewportSize() {
return new Dimension(60, 140);
}
};
sizeList.setFont(font);
fontSizeScroll=new JScrollPane(sizeList);
fontSizePanel.add(fontSizeScroll);
styleList=new JList(styles){
public Dimension getPreferredScrollableViewportSize(){
return new Dimension(60,140);
}
};
styleList.setFont(font);
styleScroll=new JScrollPane(styleList);
fontStylePanel.add(styleScroll);
fontList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
styleList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
sizeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
//styleList.setPreferredSize(new Dimension(60,160));
// fontFamilyPanel.setBorder(BorderFactory.createCompoundBorder(
// BorderFactory.createTitledBorder(
// "字体"),
// BorderFactory.createEmptyBorder(10,10,10,10)));
sampleText=new JTextArea(){
public Dimension getPreferredScrollableViewportSize() {
return new Dimension(300, 100);
}
};
sampleText.setEditable(false);
sampleText.setFont(font);
sampleText.setText("选择威尔,选择成功\nchoose WELL,choose to succeed");
sampleTextScroll=new JScrollPane(sampleText);
//sampleTextScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
//sampleTextScroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
fullPanel.setBorder(BorderFactory.createRaisedBevelBorder());
fontFamilyPanel.setBorder(BorderFactory.createTitledBorder("字体"));
fontStylePanel.setBorder(BorderFactory.createTitledBorder("字形"));
fontSizePanel.setBorder(BorderFactory.createTitledBorder("字体大小"));
viewPanel.setBorder(BorderFactory.createTitledBorder("预览"));
okButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
FontChooser.this.OPTION=FontChooser.this.OK_OPTION;
FontChooser.this.setVisible(false);
System.out.println(OPTION);
}
});
cancelButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
FontChooser.this.OPTION=FontChooser.this.CANCEL_OPTION;
FontChooser.this.setVisible(false);
System.out.println(OPTION);
}
});
viewPanel.add(sampleTextScroll);
fontPanel.add(fontFamilyPanel);
fontPanel.add(fontStylePanel);
fontPanel.add(fontSizePanel);
//centerPanel.setLayout(new GridLayout(3,1));
centerPanel.add(fontPanel);
centerPanel.add(colorPanel);
centerPanel.add(viewPanel);
undersidePanel.add(okButton);
undersidePanel.add(cancelButton);
con=this.getContentPane();
fullPanel.setLayout(new BorderLayout());
fullPanel.add(topPanel,BorderLayout.NORTH);
fullPanel.add(centerPanel,BorderLayout.CENTER);
fullPanel.add(undersidePanel,BorderLayout.SOUTH);
con.add(fullPanel);
setFont(null);
ListSelectionListener listListener = new ListSelectionListener(){
public void valueChanged(ListSelectionEvent e) {
sampleText.setFont(getCurrentFont());
}
};
fontList.addListSelectionListener(listListener);
styleList.addListSelectionListener(listListener);
sizeList.addListSelectionListener(listListener);
}
public void setFont(Font font){
if(font==null){
font=sampleText.getFont();
}
fontList.setSelectedValue(""+font.getFontName(),true);
fontList.ensureIndexIsVisible(fontList.getSelectedIndex());
sizeList.setSelectedValue(""+font.getSize(),true);
sizeList.ensureIndexIsVisible(fontList.getSelectedIndex());
styleList.setSelectedIndex(font.getStyle());
styleList.ensureIndexIsVisible(styleList.getSelectedIndex());
//System.out.println(font.getStyle());
}
public Font getFont(){
if(this.OPTION==this.OK_OPTION){
return getCurrentFont();
}else{
return null;
}
}
public Font getCurrentFont(){
fontFamily=(String)fontList.getSelectedValue();
fontSize=Integer.parseInt((String)sizeList.getSelectedValue());
fontStyle=styleList.getSelectedIndex();
//System.out.println(fontStyle);
return new Font(fontFamily,fontStyle,fontSize);
}
public static void main(String[] args) {
FontChooser fontChooser=new FontChooser();
fontChooser.setSize(500,600);
fontChooser.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -