0

我正在尝试将JTextArea字体更改为 3 JComboBoxes,即字体、样式和大小以及一个按钮。用户将从中选择所需的选项JComboBoxes并单击确定。OK 按钮在ActionListener这里

final JDialog dialog = new JDialog();
JPanel  dpanel = new JPanel();
dialog.add(dpanel);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fontnames = ge.getAvailableFontFamilyNames();    

JLabel label = new JLabel("         Font                                     Size                   Style");
dpanel.add(label);

final JComboBox font,style,size;
font = new JComboBox(fontnames);
dpanel.add(font);

style = new JComboBox();
style.addItem("Bold");
style.addItem("Italic");
style.addItem("Bold and Italic");
style.addItem("Plain");
dpanel.add(style);

size = new JComboBox();
for(int i=0;i<=100;i++)
{
    size.addItem(i);
}
size.setSelectedItem("22");
dpanel.add(size);

JButton button = new JButton("Done");
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
    String fontvalue = (String) font.getSelectedItem();
    int stylevalue = (Integer) style.getSelectedItem();
    int sizevalue = (Integer) size.getSelectedItem();

    if(style.getSelectedItem().equals("Bold"))
    {
        stylevalue = Font.BOLD;
    }
    else if(style.getSelectedItem().equals("Italic"))
    {
        stylevalue = Font.ITALIC;
    }
    else if(style.getSelectedItem().equals("Bold and Italic"))
    {
        stylevalue = Font.BOLD|Font.ITALIC;
    }
    else
    {
        stylevalue = Font.PLAIN;
    }
    Font areafont = new Font(fontvalue,stylevalue,sizevalue);
    area.setFont(areafont);
    dialog.dispose();        
}
});
dpanel.add(button);
dialog.setTitle("Fonts");
dialog.setSize(400,200);
dialog.setLocation(400,200);
dialog.setVisible(true);
dialog.setResizable(false);

请帮帮我,因为它不能正常工作。它抛出异常Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer.

提前致谢!

4

4 回答 4

1

StyleValue 是一个字符串,您正在尝试将其转换为整数。

public void actionPerformed(ActionEvent ae)
    {
        String fontvalue = (String) font.getSelectedItem();
        int stylevalue = 0;
        int sizevalue = (Integer) size.getSelectedItem();

        if(style.getSelectedItem().equals("Bold"))
        {
            stylevalue = Font.BOLD;
        }
        else if(style.getSelectedItem().equals("Italic"))
        {
            stylevalue = Font.ITALIC;
        }
        else if(style.getSelectedItem().equals("Bold and Italic"))
        {
            stylevalue = Font.BOLD|Font.ITALIC;
        }
        else
        {
            stylevalue = Font.PLAIN;
        }
        Font areafont = new Font(fontvalue,stylevalue,sizevalue);
        //area.setFont(areafont);
        dialog.dispose();        
    }
于 2013-10-01T10:35:46.637 回答
1

您将样式定义为

style = new JComboBox();
style.addItem("Bold");
style.addItem("Italic");
style.addItem("Bold and Italic");
style.addItem("Plain");

但是您正在使用检索值

int stylevalue = (Integer) style.getSelectedItem();

getSelectedItem将返回一个String值,该值不能转换为int

这很奇怪,因为您似乎无论如何都忽略了返回值并计算其结果

于 2013-10-01T10:38:30.080 回答
0

sizeValue 是字符串,您将其转换为整数。这就是异常的原因。纠正它。

于 2013-10-01T10:37:07.860 回答
0

来自 java 文档

public Object getSelectedItem()

Returns the current selected item.

If the combo box is editable, then this value may not have been added to the combo box with addItem, insertItemAt or the data constructors.

Returns:
    the current selected Object

在代码的这一行 int stylevalue = (Integer) style.getSelectedItem();中,您尝试将 String 强制转换为 int ,这会引发异常

于 2013-10-01T10:39:41.950 回答