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

📄 tij0147.html

📁 学习java的经典书籍
💻 HTML
📖 第 1 页 / 共 3 页
字号:
is made up of 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>MenuItem</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">s,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>CheckboxMenuItem</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">s,
or even other 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Menu</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">s
(which produce submenus). When a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>MenuBar</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is assembled it can be installed into the current program with the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>setMenuBar(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method. Note that when the button is pressed, it checks to see which menu is
currently installed using 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>getMenuBar(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
then puts the other menu bar in its place.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">When
testing for &#8220;Open,&#8221; notice that spelling and capitalization are
critical, but Java signals no error if there is no match with
&#8220;Open.&#8221; This kind of string comparison is a clear source of
programming errors.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
checking and un-checking of the menu items is taken care of automatically, but
dealing with CheckboxMenuItems can be a bit surprising since for some reason
they don&#8217;t allow string matching. (Although string matching isn&#8217;t a
good approach, this seems inconsistent.) So you can match only the target
object and not its label. As shown, the <A NAME="Index1777"></A><A NAME="Index1778"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>getState(&#160;)
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">method
can be used to reveal the state. You can also change the state of a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>CheckboxMenuItem</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
with 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>setState(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">You
might think that one menu could reasonably reside on more than one menu bar.
This does seem to make sense because all you&#8217;re passing to the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>MenuBar</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>add(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method is a handle. However, if you try this, the behavior will be strange and
not what you expect. (It&#8217;s difficult to know if this is a bug or if they
intended it to work this way.)
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">This
example also shows what you need to do to create an application instead of an
applet. (Again, because an application can support menus and an applet cannot
directly have a menu.) Instead of inheriting from 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Applet</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
you inherit from 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Frame</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
Instead of 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>init(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to set things up, you make a constructor for your class. Finally, you create a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>main(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and in that you build an object of your new type, resize it, and then call 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>show(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
It&#8217;s different from an applet in only a few small places, but it&#8217;s
now a <A NAME="Index1779"></A><A NAME="Index1780"></A><A NAME="Index1781"></A>standalone
windowed application and you&#8217;ve got menus.
</FONT><a name="_Toc375545468"></a><a name="_Toc408018704"></a><P></DIV>
<A NAME="Heading421"></A><H3 ALIGN=LEFT>
Dialog
boxes
</H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">A
<A NAME="Index1782"></A><A NAME="Index1783"></A>dialog
box is a window that pops up out of another window. Its purpose is to deal with
some specific issue without cluttering the original window with those details.
Dialog boxes are heavily used in windowed programming environments, but as
mentioned previously, rarely used in applets.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">To
create a dialog box, you inherit from <A NAME="Index1784"></A><A NAME="Index1785"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Dialog</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
which is just another kind of 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Window</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
like a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Frame</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
Unlike a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Frame</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Dialog</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
cannot have a menu bar or change the cursor, but other than that they&#8217;re
quite similar. A dialog has a layout manager (which defaults to 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>BorderLayout</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">)
and you override 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>action(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
etc., or 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>handleEvent(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to deal with events. One significant difference you&#8217;ll want to note in 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>handleEvent(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">:
when the <A NAME="Index1786"></A><A NAME="Index1787"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>WINDOW_DESTROY</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
event occurs, you don&#8217;t want to shut down the application! Instead, you
release the resources used by the dialog&#8217;s window by calling <A NAME="Index1788"></A><A NAME="Index1789"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>dispose(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">In
the following example, the dialog box is made up of a grid (using 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>GridLayout</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">)
of a special kind of button that is defined here as class 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>ToeButton</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
This button draws a frame around itself and, depending on its state, a blank,
an &#8220;x,&#8221; or an &#8220;o&#8221; in the middle. It starts out blank,
and then depending on whose turn it is, changes to an &#8220;x&#8221; or an
&#8220;o.&#8221; However, it will also flip back and forth between
&#8220;x&#8221; and &#8220;o&#8221; when you click on the button. (This makes
the tic-tac-toe concept only slightly more annoying than it already is.) In
addition, the dialog box can be set up for any number of rows and columns by
changing numbers in the main application window.
</FONT><P></DIV>

<font color="#990000"><PRE><font color="#009900">//: ToeTest.java</font>
<font color="#009900">// Demonstration of dialog boxes</font>
<font color="#009900">// and creating your own components</font>
<font color="#0000ff">import</font> java.awt.*;

<font color="#0000ff">class</font> ToeButton <font color="#0000ff">extends</font> Canvas {
  <font color="#0000ff">int</font> state = ToeDialog.BLANK;
  ToeDialog parent;
  ToeButton(ToeDialog parent) {
    <font color="#0000ff">this</font>.parent = parent;
  }
  <font color="#0000ff">public</font> <font color="#0000ff">void</font> paint(Graphics  g) {
    <font color="#0000ff">int</font> x1 = 0;
    <font color="#0000ff">int</font> y1 = 0;
    <font color="#0000ff">int</font> x2 = size().width - 1;
    <font color="#0000ff">int</font> y2 = size().height - 1;
    g.drawRect(x1, y1, x2, y2);
    x1 = x2/4;
    y1 = y2/4;
    <font color="#0000ff">int</font> wide = x2/2;
    <font color="#0000ff">int</font> high = y2/2;
    <font color="#0000ff">if</font>(state == ToeDialog.XX) {
      g.drawLine(x1, y1, x1 + wide, y1 + high);
      g.drawLine(x1, y1 + high, x1 + wide, y1);
    }
    <font color="#0000ff">if</font>(state == ToeDialog.OO) {
      g.drawOval(x1, y1, x1+wide/2, y1+high/2);
    }
  }
  <font color="#0000ff">public</font> <font color="#0000ff">boolean</font> 
  mouseDown(Event evt, <font color="#0000ff">int</font> x, <font color="#0000ff">int</font> y) {
    <font color="#0000ff">if</font>(state == ToeDialog.BLANK) {
      state = parent.turn;
      parent.turn= (parent.turn == ToeDialog.XX ?
        ToeDialog.OO : ToeDialog.XX);
    } 
    <font color="#0000ff">else</font>
      state = (state == ToeDialog.XX ? 
        ToeDialog.OO : ToeDialog.XX);
    repaint();
    <font color="#0000ff">return</font> <font color="#0000ff">true</font>;
  }
}

<font color="#0000ff">class</font> ToeDialog <font color="#0000ff">extends</font> Dialog {
  <font color="#009900">// w = number of cells wide</font>
  <font color="#009900">// h = number of cells high</font>
  <font color="#0000ff">static</font> <font color="#0000ff">final</font> <font color="#0000ff">int</font> BLANK = 0;
  <font color="#0000ff">static</font> <font color="#0000ff">final</font> <font color="#0000ff">int</font> XX = 1;
  <font color="#0000ff">static</font> <font color="#0000ff">final</font> <font color="#0000ff">int</font> OO = 2;
  <font color="#0000ff">int</font> turn = XX; <font color="#009900">// Start with x's turn</font>
  <font color="#0000ff">public</font> ToeDialog(Frame parent, <font color="#0000ff">int</font> w, <font color="#0000ff">int</font> h) {
    <font color="#0000ff">super</font>(parent, "The game itself", <font color="#0000ff">false</font>);
    setLayout(<font color="#0000ff">new</font> GridLayout(w, h));
    <font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i &lt; w * h; i++)
      add(<font color="#0000ff">new</font> ToeButton(<font color="#0000ff">this</font>));
    resize(w * 50, h * 50);
  }
  <font color="#0000ff">public</font> <font color="#0000ff">boolean</font> handleEvent(Event evt) {
    <font color="#0000ff">if</font>(evt.id == Event.WINDOW_DESTROY) 
      dispose();
    <font color="#0000ff">else</font> 
      <font color="#0000ff">return</font> <font color="#0000ff">super</font>.handleEvent(evt);
    <font color="#0000ff">return</font> <font color="#0000ff">true</font>;
  }
}

<font color="#0000ff">public</font> <font color="#0000ff">class</font> ToeTest <font color="#0000ff">extends</font> Frame {
  TextField rows = <font color="#0000ff">new</font> TextField("3");
  TextField cols = <font color="#0000ff">new</font> TextField("3");
  <font color="#0000ff">public</font> ToeTest() {
    setTitle("Toe Test");
    Panel p = <font color="#0000ff">new</font> Panel();
    p.setLayout(<font color="#0000ff">new</font> GridLayout(2,2));
    p.add(<font color="#0000ff">new</font> Label("Rows", Label.CENTER));
    p.add(rows);
    p.add(<font color="#0000ff">new</font> Label("Columns", Label.CENTER));
    p.add(cols);
    add("North", p);
    add("South", <font color="#0000ff">new</font> Button("go"));
  }
  <font color="#0000ff">public</font> <font color="#0000ff">boolean</font> handleEvent(Event evt) {
    <font color="#0000ff">if</font>(evt.id == Event.WINDOW_DESTROY) 
      System.exit(0);
    <font color="#0000ff">else</font> 
      <font color="#0000ff">return</font> <font color="#0000ff">super</font>.handleEvent(evt);
    <font color="#0000ff">return</font> <font color="#0000ff">true</font>;
  }
  <font color="#0000ff">public</font> <font color="#0000ff">boolean</font> action(Event evt, Object arg) {
    <font color="#0000ff">if</font>(arg.equals("go")) {
      Dialog d = <font color="#0000ff">new</font> ToeDialog(
        <font color="#0000ff">this</font>, 
        Integer.parseInt(rows.getText()),
        Integer.parseInt(cols.getText()));
      d.show();
    } 
    <font color="#0000ff">else</font> 
      <font color="#0000ff">return</font> <font color="#0000ff">super</font>.action(evt, arg);
    <font color="#0000ff">return</font> <font color="#0000ff">true</font>;
  }
  <font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> main(String[] args) {
    Frame f = <font color="#0000ff">new</font> ToeTest();
    f.resize(200,100);
    f.show();
  }
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>ToeButton</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
class keeps a handle to its parent, which must be of type 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>ToeDialog</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
As before, this introduces high coupling because a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>ToeButton</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
can be used only with a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>ToeDialog</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,

⌨️ 快捷键说明

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