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。如果你有問題,然後問,我們會提供幫助。 –