Java All-in-One Desk Reference For Dummies phần 8 ppsx

89 278 0
Java All-in-One Desk Reference For Dummies phần 8 ppsx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Book VI Chapter 5 Using Layout Managers Using GridBag Layout 597 ✦ You also want to set the anchor field to indicate where you want the component placed if it doesn’t fill the cell or cells allotted to it. Working with GridBagConstraints To create a GridBagConstraint object, you call the GridBagConstraint constructor, and then set any of the fields that you want to vary from the default values. For example, here’s code that creates a GridBagConstraint object to add the name text field that is shown earlier in Figure 5-5: GridBagConstraints nameConstraints = new GridBagConstraints(); nameConstraints.gridx = 1; nameConstraints.gridy = 0; nameConstraints.gridwidth = 2; nameConstraints.gridheight = 1; nameConstraints.weightx = 100.0; nameConstraints.weighty = 100.0; nameConstraints.insets = new Insets(5, 5, 5, 5); nameConstraints.anchor = GridBagConstraints.WEST; nameConstraints.fill = GridBagConstraints.NONE; Then, you can call the add method to add the name text field to the panel: panel1.add(name, nameConstraints); Obviously, this approach to controlling constraints is going to require a lot of coding. You have two common alternatives to creating a new constraint object for every component you add to the panel. The first is to create a single constraint object and reuse it for all the components in the panel. Then, you simply change the fields that need to be changed for each compo- nent. For example, here’s code that adds all three text fields using a single constraint object: GridBagConstraints gc = new GridBagConstraints(); gc.gridx = 0; gc.gridy = 0; gc.gridwidth = 1; gc.gridheight = 1; gc.weightx = 100.0; gc.weighty = 100.0; gc.insets = new Insets(5, 5, 5, 5); gc.anchor = GridBagConstraints.WEST; gc.fill = GridBagConstraints.NONE; gc.gridy = 0; gc.gridwidth = 2; 43_58961X bk06ch05.qxd 3/29/05 3:41 PM Page 597 Using GridBag Layout 598 add(name, gc); gc.gridy = 1; gc.gridwidth = 1; add(phone, gc); gc.gridy = 2; gc.gridwidth = 2; add(address, gc); Here, the first group of statements creates a GridBagConstraints object named gc and sets its values to the defaults that I want to apply to most of the components in the panel. Then, the second group of statements sets the gridy and gridwidth fields before adding each text field to the panel. The second option is to create a helper method that you can call, passing just the values that vary for each component. For example, here’s a method named addItem that adds a component and left aligns it within the speci- fied cells: private void addItem(JPanel p, JComponent c, int x, int y, int width, int height, int align) { GridBagConstraints gc = new GridBagConstraints(); gc.gridx = x; gc.gridy = y; gc.gridwidth = width; gc.gridheight = height; gc.weightx = 100.0; gc.weighty = 100.0; gc.insets = new Insets(5, 5, 5, 5); gc.anchor = align; gc.fill = GridBagConstraints.NONE; p.add(c, gc); } Then, you can call this method to add a component to the panel. You must pass the panel and the component, its x and y position, and its width and height. For example, here’s how you add the name text field: addItem(panel1, name, 0, 1, 2, 1, GridBagConstraints.WEST); A GridBag layout example Listing 5-1 shows the code for a program that displays the frame that I drew in Figure 5-5, and Figure 5-6 shows how this frame appears when the pro- gram is run. As you can see, the final appearance of this frame is pretty close to the way I sketched it out at McDonald’s. I could probably fix a few minor variations with a little tweaking. 43_58961X bk06ch05.qxd 3/29/05 3:41 PM Page 598 Book VI Chapter 5 Using Layout Managers Using GridBag Layout 599 LISTING 5-1:THE PIZZA ORDER APPLICATION import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Pizza extends JFrame { public static void main(String [] args) { new Pizza(); } JTextField name, phone, address; JRadioButton small, medium, large, thick, thin; JCheckBox pepperoni, mushrooms, anchovies; JButton okButton, closeButton; public Pizza() { this.setTitle(“Pizza Order”); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel1 = new JPanel(); panel1.setLayout(new GridBagLayout()); ➞ 23 addItem(panel1, new JLabel(“Name:”), ➞ 25 0, 0, 1, 1, GridBagConstraints.EAST); addItem(panel1, new JLabel(“Phone:”), 0, 1, 1, 1, GridBagConstraints.EAST); addItem(panel1, new JLabel(“Address:”), 0, 2, 1, 1, GridBagConstraints.EAST); name = new JTextField(20); phone = new JTextField(10); address = new JTextField(20); addItem(panel1, name, 1, 0, 2, 1, ➞ 36 GridBagConstraints.WEST); addItem(panel1, phone, 1, 1, 1, 1, continued Figure 5-6: The Pizza Order application in action. 43_58961X bk06ch05.qxd 3/29/05 3:41 PM Page 599 Using GridBag Layout 600 LISTING 5-1 (CONTINUED) GridBagConstraints.WEST); addItem(panel1, address, 1, 2, 2, 1, GridBagConstraints.WEST); Box sizeBox = Box.createVerticalBox(); ➞ 43 small = new JRadioButton(“Small”); medium = new JRadioButton(“Medium”); large = new JRadioButton(“Large”); ButtonGroup sizeGroup = new ButtonGroup(); sizeGroup.add(small); sizeGroup.add(medium); sizeGroup.add(large); sizeBox.add(small); sizeBox.add(medium); sizeBox.add(large); sizeBox.setBorder( BorderFactory.createTitledBorder(“Size”)); addItem(panel1, sizeBox, 0, 3, 1, 1, GridBagConstraints.NORTH); Box styleBox = Box.createVerticalBox(); ➞ 59 thin = new JRadioButton(“Thin”); thick = new JRadioButton(“Thick”); ButtonGroup styleGroup = new ButtonGroup(); styleGroup.add(thin); styleGroup.add(thick); styleBox.add(thin); styleBox.add(thick); styleBox.setBorder( BorderFactory.createTitledBorder(“Style”)); addItem(panel1, styleBox, 1, 3, 1, 1, GridBagConstraints.NORTH); Box topBox = Box.createVerticalBox(); ➞ 72 pepperoni = new JCheckBox(“Pepperoni”); mushrooms = new JCheckBox(“Mushrooms”); anchovies = new JCheckBox(“Anchovies”); ButtonGroup topGroup = new ButtonGroup(); topGroup.add(pepperoni); topGroup.add(mushrooms); topGroup.add(anchovies); topBox.add(pepperoni); topBox.add(mushrooms); topBox.add(anchovies); topBox.setBorder( BorderFactory.createTitledBorder(“Toppings”)); addItem(panel1, topBox, 2, 3, 1, 1, GridBagConstraints.NORTH); Box buttonBox = Box.createHorizontalBox(); ➞ 88 okButton = new JButton(“OK”); closeButton = new JButton(“Close”); buttonBox.add(okButton); buttonBox.add(Box.createHorizontalStrut(20)); buttonBox.add(closeButton); 43_58961X bk06ch05.qxd 3/29/05 3:41 PM Page 600 Book VI Chapter 5 Using Layout Managers Using GridBag Layout 601 addItem(panel1, buttonBox, 2, 4, 1, 1, GridBagConstraints.NORTH); this.add(panel1); this.pack(); this.setVisible(true); } private void addItem(JPanel p, JComponent c, int x, int y, int width, int height, int align) { GridBagConstraints gc = new GridBagConstraints(); gc.gridx = x; gc.gridy = y; gc.gridwidth = width; gc.gridheight = height; gc.weightx = 100.0; gc.weighty = 100.0; gc.insets = new Insets(5, 5, 5, 5); gc.anchor = align; gc.fill = GridBagConstraints.NONE; p.add(c, gc); } } Note that this application doesn’t include any event listeners, so the buttons don’t do anything other than demonstrate how to use the GridBag layout. The following paragraphs point out the highlights: ➞ 23 This line creates a GridBag layout manager for the panel. ➞ 25 These lines add the labels to the panel. ➞ 36 These lines add the text fields to the panel. ➞ 43 These lines use a vertical Box object to create the radio buttons that let the user select the size. ➞ 59 These lines use a vertical Box object to create the radio buttons that let the user select the crust style. ➞ 72 These lines use a vertical Box object to create the check boxes that let the user select check boxes. ➞ 88 These lines use a horizontal Box object to hold the OK and Close buttons. 43_58961X bk06ch05.qxd 3/29/05 3:41 PM Page 601 Book VI: Swing 602 43_58961X bk06ch05.qxd 3/29/05 3:41 PM Page 602 Book VII Web Programming 44_58961X pt07.qxd 3/29/05 3:40 PM Page 603 Contents at a Glance Chapter 1: Creating Applets 605 Chapter 2: Creating Servlets 613 Chapter 3: Using Java Server Pages 633 Chapter 4: Using JavaBeans 647 44_58961X pt07.qxd 3/29/05 3:40 PM Page 604 Chapter 1: Creating Applets In This Chapter ߜ Looking at applets ߜ Creating an applet ߜ Creating HTML to display applets ߜ Testing applets with the applet viewer A n applet is not a small piece of fruit. Rather, it’s a Java application that’s designed to run in a browser window on an Internet user’s com- puter. When an Internet user visits a Web page that contains an applet, the Java applet class is downloaded to the user’s computer and run there. The applet takes over a portion of the page and, within that space, can do any- thing it wants. Applets are, at least in most cases, Swing applications. As a result, every- thing that’s covered in Book VI applies to applets. In this chapter, you create applets that include Swing components. Then, you add an applet to a Web page so anyone who views the page can use it. Understanding Applets An applet is similar to a Swing application, with several crucial differences: ✦ Instead of extending the JFrame class, applets extend the JApplet class. Both JFrame and JApplet provide a “space” for your Swing application to operate in: • With JFrame, that space is a window that’s managed by the host operating system’s windowing system. • With JApplet, the space is a rectangular area of a Web page that’s managed by a Web browser. ✦ Stand-alone Swing applications are started when the JVM calls the static main method. Thus, a Swing application typically starts by creating an instance of the class that extends JFrame. In contrast, the browser auto- matically creates an instance of the class that extends JApplet when the applet is started. As a result, applets don’t have a static main method. Instead, a method named init is called to get the applet started. As a result, the init method is where you put the code that you’d put in the constructor for a class that extends JFrame. 45_58961X bk07ch01.qxd 3/29/05 3:40 PM Page 605 [...]... this tag: Change the port number from 80 80 to some other number, such as 180 80 Later, when you display servlets in a browser window, you have to specify this number as the HTTP port number instead of 80 80 Creating Servlets You know that Tomcat has successfully started up when you see a line such as the following indicating how long the startup took: 6 18 Using Tomcat You don’t... the form using a separate HTML file For example, Listing 2-3 shows an HTML file named InputServlet.html that displays the form shown in Figure 2-4 Book VII Chapter 2 626 Getting Input from the User Figure 2-4: A simple input form The action attribute in the form tag of this form specifies that /servlet/ InputServlet is called when the form is submitted, and the method attribute indicates that the form... servlet produces the page shown in Figure 2-3 Obviously, you need a solid understanding of HTML to write servlets If HTML is like a foreign language, you need to pick up a good HTML book, such as HTML 4 For Dummies by Ed Tittel and Natanya Pitts, before you go much further For your reference, Table 2-2 summarizes all the HTML tags that I use in this book Creating Servlets Here, the following HTML is sent... , The text between these tags is formatted as a level-1 heading , The text between these tags is formatted as a level-2 heading , The text between these tags is formatted as a level-3 heading Marks the start of a form The action attribute specifies the name of the page, servlet, or JSP the form is posted to The method attribute can... a simple Hello, World! type servlet Importing the servlet packages Most servlets need access to at least three packages — javax.servlet, javax.servlet.http, and java. io As a result, you usually start with these import statements: import java. io.*; import javax.servlet.*; import javax.servlet.http.*; Depending on what other processing your servlet does, you may need additional import statements Extending... year, price); } } } Listing 2-7 shows the code for the ListMovie servlet class LISTING 2-7: THE LISTMOVIE SERVLET CLASS import import import import java. io.*; javax.servlet.*; javax.servlet.http.*; java. util.*; public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { doGet(request, response); } ➞ 28 private String getMovieList() { String msg... environment variable named JAVA_ HOME that points to the location of your JDK A common mistake is to set this variable to the bin directory or to the directory for the JRE, not the JDK If Tomcat doesn’t start up later, double-check the JAVA_ HOME directory 4 Copy the servlet-api.jar file to the jre\lib\ext folder in your JDK root For example, if your JDF is installed in c:\Program Files \Java\ jdk1.5.0, copy... than a GET request The form itself consists of an input text field named name and a Submit button Nothing fancy; just enough to get some text from the user and send it to a servlet The InputServlet servlet Listing 2-4 shows a servlet that can retrieve the data from the form shown in Listing 2-3 LISTING 2-4: THE INPUTSERVLET SERVLET import java. io.*; import javax.servlet.*; import javax.servlet.http.*;... by an HTTP GET or POST request that came from a form, you can call the getParameter method of the request object to get the values entered by the user into each form field For example String name = request.getParameter(“name”); Here, the value entered into the form input field named name is retrieved and assigned to the String variable name Working with forms LISTING 2-3: THE INPUTSERVLET.HTML FILE ... example, Listing 2-1 shows a basic HelloWorld servlet that sends an HTML response Creating a Simple Servlet 621 LISTING 2-1: THE HELLOWORLD SERVLET import import import import java. io.*; javax.servlet.*; javax.servlet.http.*; java. util.*; public class HelloWorld extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType(“text/html”); . Web page. 45_ 589 61X bk07ch01.qxd 3/29/05 3:40 PM Page 607 Looking At a Sample Applet 6 08 LISTING 1-1:THE PIZZA ORDER APPLET import javax.swing.*; import java. awt.event.*; import javax.swing.border.*; public. select check boxes. ➞ 88 These lines use a horizontal Box object to hold the OK and Close buttons. 43_ 589 61X bk06ch05.qxd 3/29/05 3:41 PM Page 601 Book VI: Swing 602 43_ 589 61X bk06ch05.qxd 3/29/05. tweaking. 43_ 589 61X bk06ch05.qxd 3/29/05 3:41 PM Page 5 98 Book VI Chapter 5 Using Layout Managers Using GridBag Layout 599 LISTING 5-1:THE PIZZA ORDER APPLICATION import javax.swing.*; import java. awt.event.*; import

Ngày đăng: 12/08/2014, 19:21

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan