📄 editfont.java
字号:
/*************************************************************************************
* 类名: EditFont
*
* 功能: 此类为“字体选择对话框”生成所需的类,主要用于用户定义字体
*
**************************************************************************************/
package edit.com;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
class EditFont extends JPanel
{
private JPanel southPanel;
private JPanel centerPanel;
private boolean flag; //“字体选择对话框”是否正确返回
private JDialog dialog;
private JLabel[] labels = new JLabel[3];
private JButton[] buttons = new JButton[2];//“确定”和“取消”按钮
private JTextField[] textFields = new JTextField[3]; //分别显示字体的风格,大小,模式
private JList[] lists = new JList[3];//字体风格列表,字体大小列表,字体模式列表
private JTextField showText; //用于显示用户所定义的字体模型
private FontSet fontSet = new FontSet(); //用于存储用户定义的字体数据
private EditFontTools tool = new EditFontTool(); //工具类
public EditFont()
{
setLayout( new BorderLayout() );
southPanel = new JPanel();
//生成”确认“和”取消“按钮
buttons[0] = tool.createButton( new OkButtonAction( " O k " ) ,
Color.orange );
buttons[1] = tool.createButton( new CancelButtonAction( "Cancel" ) ,
Color.orange );
tool.addSouthPanel( buttons, southPanel );
add( southPanel , BorderLayout.SOUTH );
centerPanel = new JPanel();
//生成用户操作面板
addCenterPanel( centerPanel );
}
public boolean showDialog( Component c , String s )
{
flag = false;
Frame owner = null;
if( c instanceof Frame )
{
owner = ( Frame )c;
}
else
{
owner = ( Frame )SwingUtilities.getAncestorOfClass( Frame.class , c );
}
if( dialog == null || dialog.getOwner() != owner )
{
owner = null;
dialog = new JDialog( owner , true );
dialog.getContentPane().add( this );
dialog.setResizable( false );
dialog.setSize( 550 , 250 );
}
dialog.setTitle( s );
dialog.show();
return flag;
}
public FontSet getFontSet()
{
return fontSet;
}
/*****************************************
private class or private function defined
******************************************/
private void addCenterPanel( JPanel aPanel )
{
aPanel.setLayout( new GridBagLayout() );
GridBagConstraints constraints = new GridBagConstraints();
String[] labelText = { " Style " , " Size " , " Modal " };
labels = tool.createLabel( labelText );
//生成用于分别显示字体的风格,大小,模式的文本区域
String[] textShow = { " Serif " , " 5 " , " Bold " };
textFields = tool.createText( 3 , textShow );
//分别生成字体风格列表,字体大小列表,字体模式列表
String[] styleText = { " Serif " , " SansSerif " , " Monospaced " ,
" Dialog " , " DialogInput " };
String[] sizeText = { " 9 " , " 10 " , " 11 " , " 12 " ,
" 13 " , " 14 " , " 15 " , " 16 " , " 17 " ,
" 18 " , " 19 " };
String[] modalText = { " Bold " , " Italic " ,
" BoldItalic " , " Normal " };
lists[0] = tool.createList( styleText );
lists[1] = tool.createList( sizeText );
lists[2] = tool.createList( modalText );
//生成用于显示用户所定义的字体模型的文本区
showText = tool.createShowText( " Test Font Express " , fontSet );
//用于使用滚动显示的模式来显示字体风格列表,字体大小列表,字体模式列表
JScrollPane[] scrollPane =
tool.addEditFontListListenerToList( lists,
textFields ,
showText ,
fontSet );
//the first column
Box temBox = tool.createVerticalBox( 6 );
tool.addToCenterWithConstraint( temBox , constraints , aPanel, 0 , 0 , 3 , 1 , GridBagConstraints.NONE ,
GridBagConstraints.WEST );
tool.addToCenterWithConstraint( labels[0] , constraints , aPanel, 0 , 1 , 1 , 1 , GridBagConstraints.NONE ,
GridBagConstraints.CENTER );
Box temBox_2 = Box.createVerticalBox();
temBox_2.add( Box.createVerticalStrut( 4 ) );
tool.addToCenterWithConstraint( temBox_2 , constraints , aPanel, 0 , 2 , 3 , 1 , GridBagConstraints.NONE ,
GridBagConstraints.WEST );
textFields[0].setColumns( 10 );
tool.addToCenterWithConstraint( textFields[0] , constraints , aPanel, 0 , 3 , 1 , 1 , GridBagConstraints.NONE ,
GridBagConstraints.WEST );
Box horizonBox = tool.createVerticalBox( 2 );
tool.addToCenterWithConstraint( horizonBox , constraints , aPanel, 0 , 4 , 1 , 1 , GridBagConstraints.NONE ,
GridBagConstraints.WEST );
tool.addToCenterWithConstraint( scrollPane[0] , constraints , aPanel, 0 , 5 , 1 , 1 , GridBagConstraints.NONE ,
GridBagConstraints.WEST );
// the second column
Box verticalBox = tool.createVerticalBox( 12 );
tool.addToCenterWithConstraint( verticalBox , constraints , aPanel, 1 , 1 , 1 , 1 , GridBagConstraints.NONE ,
GridBagConstraints.WEST );
//the third column
tool.addToCenterWithConstraint( labels[1] , constraints , aPanel, 2 , 1 , 1 , 1 , GridBagConstraints.NONE ,
GridBagConstraints.WEST );
textFields[1].setColumns( 5 );
tool.addToCenterWithConstraint( textFields[1] , constraints , aPanel, 2 , 3 , 1 , 1 , GridBagConstraints.NONE ,
GridBagConstraints.WEST );
tool.addToCenterWithConstraint( scrollPane[1] , constraints , aPanel, 2 , 5 , 1 , 1 , GridBagConstraints.NONE ,
GridBagConstraints.WEST );
//the forth column
Box verticalBox_1 = tool.createVerticalBox( 12 );
tool.addToCenterWithConstraint( verticalBox_1 , constraints , aPanel, 3 , 1 , 1 , 1 , GridBagConstraints.NONE ,
GridBagConstraints.WEST );
//the fifth column
tool.addToCenterWithConstraint( labels[2] , constraints , aPanel, 4 , 1 , 1 , 1 , GridBagConstraints.NONE ,
GridBagConstraints.CENTER );
textFields[2].setColumns( 7 );
tool.addToCenterWithConstraint( textFields[2] , constraints , aPanel, 4 , 3 , 1 , 1 , GridBagConstraints.NONE ,
GridBagConstraints.WEST );
tool.addToCenterWithConstraint( scrollPane[2] , constraints , aPanel, 4 , 5 , 1 , 1 , GridBagConstraints.NONE ,
GridBagConstraints.WEST );
//the sixth column
Box verticalBox_2 = tool.createVerticalBox( 12 );
tool.addToCenterWithConstraint( verticalBox_2 , constraints , aPanel, 5 , 1 , 1 , 1 , GridBagConstraints.NONE ,
GridBagConstraints.WEST );
//the seventh column
tool.addToCenterWithConstraint( showText , constraints , aPanel, 6 , 5 , 1 , 1 , GridBagConstraints.BOTH ,
GridBagConstraints.WEST );
add( aPanel , BorderLayout.CENTER );
}
private class OkButtonAction extends AbstractAction
{
public OkButtonAction( String name )
{
putValue( Action.NAME , name );
}
public void actionPerformed( ActionEvent e )
{
flag = true;
dialog.setVisible( false );
}
}
private class CancelButtonAction extends AbstractAction
{
public CancelButtonAction( String name )
{
putValue( Action.NAME , name );
}
public void actionPerformed( ActionEvent e )
{
dialog.setVisible( false );
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -