⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tij0151.html

📁 学习java的经典书籍
💻 HTML
📖 第 1 页 / 共 5 页
字号:
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Object</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
(that way you can put anything you want into the array). This array is
organized so that the first row represents the menu name, and the remaining
rows represent the menu items and their characteristics. You&#8217;ll notice
the rows of the array do not have to be uniform from one to the next &#8211; as
long as your code knows where everything should be, each row can be completely
different.
</FONT><P></DIV>

<font color="#990000"><PRE><font color="#009900">//: Menus.java</font>
<font color="#009900">// A menu-building system; also demonstrates</font>
<font color="#009900">// icons in labels and menu items.</font>
<font color="#0000ff">package</font> c13.swing;
<font color="#0000ff">import</font> java.awt.*;
<font color="#0000ff">import</font> java.awt.event.*;
<font color="#0000ff">import</font> com.sun.java.swing.*;

<font color="#0000ff">public</font> <font color="#0000ff">class</font> Menus <font color="#0000ff">extends</font> JPanel {
  <font color="#0000ff">static</font> <font color="#0000ff">final</font> Boolean
    bT = <font color="#0000ff">new</font> Boolean(<font color="#0000ff">true</font>), 
    bF = <font color="#0000ff">new</font> Boolean(<font color="#0000ff">false</font>);
  <font color="#009900">// Dummy class to create type identifiers:</font>
  <font color="#0000ff">static</font> <font color="#0000ff">class</font> MType { MType(<font color="#0000ff">int</font> i) {} };
  <font color="#0000ff">static</font> <font color="#0000ff">final</font> MType
    mi = <font color="#0000ff">new</font> MType(1), <font color="#009900">// Normal menu item</font>
    cb = <font color="#0000ff">new</font> MType(2), <font color="#009900">// Checkbox menu item</font>
    rb = <font color="#0000ff">new</font> MType(3); <font color="#009900">// Radio button menu item</font>
  JTextField t = <font color="#0000ff">new</font> JTextField(10);
  JLabel l = <font color="#0000ff">new</font> JLabel("Icon Selected", 
    Faces.faces[0], JLabel.CENTER);
  ActionListener a1 = <font color="#0000ff">new</font> ActionListener() {
    <font color="#0000ff">public</font> <font color="#0000ff">void</font> actionPerformed(ActionEvent e) {
      t.setText(
        ((JMenuItem)e.getSource()).getText());
    }
  };
  ActionListener a2 = <font color="#0000ff">new</font> ActionListener() {
    <font color="#0000ff">public</font> <font color="#0000ff">void</font> actionPerformed(ActionEvent e) {
      JMenuItem mi = (JMenuItem)e.getSource();
      l.setText(mi.getText());
      l.setIcon(mi.getIcon());
    }
  };
  <font color="#009900">// Store menu data as "resources":</font>
  <font color="#0000ff">public</font> Object[][] fileMenu = {
    <font color="#009900">// Menu name and accelerator:</font>
    { "File", <font color="#0000ff">new</font> Character('F') },
    <font color="#009900">// Name type accel listener enabled</font>
    { "New", mi, <font color="#0000ff">new</font> Character('N'), a1, bT },
    { "Open", mi, <font color="#0000ff">new</font> Character('O'), a1, bT },
    { "Save", mi, <font color="#0000ff">new</font> Character('S'), a1, bF },
    { "Save As", mi, <font color="#0000ff">new</font> Character('A'), a1, bF},
    { <font color="#0000ff">null</font> }, <font color="#009900">// Separator</font>
    { "Exit", mi, <font color="#0000ff">new</font> Character('x'), a1, bT },
  };
  <font color="#0000ff">public</font> Object[][] editMenu = {
    <font color="#009900">// Menu name:</font>
    { "Edit", <font color="#0000ff">new</font> Character('E') },
    <font color="#009900">// Name type accel listener enabled</font>
    { "Cut", mi, <font color="#0000ff">new</font> Character('t'), a1, bT },
    { "Copy", mi, <font color="#0000ff">new</font> Character('C'), a1, bT },
    { "Paste", mi, <font color="#0000ff">new</font> Character('P'), a1, bT },
    { <font color="#0000ff">null</font> }, <font color="#009900">// Separator</font>
    { "Select All", mi,<font color="#0000ff">new</font> Character('l'),a1,bT},
  };
  <font color="#0000ff">public</font> Object[][] helpMenu = {
    <font color="#009900">// Menu name:</font>
    { "Help", <font color="#0000ff">new</font> Character('H') },
    <font color="#009900">// Name type accel listener enabled</font>
    { "Index", mi, <font color="#0000ff">new</font> Character('I'), a1, bT },
    { "Using help", mi,<font color="#0000ff">new</font> Character('U'),a1,bT},
    { <font color="#0000ff">null</font> }, <font color="#009900">// Separator</font>
    { "About", mi, <font color="#0000ff">new</font> Character('t'), a1, bT },
  };
  <font color="#0000ff">public</font> Object[][] optionMenu = {
    <font color="#009900">// Menu name:</font>
    { "Options", <font color="#0000ff">new</font> Character('O') },
    <font color="#009900">// Name type accel listener enabled</font>
    { "Option 1", cb, <font color="#0000ff">new</font> Character('1'), a1,bT},
    { "Option 2", cb, <font color="#0000ff">new</font> Character('2'), a1,bT},
  };
  <font color="#0000ff">public</font> Object[][] faceMenu = {
    <font color="#009900">// Menu name:</font>
    { "Faces", <font color="#0000ff">new</font> Character('a') },
    <font color="#009900">// Optinal last element is icon</font>
    { "Face 0", rb, <font color="#0000ff">new</font> Character('0'), a2, bT, 
      Faces.faces[0] },
    { "Face 1", rb, <font color="#0000ff">new</font> Character('1'), a2, bT, 
      Faces.faces[1] },
    { "Face 2", rb, <font color="#0000ff">new</font> Character('2'), a2, bT, 
      Faces.faces[2] },
    { "Face 3", rb, <font color="#0000ff">new</font> Character('3'), a2, bT, 
      Faces.faces[3] },
    { "Face 4", rb, <font color="#0000ff">new</font> Character('4'), a2, bT, 
      Faces.faces[4] },
  };
  <font color="#0000ff">public</font> Object[] menuBar = {
    fileMenu, editMenu, faceMenu, 
    optionMenu, helpMenu,
  };
  <font color="#0000ff">static</font> <font color="#0000ff">public</font> JMenuBar
  createMenuBar(Object[] menuBarData) {
    JMenuBar menuBar = <font color="#0000ff">new</font> JMenuBar();
    <font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i &lt; menuBarData.length; i++)
      menuBar.add(
        createMenu((Object[][])menuBarData[i]));
    <font color="#0000ff">return</font> menuBar;
  }
  <font color="#0000ff">static</font> ButtonGroup bgroup;
  <font color="#0000ff">static</font> <font color="#0000ff">public</font> JMenu 
  createMenu(Object[][] menuData) {
    JMenu menu = <font color="#0000ff">new</font> JMenu();
    menu.setText((String)menuData[0][0]);
    menu.setKeyAccelerator(
      ((Character)menuData[0][1]).charValue());
    <font color="#009900">// Create redundantly, in case there are</font>
    <font color="#009900">// any radio buttons:</font>
    bgroup = <font color="#0000ff">new</font> ButtonGroup();
    <font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 1; i &lt; menuData.length; i++) {
      <font color="#0000ff">if</font>(menuData[i][0] == <font color="#0000ff">null</font>)
        menu.add(<font color="#0000ff">new</font> JSeparator());
      <font color="#0000ff">else</font>
        menu.add(createMenuItem(menuData[i]));
    }
    <font color="#0000ff">return</font> menu;
  }
  <font color="#0000ff">static</font> <font color="#0000ff">public</font> JMenuItem 
  createMenuItem(Object[] data) {
    JMenuItem m = <font color="#0000ff">null</font>;
    MType type = (MType)data[1];
    <font color="#0000ff">if</font>(type == mi)
      m = <font color="#0000ff">new</font> JMenuItem();
    <font color="#0000ff">else</font> <font color="#0000ff">if</font>(type == cb)
      m = <font color="#0000ff">new</font> JCheckBoxMenuItem();
    <font color="#0000ff">else</font> <font color="#0000ff">if</font>(type == rb) {
      m = <font color="#0000ff">new</font> JRadioButtonMenuItem();
      bgroup.add(m);
    }
    m.setText((String)data[0]);
    m.setKeyAccelerator(
      ((Character)data[2]).charValue());
    m.addActionListener(
      (ActionListener)data[3]);
    m.setEnabled(
      ((Boolean)data[4]).booleanValue());
    <font color="#0000ff">if</font>(data.length == 6)
      m.setIcon((Icon)data[5]);
    <font color="#0000ff">return</font> m;
  }
  Menus() {
    setLayout(<font color="#0000ff">new</font> BorderLayout());
    add(createMenuBar(menuBar), 
      BorderLayout.NORTH);
    JPanel p = <font color="#0000ff">new</font> JPanel();
    p.setLayout(<font color="#0000ff">new</font> BorderLayout());
    p.add(t, BorderLayout.NORTH);
    p.add(l, BorderLayout.CENTER);
    add(p, BorderLayout.CENTER);
  }
  <font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> main(String args[]) {
    Show.inFrame(<font color="#0000ff">new</font> Menus(), 300, 200);
  }
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
goal is to allow the programmer to simply create tables to represent each menu,
rather than typing lines of code to build the menus. Each table produces one
menu, and the first row in the table contains the menu name and its keyboard
accelerator. The remaining rows contain the data for each menu item: the string
to be placed on the menu item, what type of menu item it is, its keyboard
accelerator, the actionlistener that is fired when this menu item is selected,
and whether this menu item is enabled. If a row starts with 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>null</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
it is treated as a separator.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">To
prevent wasteful and tedious multiple creations of 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Boolean</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
objects and type flags, these are created as 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static
final
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
values at the beginning of the class: 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>bT</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>bF</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to represent 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Boolean</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">s
and different objects of the dummy class 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>MType</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to describe normal menu items (
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>mi</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">),
checkbox menu items (
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>cb</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">),
and radio button menu items (
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>rb</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">).
Remember that an array of 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Object</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
may hold only
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>
Object
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
handles and not primitive values.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">This
example also shows how <A NAME="Index2336"></A><A NAME="Index2337"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>JLabel</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">s
and <A NAME="Index2338"></A><A NAME="Index2339"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>JMenuItems</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
(and their descendants) may hold 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Icon</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">s.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -