3
我有这段代码,当用户点击其中一个字体“粗体或斜体...”时,文本应该改变。我不能添加动作监听器,将做到这一点:使用菜单更改文本字体
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class question4 extends JFrame {
private JTextField textField;
private JComboBox fontbox;
private String names[]={ "Bold","Italic","Plain"};
private Icon icons[]={};
public question4()
{
super("JcheckBox");
setLayout(new FlowLayout());//set frame
fontbox = new JComboBox(names);//set jcobobox
fontbox.setMaximumRowCount(3);
//listener
add(fontbox);
//add the text content
textField = new JTextField ("Hello World", 20);
textField.setFont(new Font("Calibri", Font.BOLD,18));//set the text font and size
add(textField);//add textfield to jframe
}
public static void main(String args[])
{
question4 obj = new question4();//create object
obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
obj.setSize(700,400);
obj.setVisible(true);
}//end main
}//end class
那你试试?你有什么麻烦? – SLaks
请正确缩进您的代码。 – SLaks
首先阅读如何将[菜单](http://docs.oracle.com/javase/tutorial/uiswing/components/menu.html)添加到JFrame。如果你有问题,然后问,我们会提供帮助。 –