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

📄 tij316.htm

📁 这也是我们java老师给我们的thinking in java的一些资料
💻 HTM
📖 第 1 页 / 共 5 页
字号:
    b2 = <font color=#0000ff>new</font> JButton(<font color=#004488>"Button 2"</font>);
  <font color=#0000ff>private</font> JTextField txt = <font color=#0000ff>new</font> JTextField(10);
  <font color=#0000ff>private</font> ActionListener bl = <font color=#0000ff>new</font> ActionListener() {
    <font color=#0000ff>public</font> <font color=#0000ff>void</font> actionPerformed(ActionEvent e) {
      String name = ((JButton)e.getSource()).getText();
      txt.setText(name);
    }
  };
  <font color=#0000ff>public</font> <font color=#0000ff>void</font> init() {
    b1.addActionListener(bl);
    b2.addActionListener(bl);
    Container cp = getContentPane();
    cp.setLayout(<font color=#0000ff>new</font> FlowLayout());
    cp.add(b1);
    cp.add(b2);
    cp.add(txt);
  }
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
    Console.run(<font color=#0000ff>new</font> Button2b(), 200, 75);
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>
<p>The approach of using an anonymous inner class will be preferred (when possible) for the examples in this book. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1961" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h2>
<a name="_Toc375545450"></a><a name="_Toc24775874"></a><a name="Heading18802"></a>Text
areas</h2>
<p>A <a name="Index1705"></a><b>JTextArea</b> is like a <b>JTextField</b> except that it can have multiple lines and has more functionality. A particularly useful method is <b>append(&#160;)</b>; with this you can easily pour output into the <b>JTextArea</b>, thus making a Swing program an improvement (since you can scroll backward) over what has been accomplished thus far using command-line programs that print to standard output. As an example, the following program fills a <b>JTextArea</b> with the output from the <b>geography</b> generator in Chapter 11:<br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c14:TextArea.java</font>
<font color=#009900>// Using the JTextArea control.</font>
<font color=#0000ff>import</font> javax.swing.*;
<font color=#0000ff>import</font> java.awt.event.*;
<font color=#0000ff>import</font> java.awt.*;
<font color=#0000ff>import</font> java.util.*;
<font color=#0000ff>import</font> com.bruceeckel.swing.*;
<font color=#0000ff>import</font> com.bruceeckel.util.*;

<font color=#0000ff>public</font> <font color=#0000ff>class</font> TextArea <font color=#0000ff>extends</font> JFrame {
  <font color=#0000ff>private</font> JButton
    b = <font color=#0000ff>new</font> JButton(<font color=#004488>"Add Data"</font>),
    c = <font color=#0000ff>new</font> JButton(<font color=#004488>"Clear Data"</font>);
  <font color=#0000ff>private</font> JTextArea t = <font color=#0000ff>new</font> JTextArea(20, 40);
  <font color=#0000ff>private</font> Map m = <font color=#0000ff>new</font> HashMap();
  <font color=#0000ff>public</font> TextArea() {
    <font color=#009900>// Use up all the data:</font>
    Collections2.fill(m, Collections2.geography,
      CountryCapitals.pairs.length);
    b.addActionListener(<font color=#0000ff>new</font> ActionListener() {
      <font color=#0000ff>public</font> <font color=#0000ff>void</font> actionPerformed(ActionEvent e) {
        Iterator it = m.entrySet().iterator();
        <font color=#0000ff>while</font>(it.hasNext()) {
          Map.Entry me = (Map.Entry)(it.next());
          t.append(me.getKey() + <font color=#004488>": "</font>+ me.getValue()+<font color=#004488>"\n"</font>);
        }
      }
    });
    c.addActionListener(<font color=#0000ff>new</font> ActionListener() {
      <font color=#0000ff>public</font> <font color=#0000ff>void</font> actionPerformed(ActionEvent e) {
        t.setText(<font color=#004488>""</font>);
      }
    });
    Container cp = getContentPane();
    cp.setLayout(<font color=#0000ff>new</font> FlowLayout());
    cp.add(<font color=#0000ff>new</font> JScrollPane(t));
    cp.add(b);
    cp.add(c);
  }
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
    Console.run(<font color=#0000ff>new</font> TextArea(), 475, 425);
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>
<p>This is a <b>JFrame</b> rather than a <b>JApplet</b> because it reads from the local disk, and therefore cannot be run as an applet in an HTML page. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]A0547" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>In <b>init(&#160;)</b>, the <b>Map</b> is filled with all the countries and their capitals. Note that for both buttons, the <a name="Index1706"></a><b>ActionListener</b> is created and added without defining an intermediate variable, since you never need to refer to that listener again during the program. The &#147;Add Data&#148; button formats and appends all the data, and the &#147;Clear Data&#148; button uses <b>setText(&#160;)</b> to remove all the text from the <b>JTextArea</b>. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1962" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>As the <b>JTextArea</b> is added to the applet, it is wrapped in a <a name="Index1707"></a><a name="Index1708"></a><b>JScrollPane</b> to control scrolling when too much text is placed on the screen. That&#146;s all you must do in order to produce full scrolling capabilities. Having tried to figure out how to do the equivalent in some other GUI programming environments, I am very impressed with the simplicity and good design of components like <b>JScrollPane</b>. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1963" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h2>
<a name="_Toc24775875"></a><a name="Heading18851"></a>Controlling
layout<br></h2>
<p><a name="Index1709"></a>The way that you place components on a form in Java is probably different from any other GUI system you&#146;ve used. First, it&#146;s all code; there are no &#147;resources&#148; that control placement of components. Second, the way components are placed on a form is controlled not by absolute positioning but by a &#147;layout manager&#148; that decides how the components lie based on the order that you <b>add(&#160;)</b> them. The size, shape, and placement of components will be remarkably different from one layout manager to another. In addition, the layout managers adapt to the dimensions of your applet or application window, so if the window dimension is changed, the size, shape, and placement of the components can change in response. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1964" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p><a name="Index1710"></a><a name="Index1711"></a><b>JApplet</b>, <b>JFrame JWindow</b>, and <b>JDialog</b> can all produce a <b>Container </b>with <b>getContentPane(&#160;) </b>that can contain and display <b>Component</b>s. In <b>Container,</b> there&#146;s a method called <a name="Index1712"></a><b>setLayout(&#160;)</b> that allows you to choose a different layout manager. Other classes, such as <a name="Index1713"></a><b>JPanel</b>, contain and display components directly, so you also set the layout manager directly, without using the content pane. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1965" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>In this section we&#146;ll explore the various layout managers by placing buttons in them (since that&#146;s the simplest thing to do). There won&#146;t be any capturing of button events because these examples are just intended to show how the buttons are laid out. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1966" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h3>
<a name="_Toc375545458"></a><a name="_Toc375545459"></a><a name="_Toc24775876"></a><a name="Heading18855"></a>BorderLayout</h3>
<p>Applets use a default layout scheme: the <a name="Index1714"></a><b>BorderLayout</b> (a number of the previous examples have changed the layout manager to <b>FlowLayout</b>). Without any other instruction, this takes whatever you <b>add(&#160;) </b>to it and places it in the center, stretching the object all the way out to the edges. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1967" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>However, there&#146;s more to the <b>BorderLayout</b>. This layout manager has the concept of four border regions and a center area. When you add something to a panel that&#146;s using a <b>BorderLayout</b>, you can use the overloaded <b>add(&#160;)</b> method that takes a constant value as its first argument. This value can be any of the following:<b> </b><br></p>
<div style="position:relative; left: 18"><table border="1">
<tr valign="top">
<td width="215.999946" colspan="1" rowspan="1" valign="top">
<p class="Table"><a name="Index1715"></a><a name="Index1716"></a><a name="Index1717"></a><a name="Index1718"></a><b>BorderLayout. NORTH</b><br></p>
</td>
<td width="239.999940" colspan="1" rowspan="1" valign="top">
<p class="Table">Top<br></p>
</td>
</tr>
<tr valign="top">
<td width="215.999946" colspan="1" rowspan="1" valign="top">
<p class="Table"><b>BorderLayout. SOUTH</b><br></p>
</td>
<td width="239.999940" colspan="1" rowspan="1" valign="top">
<p class="Table">Bottom<br></p>
</td>
</tr>
<tr valign="top">
<td width="215.999946" colspan="1" rowspan="1" valign="top">
<p class="Table"><b>BorderLayout. EAST</b><br></p>
</td>
<td width="239.999940" colspan="1" rowspan="1" valign="top">
<p class="Table">Right<br></p>

⌨️ 快捷键说明

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