我正在尝试将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.
提前致谢!