class EditFontTool extends NewTool
                   implements EditFontTools {
     public JLabel[] createLabel( String labelText[] ) {
         JLabel[] labels = new JLabel[labelText.length];         for ( int i = 0 ; i < labelText.length ; i++ ) {
             labels[i] = new JLabel( labelText[i] );
         }         return labels;
     }     public JTextField[] createText( int count , String[] text ) {
         JTextField[] texts = new JTextField[count];         for ( int i = 0 ; i < count ; i++ ) {
             texts[i] = new JTextField();
             texts[i].setBackground( Color.white );
          texts[i].setEditable( false );
             texts[0].setText( text[i] );
         }         return texts;
     }     public JList createList( String[] listText ) {
         JList list = new JList( listText );
         list.setVisibleRowCount( 4 );
         list.setSelectionBackground( Color.red );         return list;
     }     public JTextField createShowText( String text , FontSet fontSet ) {
        JTextField showText = new JTextField( text );        Border etch = BorderFactory.createEtchedBorder( EtchedBorder.RAISED );
        showText.setEditable( false );
        showText.setMaximumSize( showText.getSize() );
        showText.setFont( new Font( fontSet.getStyle() ,
                                    fontSet.getModal() ,
                                    fontSet.getSize() ) );
        showText.setBorder( BorderFactory.createTitledBorder( etch , "Express"  ) );        return showText;
     }     public JScrollPane[] addEditFontListListenerToList( JList[] lists ,
                                                JTextField[] textFields ,
                                                JTextField showText ,
                                                FontSet fontSet ) {
         JScrollPane[] scrollPane = new JScrollPane[lists.length];         EditFontListListener listListener =
            new EditFontListListener( lists , textFields , showText , fontSet );         for ( int i = 0 ; i < lists.length ; i++ ) {
             lists[i].addListSelectionListener( listListener );
             scrollPane[i] = new JScrollPane( lists[i] );
         }         return scrollPane;
     }
}
interface EditFontTools extends Tools {
    //生成显示标签
    public JLabel[] createLabel( String labelText[] );    //生成文本框
    public JTextField[] createText( int count , String[] text );    //生成列表
    public JList createList( String[] listText );    //生成字体模型显示文本区
    public JTextField createShowText( String text , FontSet fontSet );    //用滚动的方式将列表显示
    public JScrollPane[] addEditFontListListenerToList( JList[] lists ,
                                                        JTextField[] textFields ,
                                                        JTextField showText ,
                                                        FontSet fontSet );

解决方案 »

  1.   

    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 );
            }
        }
    }
      

  2.   

    不懂楼主的意思,按我的理解给你一个例子。// FontPropertiesPanel.java
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;public class FontPropertiesPanel extends JPanel {  protected JList nameList;
      protected JComboBox sizeBox;
      protected JCheckBox boldBox;
      protected JCheckBox italicBox;  protected SampleTextFrame frame;  public final static int[] fontSizes = {10, 12, 14, 18, 24, 32, 48, 64};  public FontPropertiesPanel(SampleTextFrame stf) {
        super();
        frame = stf;
        createComponents();
        buildLayout();
      }  protected void buildLayout() {
        JLabel label;
        GridBagConstraints gbc = new GridBagConstraints();
        GridBagLayout gbl = new GridBagLayout();
        setLayout(gbl);    gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(5, 10, 5, 10);    gbc.gridx = 0;
        label = new JLabel("Name:", JLabel.LEFT);
        gbl.setConstraints(label, gbc);
        add(label);
        label = new JLabel("Size:", JLabel.LEFT);
        gbl.setConstraints(label, gbc);
        add(label);
        gbl.setConstraints(boldBox, gbc);
        add(boldBox);    gbc.gridx++;
        nameList.setVisibleRowCount(3);
        JScrollPane jsp = new JScrollPane(nameList);
        gbl.setConstraints(jsp, gbc);
        add(jsp);
        gbl.setConstraints(sizeBox, gbc);
        add(sizeBox);
        gbl.setConstraints(italicBox, gbc);
        add(italicBox);
      }  protected void createComponents() {
        GraphicsEnvironment ge =
            GraphicsEnvironment.getLocalGraphicsEnvironment();
        String[] names = ge.getAvailableFontFamilyNames();
        nameList = new JList(names);
        nameList.setSelectedIndex(0);
        nameList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        nameList.addListSelectionListener(new ListSelectionListener() {
          public void valueChanged(ListSelectionEvent event) {
            handleFontPropertyChange();
          }
        }
        );
        Integer sizes[] = new Integer[fontSizes.length];
        for (int i = 0; i < sizes.length; i++) {
          sizes[i] = new Integer(fontSizes[i]);
        }
        sizeBox = new JComboBox(sizes);
        sizeBox.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent event) {
            handleFontPropertyChange();
          }
        }
        );
        boldBox = new JCheckBox("Bold");
        boldBox.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent event) {
            handleFontPropertyChange();
          }
        }
        );
        italicBox = new JCheckBox("Italic");
        italicBox.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent event) {
            handleFontPropertyChange();
          }
        }
        );
      }  protected void handleFontPropertyChange() {
        frame.refreshDisplayFont();
      }  public String getSelectedFontName() {
        return (String)(nameList.getSelectedValue());
      }  public int getSelectedFontSize() {
        return ((Integer)(sizeBox.getSelectedItem())).intValue();
      }  public boolean isBoldSelected() {
        return boldBox.isSelected();
      }  public boolean isItalicSelected() {
        return italicBox.isSelected();
      }}// SampleTextFrame.java
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.border.*;
    import javax.swing.event.*;
    import javax.swing.text.*;public class SampleTextFrame extends JFrame {  protected FontPropertiesPanel propertiesPanel;
      protected JTextField sampleText;
      protected JLabel displayArea;  public static void main(String[] args) {
        SampleTextFrame stf = new SampleTextFrame();
        stf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        stf.setVisible(true);
      }  public SampleTextFrame() {
        super();
        createComponents();
        createDocumentListener();
        buildLayout();
        refreshDisplayFont();
        pack();
      }  protected void createComponents() {
        propertiesPanel = new FontPropertiesPanel(this);
        sampleText = new JTextField(20);
        displayArea = new JLabel("");
        displayArea.setPreferredSize(new Dimension(200, 75));
        displayArea.setMinimumSize(new Dimension(200, 75));
      }  protected void createDocumentListener() {
        Document document = sampleText.getDocument();
        document.addDocumentListener(new DocumentListener() {
          public void changedUpdate(DocumentEvent event) {
            handleDocumentUpdate();
          }      public void insertUpdate(DocumentEvent event) {
            handleDocumentUpdate();
          }      public void removeUpdate(DocumentEvent event) {
            handleDocumentUpdate();
          }
        }
        );
      }  protected void buildLayout() {
        Container pane = getContentPane();
        GridBagConstraints gbc = new GridBagConstraints();
        GridBagLayout gbl = new GridBagLayout();
        pane.setLayout(gbl);    gbc.insets = new Insets(5, 10, 5, 10);
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.weightx = 1;    gbc.gridx = 0;
        BevelBorder bb = new BevelBorder(BevelBorder.RAISED);
        TitledBorder tb = new TitledBorder(bb, "Font");
        propertiesPanel.setBorder(tb);
        gbl.setConstraints(propertiesPanel, gbc);
        pane.add(propertiesPanel);    gbl.setConstraints(sampleText, gbc);
        pane.add(sampleText);    gbl.setConstraints(displayArea, gbc);
        pane.add(displayArea);
      }  protected void handleDocumentUpdate() {
        displayArea.setText(sampleText.getText());
      }  public void refreshDisplayFont() {
        displayArea.setFont(getSelectedFont());
      }  public Font getSelectedFont() {
        String name = propertiesPanel.getSelectedFontName();
        int style = 0;
        style += (propertiesPanel.isBoldSelected() ? Font.BOLD : 0);
        style += (propertiesPanel.isItalicSelected() ? Font.ITALIC : 0);
        int size = propertiesPanel.getSelectedFontSize();
        return new Font(name, style, size);
      }}