图11(点击放大) (五)设计Choice对象
在此步骤中,我们将在Choice对象内加入两个选项,并加入必要的程序代码。
1、点击编辑器下方的Source页签,切换到源代码编辑画面。
2、点击“结构窗格”的init()方法,JBuilder 2005会自动将光标移到init()方法的声明位置。
3、在init()方法内加入下列两行程序代码,用蓝颜色标识的(位于try/catch区块上方):(如下图12所示)
图12(点击放大)
--------------------------------------------------- choice1.addItem("English"); choice1.addItem("Chinese"); -------------------------------------------------- 说明:additem()方法可以为Choice对象加入一个选项。
4、点击Design页签回到UI设计工具。
5、在“结构窗格”内点击choice1节点,然后选取“查看器”的Event页签。
6、以鼠标左键双击itemStateChaoged事件,JBuilder会在HelloApplet.java内新增一个choice1_itemStateChanged()方法,并将光标移到该方法所在位置。
7、参考下列范例撰写choice1_itemstateChanged()方法的内容,用蓝颜色标识的(如下图13所示)。
------------------------------------------------------------------------------------ public void choice1_itemStateChanged(ItemEvent e) { if ("English".equals(choice1.getSelectedItem())){ cardLayout1.show(LowerPanel,"panel1"); } else if ("Chinese".equals(choice1.getSelectedItem())){ cardLayout1.show(LowerPanel,"panel2"); } } -------------------------------------------------------------------------------------
图13 当HellloApplet被执行时,如果Choice选项为“English”,LowerPanel对象内将显示“Good Movning!”字符串(包含于panel1容器);如果Choice选项为“Chinese”,LowerPanel对象内将显示“早上好”字符串(包含于panel2容器)。
8、保存JBuilder工程,编译并执行HelloApplet,执行结果如图14所示。
图14 9、将[Select a language]旁边的下拉式菜单(Choice对象)切换至“Chinese”,我们将发现下方的信息更改为“早上好”。如图15所示。
图15 Applet1.java源代码如下:
package helloapplet;
import java.awt.*; import java.awt.event.*; import java.applet.*;
/** * <p>Title: Study</p> * * <p>Description: This is a Stendent</p> * * <p>Copyright: Copyright (c) 2004</p> * * <p>Company: </p> * * @author ghq * @version 1.0 */
public class Applet1 extends Applet { boolean isStandalone = false; BorderLayout borderLayout1 = new BorderLayout(); BorderLayout borderLayout2 = new BorderLayout(); Panel UpperPanel = new Panel(); Panel LowerPanel = new Panel(); CardLayout cardLayout1 = new CardLayout(); Panel panel1 = new Panel(); BorderLayout borderLayout3 = new BorderLayout(); Panel panel2 = new Panel(); BorderLayout borderLayout4 = new BorderLayout(); Choice choice1 = new Choice(); Label label1 = new Label(); Label label2 = new Label(); Label label3 = new Label(); //Get a parameter value public String getParameter(String key, String def) { return isStandalone ? System.getProperty(key, def) : (getParameter(key) != null ? getParameter(key) : def); }
//Construct the applet public Applet1() {}
//Initialize the applet public void init() { choice1.addItem("English"); choice1.addItem("Chinese"); try { jbInit(); } catch (Exception e) { e.printStackTrace(); } }
//Component initialization private void jbInit() throws Exception { this.setLayout(borderLayout2); UpperPanel.setForeground(Color.lightGray); LowerPanel.setForeground(Color.white); LowerPanel.setLayout(cardLayout1); panel1.setLayout(borderLayout3); panel2.setForeground(Color.pink); panel2.setLayout(borderLayout4); panel1.setForeground(Color.white); label1.setFont(new java.awt.Font("Dialog", Font.PLAIN, 20)); label1.setForeground(Color.blue); label1.setText(""Select a Language:""); label2.setAlignment(Label.CENTER); label2.setFont(new java.awt.Font("Dialog", Font.PLAIN, 24)); label2.setText(""Good Movning!""); label3.setAlignment(Label.CENTER); label3.setFont(new java.awt.Font("Dialog", Font.PLAIN, 24)); label3.setText(""早上好""); choice1.addItemListener(new Applet1_choice1_itemAdapter(this)); this.add(UpperPanel, java.awt.BorderLayout.NORTH); UpperPanel.add(label1); UpperPanel.add(choice1); this.add(LowerPanel, java.awt.BorderLayout.CENTER); LowerPanel.add(panel1, "panel1"); LowerPanel.add(panel2, "panel2"); panel1.add(label2, java.awt.BorderLayout.CENTER); panel2.add(label3, java.awt.BorderLayout.CENTER); }
//Get Applet information public String getAppletInfo() { return "Applet Information"; }
//Get parameter info public String[][] getParameterInfo() { return null; }
public void choice1_itemStateChanged(ItemEvent e) { if ("English".equals(choice1.getSelectedItem())){ cardLayout1.show(LowerPanel,"panel1"); } else if ("Chinese".equals(choice1.getSelectedItem())){ cardLayout1.show(LowerPanel,"panel2"); } } }
class Applet1_choice1_itemAdapter implements ItemListener { private Applet1 adaptee; Applet1_choice1_itemAdapter(Applet1 adaptee) { this.adaptee = adaptee; }
public void itemStateChanged(ItemEvent e) { adaptee.choice1_itemStateChanged(e); } }
上一页 [1] [2] [3] [4] [5] [6] [7] 下一页
|