无法改变字体和使用JComboBoxes的JTextArea中的字体大小

时间:2013-04-02 15:18:53

标签: java swing fonts jcombobox font-size

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

    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 个答案:

答案 0 :(得分: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);

    }
}
}

答案 1 :(得分:1)

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

int size = (Integer)jcbSizes.getSelectedItem();

答案 2 :(得分:1)

您需要jcbSizes的正确方法:

int size = (Integer) jcbSizes.getSelectedItem();

答案 3 :(得分: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());
}

Class Font API