0

我创建了一个 JFrame,用户可以在其中编辑 JTextArea 中的文本。有一个 JComboBox 可以更改字体类型,一个 JComboBox 可以更改字体大小,还有 2 个 JCheckBoxes 可以使文本变为粗体和斜体。我已经完成了 JCheckBoxes,但我无法弄清楚如何通过使用 JComoboBoxes 来更改字体和字体大小。任何帮助,将不胜感激。

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;

    public class SetFontModified extends JFrame 
    {
        private JPanel p1, p2;
        private JLabel jlblFontName, jlblFontSize;
        private JComboBox jcbFonts, jcbSizes;
        private JTextArea jtxtWelcome;
        private JCheckBox  jckbBold, jckbItalic;

        public SetFontModified()                                                              
        {
        p1 = new JPanel();
        jlblFontName = new JLabel("Font Name");                                               

        GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();   
        String[] fontNames = e.getAvailableFontFamilyNames();                                 
        jcbFonts = new JComboBox(fontNames);                                                      

        jlblFontSize = new JLabel("Font Size");// add 2nd JLabel

        jcbSizes = new JComboBox(); // create 2nd JComboBox
        for(int i = 1; i < 101; i ++) // populate JComboBox with array of font sizes 1-100
        {
            jcbSizes.addItem(i);
        }

        p1.add(jlblFontName); // add all 4 components to p1
        p1.add(jcbFonts);
        p1.add(jlblFontSize);
        p1.add(jcbSizes);

        jtxtWelcome = new JTextArea("Welcome to Java", 3, 20);// add a JTextArea
        add(jtxtWelcome);

        p2 = new JPanel(); // create p2 & add JCheckBoxes
        p2.add(jckbBold = new JCheckBox("Bold"));
        jckbBold.setMnemonic('B');
        p2.add(jckbItalic = new JCheckBox("Italic"));
        jckbItalic.setMnemonic('I');

        add(p1,BorderLayout.NORTH);
        add(p2,BorderLayout.SOUTH);

        ListenerClass listener = new ListenerClass();
        jcbFonts.addActionListener(listener);
        jcbSizes.addActionListener(listener);
        jckbBold.addActionListener(listener);
        jckbItalic.addActionListener(listener);
        }

        public static void main(String[] args) 
        {
       SetFontModified frame = new SetFontModified();
       frame.setTitle("Set Font Details");
       frame.pack();
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.setLocationRelativeTo(null);    
       frame.setVisible(true);
    }

    private class ListenerClass implements ActionListener
    {   
        public void actionPerformed(ActionEvent e)
        {   
            jcbSizes.getFontSize();

            Font plainFont = new Font("serif", Font.PLAIN, size);
            Font boldFont = new Font("serif", Font.BOLD, 14);
            Font italicFont = new Font("serif", Font.ITALIC, 14);
            Font boldItalicFont = new Font("serif", Font.BOLD + Font.ITALIC, 14);

            if (jckbBold.isSelected() && jckbItalic.isSelected()) 
                jtxtWelcome.setFont(boldItalicFont);

            else if (jckbBold.isSelected())
                jtxtWelcome.setFont(boldFont);

            else if (jckbItalic.isSelected())
                jtxtWelcome.setFont(italicFont);

            else jtxtWelcome.setFont(plainFont);                
        }
    }
}
4

4 回答 4

1
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Test extends JFrame 
{
    private JPanel p1, p2;
    private JLabel jlblFontName, jlblFontSize;
    private JComboBox jcbFonts, jcbSizes;
    private JTextArea jtxtWelcome;
    private JCheckBox  jckbBold, jckbItalic;

    public Test()                                                              
    {
    p1 = new JPanel();
    jlblFontName = new JLabel("Font Name");                                               

    GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();   
    String[] fontNames = e.getAvailableFontFamilyNames();                                 
    jcbFonts = new JComboBox(fontNames);                                                      

    jlblFontSize = new JLabel("Font Size");                                               // add 2nd JLabel

    jcbSizes = new JComboBox();                                                               // create 2nd JComboBox
    for(int i = 1; i < 101; i ++)                                                                 // populate JComboBox with array of font sizes 1-100
    {
        jcbSizes.addItem(i);
    }

    p1.add(jlblFontName);                                                                         // add all 4 components to p1
    p1.add(jcbFonts);
    p1.add(jlblFontSize);
    p1.add(jcbSizes);

    jtxtWelcome = new JTextArea("Welcome to Java", 3, 20);                            // add a JTextArea
    add(jtxtWelcome);

    p2 = new JPanel();                                                                            // create p2 & add JCheckBoxes
    p2.add(jckbBold = new JCheckBox("Bold"));
    jckbBold.setMnemonic('B');
    p2.add(jckbItalic = new JCheckBox("Italic"));
    jckbItalic.setMnemonic('I');

    add(p1,BorderLayout.NORTH);
    add(p2,BorderLayout.SOUTH);

    ListenerClass listener = new ListenerClass();
    jcbFonts.addActionListener(listener);
    jcbSizes.addActionListener(listener);
    jckbBold.addActionListener(listener);
    jckbItalic.addActionListener(listener);
    }

    public static void main(String[] args) 
    {
   Test frame = new Test();
   frame.setTitle("Set Font Details");
   frame.pack();
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.setLocationRelativeTo(null);    
   frame.setVisible(true);
}

private class ListenerClass implements ActionListener
{   
    public void actionPerformed(ActionEvent e)
    {   
        //jcbSizes.getFontSize();

        Font plainFont = new Font("serif", Font.PLAIN, Integer.parseInt(jcbSizes.getSelectedItem().toString()));
        Font boldFont = new Font("serif", Font.BOLD, 14);
        Font italicFont = new Font("serif", Font.ITALIC, 14);
        Font boldItalicFont = new Font("serif", Font.BOLD + Font.ITALIC, 14);

        if (jckbBold.isSelected() && jckbItalic.isSelected()) 
            jtxtWelcome.setFont(boldItalicFont);

        else if (jckbBold.isSelected())
            jtxtWelcome.setFont(boldFont);

        else if (jckbItalic.isSelected())
            jtxtWelcome.setFont(italicFont);

        else jtxtWelcome.setFont(plainFont);

    }
}
}
于 2013-04-02T15:26:39.953 回答
1

编译器抱怨未定义的变量size。您可以将此声明添加到您的ActionListener

int size = (Integer)jcbSizes.getSelectedItem();
于 2013-04-02T15:26:43.877 回答
1

您需要正确的方法jcbSizes

int size = (Integer) jcbSizes.getSelectedItem();
于 2013-04-02T15:30:21.347 回答
0

但我无法弄清楚如何通过使用 JComoboBoxes 来更改字体和字体大小。

为此,您必须更改 else 块,如下所示。

else {

    // jcbFonts.getSelectedItem().toString()
    // gets font string selected in font combobox
    // -------------------------------------
    // jcbSizes.getSelectedIndex())
    // get font size selected in size combobox

    jtxtWelcome.setFont(new Font(jcbFonts.getSelectedItem()
                    .toString(), Font.PLAIN, jcbSizes.getSelectedIndex()));

    System.out.println("Font Name: " + jcbFonts.getSelectedItem() 
                + " Font Size:" + jcbSizes.getSelectedIndex());
}

类字体 API

于 2013-04-02T15:45:57.317 回答