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

📄 front.html

📁 java 基础的 一点东西,,可以看看
💻 HTML
📖 第 1 页 / 共 2 页
字号:
//Add button as an event listener     clickButton.addActionListener(this);//Create panel     panel = new JPanel();//Specify layout manager and background color     panel.setLayout(new BorderLayout(1,1));     panel.setBackground(Color.white);//Add label and button to panel     getContentPane().add(panel);     panel.add(BorderLayout.CENTER, text);     panel.add(BorderLayout.SOUTH, button);   }</PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><BR CLEAR=ALL><IMG SRC="./Art/border2.gif" WIDTH="222" HEIGHT="175" ALT="" ALIGN=LEFT HSPACE=10>When the <CODE>JPanel</CODE> object is created, the layoutmanager and background color are specified.The layout manager in use determines how user interface components are arranged on the display area. <P>The code uses the<CODE>BorderLayout</CODE> layout manager, which arrangesuser interface components in the five areas shown atleft.  To add a component, specify the area (north, south, east, west, or center). <BR CLEAR=ALL></FONT><PRE>//Create panel     panel = new JPanel();//Specify layout manager and background color     panel.setLayout(new BorderLayout(1,1));     panel.setBackground(Color.white);//Add label and button to panel     getContentPane().add(panel);     panel.add(BorderLayout.CENTER, text);     panel.add(BorderLayout.SOUTH, button);   }</PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif">To find out about some of the other available layout managers and how to use them, see the JDC article<A HREF="/developer/technicalArticles/GUI/AWTLayoutMgr/index.html">Exploringthe AWT Layout Managers</A>. <P>The call to the <CODE>getContentPane</CODE> method of the<CODE>JFrame</CODE> class is for adding the <CODE>Panel</CODE>to the <CODE>JFrame</CODE>. Components are not added directly to a<CODE>JFrame</CODE>, but to its content pane.  Because thelayout manager controls the layout of components, it is seton the content pane where the components reside. A contentpane provides functionality that allows different types ofcomponents to work together in Project Swing. <A NAME="listen"></A><H3>Action Listening</H3>In addition to implementing the <CODE>ActionListener</CODE>interface, you have to add the event listener to the JButton components. An action listener is theSwingUI object because it implements the ActionListener interface. In this example,when the end user clicks the button, the underlying Java platform services pass theaction (or event) to the actionPerformed method. In your code, you implement theactionPerformed method to take the appropriate action based on which button isclicked..<P>The component classes have the appropriate add methods to add action listeners to them.In the code the JButton class has an addActionListener method. The parameter passed toaddActionListener is this, which means the SwingUI action listener is added to thebutton so button-generated actions are passed to the actionPerformed method in theSwingUI object. </FONT><PRE>     button = new JButton(&quot;Click Me&quot;);//Add button as an event listener     button.addActionListener(this);</PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><A NAME="event"></A><H3>Event Handling</H3>The actionPerformed method is passed an event object that represents the action eventthat occurred. Next, it uses an if statement to find out which component had the event,and takes action according to its findings. </FONT><PRE>   public void actionPerformed(ActionEvent event){     Object source = event.getSource();        if (_clickMeMode) {          text.setText(&quot;Button Clicked&quot;);          button.setText(&quot;Click Again&quot;);          _clickMeMode = false;        } else {          text.setText(&quot;I'm a Simple Program&quot;);          button.setText(&quot;Click Me&quot;);          _clickMeMode = true;        }   }</PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif">You can find information on event handling for the different components in <A HREF="http://java.sun.com/docs/books/tutorial/index.html">The Java Tutorial</A> section on<A HREF="http://java.sun.com/docs/books/tutorial/ui/swingOverview/event.html">EventHandling</A>.<A NAME="main"></A><H3>Main Method</H3>The <CODE>main</CODE> method creates the top-level <CODE>frame</CODE>, sets the title, and includes code that lets the end user closethe window using the frame menu.</FONT><PRE>public static void main(String[] args){//Create top-level frame  SwingUI frame = new SwingUI();  frame.setTitle(&quot;Example&quot;);//This code lets you close the window  WindowListener l = new WindowAdapter() {     public void windowClosing(WindowEvent e) {       System.exit(0);     }  };    frame.addWindowListener(l);//This code lets you see the frame  frame.pack();  frame.setVisible(true); }}</PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif">The code for closing the window shows an easy way to add event handling functionality to aprogram. If the event listener interface you need provides more functionality than the programactually uses, use an adapter class. The Java APIs provide adapter classes for all listenerinterfaces with more than one method. This way, you can use the adapter class instead of thelistener interface and implement only the methods you need. In the example, the WindowListenerinterface has 7 methods and this program needs only the windowClosing method so it makes senseto use the WindowAdapter class instead. <P>This code extends the WindowAdapter class and overrides the windowClosing method. The newkeyword creates an anonymous instance of the extended inner class. It is anonymous because youare not assigning a name to the class and you cannot create another instance of the classwithout executing the code again. It is an inner class because the extended class definition isnested within the SwingUI class. <P>This approach takes only a few lines of code, while implementing the WindowListener interfacewould require 6 empty method implementations. Be sure to add the WindowAdapter object to theframe object so the frame object will listen for window events.</FONT><PRE>   WindowListener l = new WindowAdapter() { //The instantiation of object l is extended to  //include this code:     public void windowClosing(WindowEvent e){        System.exit(0);     }   };    frame.addWindowListener(l);</PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><A NAME="applets"></A><H3>Applets Revisited</H3>Using what you learned in <A HREF="./Code/SimpleApplet.java">Lesson 3: Building Applets</A>and this lesson, convert the example for <A HREF="./Code/SwingUI.java">this lesson</A> intoan applet. Give it a try before looking at the <A HREF="./Code/ApptoAppl.java">solution</A>.<P><IMG SRC="./Art/apptoappl.gif" WIDTH="212" HEIGHT="187" ALT=""><P>In short, the differences between the applet and application versions are the following:<UL><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">The applet class is declared <CODE>public</CODE> so <CODE>appletviewer</CODE> can access it. </FONT><P><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">The applet class descends from <CODE>Applet</CODE>and the application class descends from <CODE>JFrame</CODE>.</FONT><P><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">The applet version has no <CODE>main</CODE> method. </FONT><P><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">The application constructor is replaced in the applet by <CODE>start</CODE> and <CODE>init</CODE> methods.</FONT><P><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">GUI components are added directly to the <CODE>Applet</CODE>; whereas, in the case of an application, GUI components are added to the content pane of its <CODE>JFrame</CODE> object.</FONT></UL><A NAME="more"></A><H3>More Information</H3>For more information on Project Swing, see the<A HREF="http://java.sun.com/products/jfc/tsc/index.html">SwingConnection</A>,and <A HREF="/developer/onlineTraining/GUI/Swing1/index.html">Fundamentalsof Swing, Part 1</A>.<P>Also see <A HREF="http://java.sun.com/docs/books/tutorial/uiswing/">The JFCProject Swing Tutorial: A Guide to Constructing GUIs</A>.<P>To find out about some of the otheravailable layout managers and how to use them, see the JDC article<A HREF="/developer/technicalArticles/GUI/AWTLayoutMgr/index.html">Exploringthe AWT Layout Managers</A>.<P ALIGN="RIGHT"><FONT SIZE="-1">[<A HREF="#top">TOP</A>]</FONT></FONT></TD></TR></TABLE><!-- ================ --><!-- End Main Content --><!-- ================ --></FONT></TD></TR></TABLE><!-- Copyright Insert --><BR CLEAR="ALL"><FORM ACTION="/cgi-bin/search.cgi" METHOD="POST"><TABLE WIDTH="100%" CELLPADDING="0" BORDER="0" CELLSPACING="5">     <TR>    <TD VALIGN="BOTTOM"></TD></TR><A HREF="/servlet/PrintPageServlet"><IMG SRC="/images/printbutton.gif" WIDTH="155" HEIGHT="25" ALT="Print Button" BORDER="0"></A>	    <CENTER>    <FONT SIZE="-1" COLOR="#999999" FACE="Verdana, Arial, Helvetica, sans-serif">    [ This page was updated: <!-- new date --> 31-Mar-2000 ]</font></CENTER>    </TD>  </TR>    <TR>    <TD BGCOLOR="#CCCCCC">    <IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD>  </TR>    <TR>    <TD>    <CENTER>    <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">    <A HREF="http://java.sun.com/products/">Products &amp; APIs</A> |     <A HREF="/developer/index.html">Developer Connection</A> |     <A HREF="/developer/infodocs/index.shtml">Docs &amp; Training</A> |     <A HREF="/developer/support/index.html">Online Support</A><BR>    <A HREF="/developer/community/index.html">Community Discussion</A> |    <A HREF="http://java.sun.com/industry/">Industry News</A> |     <A HREF="http://java.sun.com/solutions">Solutions Marketplace</A> |     <A HREF="http://java.sun.com/casestudies">Case Studies</A>    </FONT>    </CENTER>    </TD>  </TR>    <TR>    <TD BGCOLOR="#CCCCCC">    <IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD>  </TR>  <TR>    <TD ALIGN="CENTER">    <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">    <A HREF="http://java.sun.com/docs/glossary.html">Glossary</A> -     <A HREF="http://java.sun.com/applets/">Applets</A> -     <A HREF="http://java.sun.com/docs/books/tutorial/">Tutorial</A> -     <A HREF="http://java.sun.com/jobs/">Employment</A> -     <A HREF="http://java.sun.com/nav/business/">Business &amp; Licensing</A> -     <A HREF="http://java.sun.com/javastore/">Java Store</A> -    <A HREF="http://java.sun.com/casestudies/">Java in the Real World</A>    </FONT>    </TD>  </TR>  <TR>    <TD>    <CENTER>    <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">    <a href="/siteinfo/faq.html">FAQ</a> |    <a href="/feedback/index.html">Feedback</a> |     <a href="http://www.dynamicdiagrams.net/mapa/cgi-bin/help.tcl?db=javasoft&dest=http://java.sun.com/">Map</a> |     <A HREF="http://java.sun.com/a-z/index.html">A-Z Index</A>    </FONT>    </CENTER>    </TD>  </TR>    <TR>    <TD>    <TABLE WIDTH="100%" CELLPADDING="0" BORDER="0" CELLSPACING="0">      <TR>        <TD WIDTH="50%">        <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">        For more information on Java technology<BR>        and other software from Sun Microsystems, call:<BR>        </FONT>        <FONT SIZE="-1" FACE="Verdana, Arial, Helvetica, sans-serif">        (800) 786-7638<BR></FONT>        <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">        Outside the U.S. and Canada, dial your country's         <A HREF="http://www.att.com/business_traveler/attdirecttollfree/">AT&amp;T&nbsp;Direct&nbsp;Access&nbsp;Number</A> first.<BR>        </FONT>        </TD>        <TD ALIGN="RIGHT" WIDTH="50%">        <A HREF="http://www.sun.com"><IMG SRC="/images/lgsun.gif" width="64" height="30" border="0" ALT="Sun Microsystems, Inc."></A><BR>        <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">        Copyright &copy; 1995-2000        <A HREF="http://www.sun.com">Sun Microsystems, Inc.</A><BR>        All Rights Reserved.         <A HREF="http://www.sun.com/share/text/termsofuse.html">Terms of Use</A>.         <A HREF="http://www.sun.com/privacy/">Privacy&nbsp;Policy</A>.        </FONT>        </TD>      </TR>    </TABLE>	    </TD>  </TR> </TABLE></FORM><!-- End Copyright Insert --></BODY></HTML>

⌨️ 快捷键说明

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